> ## Documentation Index
> Fetch the complete documentation index at: https://docs.openp.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Anthropic 官方 SDK

> 使用 anthropic SDK 通过 OpenPAI 调用 Claude

Anthropic 官方 SDK 通过覆盖 `base_url` 即可指向 OpenPAI。

## Python

```bash theme={null}
pip install anthropic
```

```python theme={null}
from anthropic import Anthropic

client = Anthropic(
    api_key="sk-XXXXXXXXXXXXXXXX",      # 你的 OpenPAI Key
    base_url="https://openp.ai",         # 注意:不带 /v1
)

resp = client.messages.create(
    model="claude-opus-4-8",
    max_tokens=1024,
    system="你是 Python 工程师",
    messages=[{"role": "user", "content": "用 Python 写一个 LRU Cache"}],
)
print(resp.content[0].text)
```

## Node.js / TypeScript

```bash theme={null}
npm install @anthropic-ai/sdk
```

```ts theme={null}
import Anthropic from "@anthropic-ai/sdk";

const client = new Anthropic({
  apiKey: process.env.OPENPAI_API_KEY!,
  baseURL: "https://openp.ai",
});

const resp = await client.messages.create({
  model: "claude-opus-4-8",
  max_tokens: 1024,
  messages: [{ role: "user", content: "hello" }],
});
```

## 流式

```python theme={null}
with client.messages.stream(
    model="claude-opus-4-8",
    max_tokens=1024,
    messages=[{"role": "user", "content": "讲个故事"}],
) as stream:
    for text in stream.text_stream:
        print(text, end="", flush=True)
```

## 工具调用

```python theme={null}
resp = client.messages.create(
    model="claude-opus-4-8",
    max_tokens=1024,
    tools=[{
        "name": "get_weather",
        "description": "获取城市天气",
        "input_schema": {
            "type": "object",
            "properties": {"city": {"type": "string"}},
            "required": ["city"],
        }
    }],
    messages=[{"role": "user", "content": "查上海今天的天气"}],
)
for block in resp.content:
    if block.type == "tool_use":
        print(block.name, block.input)
```

## 思考模式

```python theme={null}
resp = client.messages.create(
    model="claude-opus-4-8",
    max_tokens=4096,
    thinking={"type": "enabled", "budget_tokens": 8000},
    messages=[{"role": "user", "content": "证明费马小定理"}],
)
for block in resp.content:
    if block.type == "thinking":
        print("[思考]", block.thinking)
    elif block.type == "text":
        print("[回答]", block.text)
```

## 提示缓存

```python theme={null}
resp = client.messages.create(
    model="claude-opus-4-8",
    max_tokens=1024,
    system=[
        {
            "type": "text",
            "text": "<长 system prompt>",
            "cache_control": {"type": "ephemeral"},
        }
    ],
    messages=[{"role": "user", "content": "..."}],
)
```

详见 [缓存计费](/pricing/cache-billing)。

## 视觉

```python theme={null}
resp = client.messages.create(
    model="claude-opus-4-8",
    max_tokens=1024,
    messages=[{
        "role": "user",
        "content": [
            {"type": "text", "text": "这张图里是什么?"},
            {"type": "image", "source": {
                "type": "base64",
                "media_type": "image/png",
                "data": "<base64-string>",
            }},
        ],
    }],
)
```

## 注意事项

* OpenPAI 同时支持 OpenAI 兼容协议调用 Claude(`/v1/chat/completions`),但若你的代码已经使用 Anthropic SDK,**直接覆盖 base\_url 是最低改动方案**。
* `anthropic-version` 头会由 SDK 自动添加,无需手动指定。
* 上游 Anthropic 官方 API 暂未开放给所有地区,OpenPAI 提供稳定接入。
