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

# 通过 Anthropic SDK 将 Claude 请求路由到 Flatkey

> 在 Anthropic SDK 中设置基础 URL，将 Anthropic Claude 请求路由到 Flatkey，低至官方价格的 5 折使用 Claude。

将 Anthropic Python SDK 的请求路由到 Flatkey，即可低至 Anthropic 官方价格的 5 折使用 Claude 模型。只需设置一次 `base_url` 参数，现有 SDK 代码便可原样工作，无需其他改动。

## 安装

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

## 基础配置

```python python theme={null}
import os
import anthropic

client = anthropic.Anthropic(
    api_key=os.environ["FLATKEY_API_KEY"],
    base_url="https://router.flatkey.ai",
)
```

<Note>
  使用 Anthropic SDK 时，请将 `base_url` 设为 `https://router.flatkey.ai`，不要添加 `/v1`。SDK 会自动追加请求路径。
</Note>

## 发送消息

```python python theme={null}
message = client.messages.create(
    model="claude-sonnet-4-5",
    max_tokens=1024,
    messages=[
        {"role": "user", "content": "Write a Python function to reverse a linked list."}
    ],
)

print(message.content[0].text)
```

## 系统提示词

```python python theme={null}
message = client.messages.create(
    model="claude-opus-4-5",
    max_tokens=2048,
    system="You are a senior software engineer. Provide concise, idiomatic code.",
    messages=[
        {"role": "user", "content": "Implement a rate limiter in Python."}
    ],
)

print(message.content[0].text)
```

## 流式传输

```python python theme={null}
with client.messages.stream(
    model="claude-haiku-4-5-20251001",
    max_tokens=512,
    messages=[{"role": "user", "content": "Explain how HTTPS works."}],
) as stream:
    for text in stream.text_stream:
        print(text, end="", flush=True)
print()
```

## 多轮对话

```python python theme={null}
conversation = [
    {"role": "user", "content": "What is the capital of France?"},
    {"role": "assistant", "content": "The capital of France is Paris."},
    {"role": "user", "content": "What is its population?"},
]

response = client.messages.create(
    model="claude-sonnet-4-5",
    max_tokens=256,
    messages=conversation,
)

print(response.content[0].text)
```

## 可用的 Claude 模型

| 模型                          | 适用场景       | 输入价格（奖励后）               |
| --------------------------- | ---------- | ----------------------- |
| `claude-opus-4-5`           | 复杂推理、长篇写作  | 约 \$3 / 100 万 tokens    |
| `claude-sonnet-4-5`         | 质量与速度均衡    | 约 \$1.6 / 100 万 tokens  |
| `claude-haiku-4-5-20251001` | 快速、经济高效的任务 | 约 \$0.53 / 100 万 tokens |

所有 Claude 模型和最新价格请参阅[模型目录](/zh/reference/model-list)。

<Tip>
  你也可以通过 Flatkey 使用 OpenAI SDK 调用 Claude 模型，只需在 `client.chat.completions.create()` 调用中设置 `model="claude-sonnet-4-5"`。请选择最适合项目的 SDK。
</Tip>
