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

> Gunakan TokenLab dengan Anthropic SDK untuk kompatibilitas Messages API native Claude

## Gambaran Umum

TokenLab mendukung path native Anthropic **Messages API**, sehingga Anda dapat menggunakan Anthropic SDK resmi secara langsung untuk model Claude.

<Note>
  Untuk Anthropic SDK, gunakan `https://api.tokenlab.sh` sebagai base URL, tanpa menambahkan `/v1` sendiri.
</Note>

Di antara rute SDK yang didokumentasikan, ini adalah salah satu jalur TokenLab yang paling didukung untuk fitur native Claude.

<Note>
  **Jenis**: SDK native

  **Jalur utama**: Anthropic-native

  **Tingkat dukungan**: Jalur native yang kuat
</Note>

## Instalasi

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

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

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

## Penggunaan Dasar

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

## Penggunaan Tool

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

## Pemikiran Mendalam

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

## Model Claude yang Direkomendasikan

| Model               | Paling Cocok Untuk                   |
| ------------------- | ------------------------------------ |
| `claude-opus-4-6`   | Penalaran mendalam, analisis panjang |
| `claude-sonnet-4-6` | Coding, tugas assistant umum         |
| `claude-haiku-4-5`  | Respons cepat dan ringan             |

## Pemecahan Masalah

<AccordionGroup>
  <Accordion title="Base URL Salah">
    * Gunakan `https://api.tokenlab.sh`
    * Jangan tambahkan `/v1` secara manual saat mengonfigurasi Anthropic SDK
  </Accordion>

  <Accordion title="Autentikasi Gagal">
    * Periksa bahwa TokenLab API key Anda diawali dengan `sk-`
    * Konfirmasikan bahwa key tersebut aktif di dashboard TokenLab
    * Biarkan Anthropic SDK mengelola auth header alih-alih menambahkan custom headers secara manual
  </Accordion>

  <Accordion title="Model Tidak Ditemukan">
    * Verifikasi nama model Claude secara tepat
    * Periksa ketersediaan saat ini di katalog model TokenLab
  </Accordion>
</AccordionGroup>
