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

# Anthropic SDK drop-in integration

> Route Anthropic SDK calls through the Toqan Router AI Gateway. Access fallbacks, caching, load balancing, and cost tracking for all Claude models.

> [card] [AI Gateway](#ai-gateway)
>
> Route Claude calls through the **AI Gateway** with a single base URL change. Zero vendor lock-in: always run on the best model at the lowest cost for the use case.

> [card] [Claude Agent SDK](/docs/ai-studio/integrations/frameworks/claude-agent-sdk.md)
>
> Building programmable agents with multi-turn conversations, tool use, and MCP servers? Use the Claude Agent SDK instead.

> [card] [Anthropic Messages API](/docs/ai-gateway/features/anthropic-messages-api.md)
>
> Endpoint details, authentication, and cache\_control breakpoints on the Anthropic-compatible endpoint.

## AI Gateway

### Overview

The Anthropic SDK (`@anthropic-ai/sdk` and `anthropic`) is the native client for the Claude Messages API. Connect it to **Toqan Router**'s **AI Gateway** with a single `base_url` change and no other modifications to existing code, unlocking 500+ models across 30+ providers, fallbacks, caching, and cost tracking.

> [!NOTE]
>
> For programmable agents with multi-turn conversations, tool use, and MCP servers, see the [Claude Agent SDK](/docs/ai-studio/integrations/frameworks/claude-agent-sdk.md) integration.

### Key Benefits

Toqan Router's **AI Gateway** enhances the Anthropic SDK with:

> [card] Complete Observability
>
> Track every API call, token usage, and model interaction with detailed traces and analytics

> [card] Built-in Reliability
>
> Automatic fallbacks, retries, and load balancing for production resilience

> [card] Cost Optimization
>
> Real-time cost tracking and spend management across all AI operations

> [card] Multi-Provider Access
>
> Access 500+ LLMs and 30+ providers through a single, unified integration

### Prerequisites

Before integrating the Anthropic SDK with **Toqan Router**, ensure the following are in place:

* A Toqan Router account and API Key
* Python 3.8+ or Node.js 18+ with TypeScript support
* Anthropic SDK installed

> [!NOTE]
>
> To set up an API key, see API keys & Endpoints.

### Installation

```bash TypeScript theme={"theme":{"light":"github-light","dark":"github-dark"}}
npm install @anthropic-ai/sdk
```

```bash Python theme={"theme":{"light":"github-light","dark":"github-dark"}}
pip install anthropic
```

### Configuration

Set the **Base URL** to the [AI Gateway](/docs/ai-gateway/using-the-router.md) and authenticate with a **Toqan Router** API key to route calls through the API without changing any other part of the code.

> **base\_url**: `https://router.toqan.ai/eu/v3/anthropic`

```typescript TypeScript theme={"theme":{"light":"github-light","dark":"github-dark"}}
import Anthropic from '@anthropic-ai/sdk';

const client = new Anthropic({
  apiKey: process.env.TOQAN_API_KEY,
  baseURL: 'https://router.toqan.ai/eu/v3/anthropic',
});
```

```python Python theme={"theme":{"light":"github-light","dark":"github-dark"}}
import os
import anthropic

client = anthropic.Anthropic(
    api_key=os.environ["TOQAN_API_KEY"],
    base_url="https://router.toqan.ai/eu/v3/anthropic",
)
```

### Text Generation

Basic text generation with the Anthropic SDK through Toqan Router:

```typescript TypeScript theme={"theme":{"light":"github-light","dark":"github-dark"}}
import Anthropic from '@anthropic-ai/sdk';

const client = new Anthropic({
  apiKey: process.env.TOQAN_API_KEY,
  baseURL: 'https://router.toqan.ai/eu/v3/anthropic',
});

const message = await client.messages.create({
  model: 'anthropic/claude-sonnet-5',
  max_tokens: 1024,
  messages: [
    { role: 'user', content: 'Explain the Toqan Router AI Gateway in one paragraph.' },
  ],
});

const block = message.content[0];
if (block.type === 'text') {
  console.log(block.text);
}
```

```python Python theme={"theme":{"light":"github-light","dark":"github-dark"}}
import os
import anthropic

client = anthropic.Anthropic(
    api_key=os.environ["TOQAN_API_KEY"],
    base_url="https://router.toqan.ai/eu/v3/anthropic",
)

message = client.messages.create(
    model="anthropic/claude-sonnet-5",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Explain the Toqan Router AI Gateway in one paragraph."}],
)

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

### Streaming

Stream responses for real-time output:

```typescript TypeScript theme={"theme":{"light":"github-light","dark":"github-dark"}}
import Anthropic from '@anthropic-ai/sdk';

const client = new Anthropic({
  apiKey: process.env.TOQAN_API_KEY,
  baseURL: 'https://router.toqan.ai/eu/v3/anthropic',
});

const stream = client.messages.stream({
  model: 'anthropic/claude-sonnet-5',
  max_tokens: 1024,
  messages: [
    { role: 'user', content: 'Write a haiku about AI routing.' },
  ],
});

stream.on('text', (text) => {
  process.stdout.write(text);
});

await stream.finalMessage();
```

```python Python theme={"theme":{"light":"github-light","dark":"github-dark"}}
import os
import anthropic

client = anthropic.Anthropic(
    api_key=os.environ["TOQAN_API_KEY"],
    base_url="https://router.toqan.ai/eu/v3/anthropic",
)

with client.messages.stream(
    model="anthropic/claude-sonnet-5",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Write a haiku about AI routing."}],
) as stream:
    for text in stream.text_stream:
        print(text, end="", flush=True)
```

### Tool Use

Define tools and let the model decide when to call them. The **AI Gateway** forwards tool definitions and returns `tool_use` blocks unchanged:

```typescript TypeScript theme={"theme":{"light":"github-light","dark":"github-dark"}}
import Anthropic from '@anthropic-ai/sdk';

const client = new Anthropic({
  apiKey: process.env.TOQAN_API_KEY,
  baseURL: 'https://router.toqan.ai/eu/v3/anthropic',
});

const message = await client.messages.create({
  model: 'anthropic/claude-sonnet-5',
  max_tokens: 1024,
  tools: [
    {
      name: 'get_weather',
      description: 'Get the current weather for a location',
      input_schema: {
        type: 'object',
        properties: {
          location: { type: 'string', description: 'City name, e.g. Amsterdam' },
        },
        required: ['location'],
      },
    },
  ],
  messages: [
    { role: 'user', content: 'What is the weather in Amsterdam?' },
  ],
});

for (const block of message.content) {
  if (block.type === 'tool_use') {
    console.log(`Tool: ${block.name}`, block.input);
  }
}
```

```python Python theme={"theme":{"light":"github-light","dark":"github-dark"}}
import os
import anthropic

client = anthropic.Anthropic(
    api_key=os.environ["TOQAN_API_KEY"],
    base_url="https://router.toqan.ai/eu/v3/anthropic",
)

message = client.messages.create(
    model="anthropic/claude-sonnet-5",
    max_tokens=1024,
    tools=[
        {
            "name": "get_weather",
            "description": "Get the current weather for a location",
            "input_schema": {
                "type": "object",
                "properties": {
                    "location": {"type": "string", "description": "City name, e.g. Amsterdam"},
                },
                "required": ["location"],
            },
        }
    ],
    messages=[{"role": "user", "content": "What is the weather in Amsterdam?"}],
)

for block in message.content:
    if block.type == "tool_use":
        print(f"Tool: {block.name}", block.input)
```

### Model Selection

The `anthropic/` prefix routes to Claude models. Switch to any supported model from 30+ providers by changing the `model` value:

```typescript TypeScript theme={"theme":{"light":"github-light","dark":"github-dark"}}
const message = await client.messages.create({
  model: 'openai/gpt-5.6-sol',
  max_tokens: 1024,
  messages: [{ role: 'user', content: 'Explain machine learning' }],
});
```

```python Python theme={"theme":{"light":"github-light","dark":"github-dark"}}
message = client.messages.create(
    model="openai/gpt-5.6-sol",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Explain machine learning"}],
)
```

> [!NOTE]
>
> Browse every available model in [Supported Models](/docs/ai-gateway/supported-models.md).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.