# Anthropic SDK (/docs/integrations/anthropic-sdk)

> For AI agents, the [documentation index](https://bitrouter.ai/llms.txt) lists all pages. Append .md to a documentation URL to read its Markdown.

<IntegrationHeader id="anthropic-sdk" />

The Anthropic SDKs work with Spare unchanged: set the base URL and your Spare key. Messages, streaming
and tool use work as they do with Anthropic.

## Set up \[#set-up]

The base URL has no `/v1`: the SDK adds it.

**Python**

```python
import os
from anthropic import Anthropic

client = Anthropic(
    base_url="https://api.spareinference.ai",
    api_key=os.environ["SPARE_API_KEY"],
)

message = client.messages.create(
    model="deepseek-v4.1-flash",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Hello"}],
)
```

**TypeScript**

```ts
import Anthropic from '@anthropic-ai/sdk';

const client = new Anthropic({
  baseURL: 'https://api.spareinference.ai',
  apiKey: process.env.SPARE_API_KEY,
});

const message = await client.messages.create({
  model: 'deepseek-v4.1-flash',
  max_tokens: 1024,
  messages: [{ role: 'user', content: 'Hello' }],
});
```

The SDK sends the key in `x-api-key`, which Spare accepts on every endpoint.

## See what a request cost \[#see-what-a-request-cost]

Each response carries an `x-bitrouter-request-id` header. Read it with the raw-response helpers, then look
the request up with [settlement](https://bitrouter.ai/docs/api/settlement) for its token counts, prices and total, alongside the parsed message:

**Python**

```python
raw = client.messages.with_raw_response.create(
    model="deepseek-v4.1-flash",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Hello"}],
)
request_id = raw.headers["x-bitrouter-request-id"]
message = raw.parse()
```

**TypeScript**

```ts
const { data: message, response } = await client.messages
  .create({ model: 'deepseek-v4.1-flash', max_tokens: 1024, messages: [{ role: 'user', content: 'Hello' }] })
  .withResponse();
const requestId = response.headers.get('x-bitrouter-request-id');
```

When streaming, the final `message_delta` event carries `usage`.

## Fallbacks \[#fallbacks]

There's nothing to set up for provider failures: a request moves to the next provider serving the model on
its own. To fall back to a different model, such as your Claude model, chain them in the CLI; see
[Fall back to a different model](https://bitrouter.ai/docs/fleet/fallbacks).

To run a [routing preset](https://bitrouter.ai/docs/fleet/provider-selection) instead of a fixed model, pass `model="@<name>"`.

## Keep Claude, add the Router \[#keep-claude-add-the-router]

Spare's API runs the model you name and doesn't route between models; the Router runs in the CLI on your machine. Set it up following [Cost routing](https://bitrouter.ai/docs/fleet/cost-routing#set-up), then point the SDK at the CLI
and ask for `spare/auto`:

```python
client = Anthropic(
    base_url="http://127.0.0.1:4356",   # the CLI on your machine
    api_key=os.environ.get("BITROUTER_API_KEY", "bitrouter-local"),
)

message = client.messages.create(
    model="spare/auto",
    max_tokens=1024,
    messages=messages,
)
```

The CLI holds your Spare and Claude keys. `bitrouter-local` works when the CLI allows local
requests without a key.

## Good to know \[#good-to-know]

* **Prompt caching is automatic.** `cache_control` blocks are accepted and ignored.
* **Errors keep Anthropic's shape,** so `anthropic.RateLimitError` and the other exception classes work
  as before. A 429 carries `Retry-After`, which the SDK honors when it retries.
* **Server tools** that run on Anthropic's side, like web search, aren't available on DeepSeek V4.1
  Flash. Client tools you define work as usual.

## Check it works \[#check-it-works]

```python
raw = client.messages.with_raw_response.create(
    model="deepseek-v4.1-flash",
    max_tokens=16,
    messages=[{"role": "user", "content": "Reply with OK"}],
)
print(raw.headers["x-bitrouter-request-id"])
```