Anthropic SDK Integration (Python / TypeScript)
Call 1zheAI using the official Anthropic SDK: set base_url to https://1zheai.com. Natively supports caching, streaming, tool calls, and thinking.
The official Anthropic SDK uses the native /v1/messages, so all Claude parameters can be used directly. The only things to change are base_url and api_key.
Installation
Section titled “Installation”pip install anthropicnpm install @anthropic-ai/sdkCalling
Section titled “Calling”import osimport anthropic
client = anthropic.Anthropic( base_url="https://1zheai.com", api_key=os.environ["ONEZHE_API_KEY"],)
message = client.messages.create( model="claude-sonnet-5-5", max_tokens=1024, messages=[{"role": "user", "content": "用一句话介绍你自己"}],)print(message.model)print(message.content[0].text)print(message.usage)import Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic({ baseURL: "https://1zheai.com", apiKey: process.env.ONEZHE_API_KEY,});
const message = await client.messages.create({ model: "claude-sonnet-5-5", max_tokens: 1024, messages: [{ role: "user", content: "用一句话介绍你自己" }],});console.log(message.model);console.log(message.content);console.log(message.usage);Fill in the root address in base_url; do not add /v1.
Streaming Output
Section titled “Streaming Output”with client.messages.stream( model="claude-sonnet-5-5", max_tokens=1024, messages=[{"role": "user", "content": "写一首四行的诗"}],) as stream: for text in stream.text_stream: print(text, end="", flush=True)const stream = client.messages.stream({ model: "claude-sonnet-5-5", max_tokens: 1024, messages: [{ role: "user", content: "写一首四行的诗" }],});stream.on("text", (text) => process.stdout.write(text));await stream.finalMessage();Calls with Caching
Section titled “Calls with Caching”Put long, fixed content into system and add cache_control:
message = client.messages.create( model="claude-sonnet-5-5", max_tokens=1024, system=[ { "type": "text", "text": LONG_PROJECT_CONTEXT, # 每次都一样的长文本 "cache_control": {"type": "ephemeral"}, } ], messages=[{"role": "user", "content": "根据上面的说明回答:..."}],)print(message.usage.cache_creation_input_tokens, message.usage.cache_read_input_tokens)On the second call, if cache_read_input_tokens is greater than 0, it was a cache hit. See Prompt Caching for details.
Using Environment Variables
Section titled “Using Environment Variables”The official SDK reads ANTHROPIC_BASE_URL and ANTHROPIC_API_KEY, so you can skip passing parameters when constructing the client:
export ANTHROPIC_BASE_URL="https://1zheai.com"export ANTHROPIC_API_KEY="sk-你的Key"Then construct the client without passing parameters:
client = anthropic.Anthropic() # 自动读取上面两个变量If Claude Code on this machine also reads these two variables, they will both switch to 1zheAI, which is usually exactly what you want.
404?
base_url has an extra /v1. The Anthropic SDK will assemble /v1/messages itself.
401?
Check the API key (API key); additionally, if ANTHROPIC_AUTH_TOKEN exists in the environment at the same time (for example, an old value previously set for Claude Code), the SDK will also read it — delete the old value and try again.
Request and Response Structure
Section titled “Request and Response Structure”To query system, content blocks, and stop reasons, see Messages API Details. A complete program should read text by content block type and not assume the first block is always text.
