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

# Anthropic SDK

> How to use the Anthropic Python and TypeScript SDKs with to11.

This guide shows you how to point the official Anthropic SDK at the to11 gateway. Set the `base_url` to your gateway's `/v1` endpoint and use the Anthropic messages API as usual.

## Python

```bash theme={null}
pip install anthropic
```

```python theme={null}
import anthropic

client = anthropic.Anthropic(
    base_url="https://gw.to11.ai/v1",
    api_key="<your Anthropic API key>",  # provider credential, forwarded upstream
    default_headers={
        "x-to11-authorization": "Bearer <your to11 API key>",  # to11 auth — Settings → API keys
        "x-to11-project-id": "<your project id>",
    },
)

message = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=256,
    messages=[{"role": "user", "content": "Hello from to11!"}],
)
print(message.content[0].text)
```

## TypeScript / Node.js

```bash theme={null}
npm install @anthropic-ai/sdk
```

```typescript theme={null}
import Anthropic from "@anthropic-ai/sdk";

const client = new Anthropic({
  baseURL: "https://gw.to11.ai/v1",
  apiKey: "<your Anthropic API key>", // provider credential, forwarded upstream
  defaultHeaders: {
    "x-to11-authorization": "Bearer <your to11 API key>", // to11 auth — Settings → API keys
    "x-to11-project-id": "<your project id>",
  },
});

const message = await client.messages.create({
  model: "claude-sonnet-4-6",
  max_tokens: 256,
  messages: [{ role: "user", content: "Hello from to11!" }],
});
console.log(message.content[0].text);
```

## Streaming

<CodeGroup>
  ```python Python theme={null}
  with client.messages.stream(
      model="claude-sonnet-4-6",
      max_tokens=256,
      messages=[{"role": "user", "content": "Count to 10"}],
  ) as stream:
      for text in stream.text_stream:
          print(text, end="", flush=True)
  ```

  ```typescript TypeScript theme={null}
  const stream = client.messages.stream({
    model: "claude-sonnet-4-6",
    max_tokens: 256,
    messages: [{ role: "user", content: "Count to 10" }],
  });

  for await (const event of stream) {
    if (
      event.type === "content_block_delta" &&
      event.delta.type === "text_delta"
    ) {
      process.stdout.write(event.delta.text);
    }
  }
  ```
</CodeGroup>

Streamed responses are traced just like non-streaming calls — the gateway captures the same span and token counts either way.

## System messages

Pass a top-level `system` parameter as you normally would:

```python theme={null}
message = client.messages.create(
    model="claude-sonnet-4-6",
    system="You are a helpful assistant that speaks only in haiku.",
    max_tokens=256,
    messages=[{"role": "user", "content": "Tell me about the ocean"}],
)
```

## Tool use

Tool definitions follow the Anthropic `input_schema` format:

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

## Extended thinking

Extended thinking blocks are preserved in Anthropic-format responses when enabled on the model. The `thinking` content type passes through the gateway as-is.

## Cross-format routing

If you route an Anthropic-format request to an OpenAI model, the gateway translates automatically:

```python theme={null}
# Anthropic SDK format, but targeting an OpenAI model
message = client.messages.create(
    model="gpt-4o",
    max_tokens=256,
    messages=[{"role": "user", "content": "Hello!"}],
)
```

The `system` parameter is converted to a `{"role": "system"}` message, and tool definitions are translated to OpenAI's `parameters` format.
