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

> Use TokenLab with the Anthropic SDK for Claude-native Messages API compatibility

## Overview

TokenLab supports the native Anthropic **Messages API** path, so you can use the official Anthropic SDK directly for Claude models.

Among the documented SDK routes, this is one of the strongest-supported TokenLab paths for Claude-native features.

<Note>
  For the Anthropic SDK, use `https://api.tokenlab.sh` as the base URL, without appending `/v1` yourself.
</Note>

<Note>
  **Type**: Native SDK

  **Primary Path**: Anthropic-native

  **Support Confidence**: Strong native path
</Note>

## Installation

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

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

## Configure the Client

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

## Basic Usage

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

## Streaming

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

## Recommended Claude Models

| Model               | Best For                           |
| ------------------- | ---------------------------------- |
| `claude-opus-4-6`   | Deep reasoning, long-form analysis |
| `claude-sonnet-4-6` | Coding, general assistant tasks    |
| `claude-haiku-4-5`  | Fast, lightweight responses        |

## Troubleshooting

<AccordionGroup>
  <Accordion title="Wrong Base URL">
    * Use `https://api.tokenlab.sh`
    * Do not manually append `/v1` when configuring the Anthropic SDK
  </Accordion>

  <Accordion title="Authentication Failed">
    * Check that your TokenLab API key starts with `sk-`
    * Confirm the key is active in TokenLab dashboard
    * Let the Anthropic SDK manage the auth header instead of adding custom headers manually
  </Accordion>

  <Accordion title="Model Not Found">
    * Verify the Claude model name exactly
    * Check current availability in the TokenLab model catalog
  </Accordion>
</AccordionGroup>
