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

# Anthropic SDK

> 使用 TokenLab 与 Anthropic SDK，以兼容 Claude 原生 Messages API

## 概述

TokenLab 支持原生 Anthropic **Messages API** 路径，因此你可以直接将官方 Anthropic SDK 用于 Claude 模型。

<Note>
  对于 Anthropic SDK，请使用 `https://api.tokenlab.sh` 作为 base URL，不要自行追加 `/v1`。
</Note>

<Note>
  **类型**: 原生 SDK

  **主要路径**: Anthropic-native

  **支持级别**: 强原生路径
</Note>

在已文档化的 SDK 路径中，这是 Claude 原生功能支持性最强的一条。

## 安装

<CodeGroup>
  ```bash Python theme={null}
  pip install anthropic
  ```

  ```bash JavaScript theme={null}
  npm install @anthropic-ai/sdk
  ```
</CodeGroup>

## 配置客户端

<CodeGroup>
  ```python Python theme={null}
  from anthropic import Anthropic

  client = Anthropic(
      api_key="sk-your-tokenlab-key",
      base_url="https://api.tokenlab.sh",
  )
  ```

  ```javascript JavaScript theme={null}
  import Anthropic from '@anthropic-ai/sdk';

  const client = new Anthropic({
    apiKey: 'sk-your-tokenlab-key',
    baseURL: 'https://api.tokenlab.sh',
  });
  ```
</CodeGroup>

## 基本用法

```python theme={null}
message = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=1024,
    messages=[
        {"role": "user", "content": "Explain TokenLab in one sentence."}
    ]
)

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

## 流式传输

```python theme={null}
with client.messages.stream(
    model="claude-sonnet-4-6",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Write a short poem about coding."}]
) as stream:
    for text in stream.text_stream:
        print(text, end="", flush=True)
```

## 视觉

```python theme={null}
import base64

message = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=1024,
    messages=[{
        "role": "user",
        "content": [
            {"type": "text", "text": "What's in this image?"},
            {
                "type": "image",
                "source": {
                    "type": "url",
                    "url": "https://example.com/image.jpg"
                }
            }
        ]
    }]
)

with open("image.png", "rb") as f:
    image_data = base64.b64encode(f.read()).decode()

message = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=1024,
    messages=[{
        "role": "user",
        "content": [
            {"type": "text", "text": "Describe this image"},
            {
                "type": "image",
                "source": {
                    "type": "base64",
                    "media_type": "image/png",
                    "data": image_data
                }
            }
        ]
    }]
)
```

## 工具使用

```python theme={null}
message = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=1024,
    tools=[{
        "name": "get_weather",
        "description": "Get the weather for a location",
        "input_schema": {
            "type": "object",
            "properties": {
                "location": {"type": "string"}
            },
            "required": ["location"]
        }
    }],
    messages=[{"role": "user", "content": "What's the weather in Tokyo?"}]
)

for block in message.content:
    if block.type == "tool_use":
        print(block.name)
        print(block.input)
```

## 扩展思考

```python theme={null}
message = client.messages.create(
    model="claude-opus-4-6",
    max_tokens=16000,
    thinking={
        "type": "enabled",
        "budget_tokens": 10000
    },
    messages=[{"role": "user", "content": "Solve this complex problem step by step."}]
)

for block in message.content:
    if block.type == "thinking":
        print(block.thinking)
    elif block.type == "text":
        print(block.text)
```

## 推荐的 Claude 模型

| 模型                  | 最适合       |
| ------------------- | --------- |
| `claude-opus-4-6`   | 深度推理、长篇分析 |
| `claude-sonnet-4-6` | 编码、通用助手任务 |
| `claude-haiku-4-5`  | 快速、轻量级响应  |

## 故障排查

<AccordionGroup>
  <Accordion title="错误的 Base URL">
    * 使用 `https://api.tokenlab.sh`
    * 配置 Anthropic SDK 时，不要手动追加 `/v1`
  </Accordion>

  <Accordion title="身份验证失败">
    * 检查你的 TokenLab API key 是否以 `sk-` 开头
    * 确认该 key 已在 TokenLab dashboard 中激活
    * 让 Anthropic SDK 管理 auth header，而不是手动添加自定义 headers
  </Accordion>

  <Accordion title="找不到模型">
    * 精确核对 Claude 模型名称
    * 在 TokenLab 模型目录中检查当前可用性
  </Accordion>
</AccordionGroup>
