---
title: "Anthropic SDK Integration (Python / TypeScript)"
description: "Call 1zheAI using the official Anthropic SDK: set base_url to https://1zheai.com. Natively supports caching, streaming, tool calls, and thinking."
headingIds: {"Installation":"section-1","Calling":"section-2","Streaming Output":"section-3","Calls with Caching":"section-4","Using Environment Variables":"section-5","FAQ":"section-6","Request and Response Structure":"section-7"}
---

<aside className="docs-publication" data-pagefind-ignore>

Documentation maintained by [1ZheAI](https://1zheai.com/en/editorial/#publisher). [Sources and corrections](https://1zheai.com/en/editorial/).

Found an error? [Contact support](mailto:support@1zheai.com) with the page address and a description. Do not send API keys or account credentials.

</aside>
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

<CodeGroup>
```bash title="Python"
pip install anthropic
```
```bash title="TypeScript / Node.js"
npm install @anthropic-ai/sdk
```
</CodeGroup>

## Calling

<CodeGroup>
```python title="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)
```
```typescript title="TypeScript"
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);
```
</CodeGroup>

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

## Streaming Output

<CodeGroup>
```python title="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)
```
```typescript title="TypeScript"
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();
```
</CodeGroup>

## Calls with Caching

Put long, fixed content into `system` and add `cache_control`:

```python title="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](/en/docs/prompt-caching/) for details.

## Using Environment Variables

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.

## FAQ

<AccordionGroup>
<Accordion title="404？">

`base_url` has an extra `/v1`. The Anthropic SDK will assemble `/v1/messages` itself.

</Accordion>
<Accordion title="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.

</Accordion>
</AccordionGroup>

## Request and Response Structure

To query system, content blocks, and stop reasons, see [Messages API Details](/en/docs/api-messages/). A complete program should read text by content block type and not assume the first block is always text.
