> ## 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

> Claude-native Messages API 互換性のために Anthropic SDK で TokenLab を使用します

## 概要

TokenLab はネイティブの Anthropic **Messages API** パスをサポートしているため、Claude モデルに対して公式の Anthropic SDK を直接使用できます。

<Note>
  Anthropic SDK では、base URL として `https://api.tokenlab.sh` を使用し、自分で `/v1` を追加しないでください。
</Note>

<Note>
  **種類**: ネイティブ SDK

  **主要パス**: Anthropic-native

  **サポートレベル**: 強い ネイティブ経路
</Note>

文書化されている SDK ルートの中でも、Claude-native 機能に対して特に手厚くサポートされているパスの一つです。

## インストール

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

## Vision

```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
                }
            }
        ]
    }]
)
```

## Tool Use

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

## Extended Thinking

```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-` で始まっていることを確認してください
    * キーが TokenLab ダッシュボードで有効になっていることを確認してください
    * カスタムヘッダーを手動で追加するのではなく、認証ヘッダーは Anthropic SDK に管理させてください
  </Accordion>

  <Accordion title="モデルが見つかりません">
    * Claude モデル名が正確であることを確認してください
    * TokenLab のモデルカタログで現在の利用可否を確認してください
  </Accordion>
</AccordionGroup>
