Skip to content
English

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.

Python
pip install anthropic
Python
import os
import 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)

Fill in the root address in base_url; do not add /v1.

Python
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)

Put long, fixed content into system and add cache_control:

Python
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.

The official SDK reads ANTHROPIC_BASE_URL and ANTHROPIC_API_KEY, so you can skip passing parameters when constructing the client:

bash
export ANTHROPIC_BASE_URL="https://1zheai.com"
export ANTHROPIC_API_KEY="sk-你的Key"

Then construct the client without passing parameters:

python
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.

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.