# Anthropic SDK

Use the Anthropic Python and TypeScript SDKs with Boundless through POST /v1/messages: install, base URL and key, streaming and tool use.


The gateway serves the Anthropic Messages API at `POST /v1/messages`. The Anthropic SDKs work against it with a new base URL and a Boundless key.

## Install

```bash
pip install anthropic
npm install @anthropic-ai/sdk
```

## Base URL and key

The Anthropic SDKs add `/v1/messages` themselves, so give them the gateway root: `https://api.inference.boundless.network`, with no `/v1`. The gateway takes the key as `x-api-key`, which is what these SDKs send, or as `Authorization: Bearer`.

```bash
export BOUNDLESS_API_KEY="paste-your-key-here"
```

Use a model ID exactly as the [models table](https://inference.boundless.network/docs/models) lists it. Any model there works on Messages, not only the Claude models.

## Make a request

`max_tokens` is required and must cover thinking plus the reply. Give reasoning models 1024 or more.

```python
import os
from anthropic import Anthropic

client = Anthropic(
    api_key=os.environ["BOUNDLESS_API_KEY"],
    base_url="https://api.inference.boundless.network",
)

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

for block in message.content:
    if block.type == "text":
        print(block.text)
print(message.usage)
```

```typescript
import Anthropic from "@anthropic-ai/sdk"

const client = new Anthropic({
  apiKey: process.env.BOUNDLESS_API_KEY,
  baseURL: "https://api.inference.boundless.network",
})

const message = await client.messages.create({
  model: "deepseek/deepseek-v4.1-flash-1017",
  max_tokens: 1024,
  messages: [{ role: "user", content: "Say hello" }],
})

for (const block of message.content) {
  if (block.type === "text") console.log(block.text)
}
console.log(message.usage)
```

## Stream the reply

The gateway sends the standard Anthropic server-sent events.

```python
with client.messages.stream(
    model="deepseek/deepseek-v4.1-flash-1017",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Say hello"}],
) as stream:
    for text in stream.text_stream:
        print(text, end="", flush=True)
    print(f"\n{stream.get_final_message().usage}")
```

```typescript
const stream = client.messages.stream({
  model: "deepseek/deepseek-v4.1-flash-1017",
  max_tokens: 1024,
  messages: [{ role: "user", content: "Say hello" }],
})

stream.on("text", (text) => process.stdout.write(text))
const final = await stream.finalMessage()
console.log("\n", final.usage)
```

## Call a tool

The model answers with a `tool_use` block. You run the function and send a `tool_result` block back.

```python
message = client.messages.create(
    model="deepseek/deepseek-v4.1-flash",
    max_tokens=1024,
    tools=[{
        "name": "add",
        "description": "Add two integers.",
        "input_schema": {
            "type": "object",
            "properties": {"a": {"type": "integer"}, "b": {"type": "integer"}},
            "required": ["a", "b"],
        },
    }],
    tool_choice={"type": "tool", "name": "add"},
    messages=[{"role": "user", "content": "What is 20 + 22? Use the add tool."}],
)

call = next(block for block in message.content if block.type == "tool_use")
print(call.name, call.input)
```

[Tool calling](https://inference.boundless.network/docs/tool-calling) covers the other wire formats.

## Known differences

- Responses are Anthropic-shaped: `content` blocks, `stop_reason`, `tool_use`, and `usage` with `input_tokens` and `output_tokens`.
- Errors use the Anthropic shape, `{"type": "error", "error": {"type", "message"}}`, with no `code` and no `param`. Match on `error.type`. See [Errors](https://inference.boundless.network/docs/errors).
- `POST /v1/messages/count_tokens` counts input tokens without running the request.
- Credits that run out return `402`. The error `type` is `billing_error`, and the SDK does not retry it.
- Anthropic-hosted features are not served. See [API Compatibility](https://inference.boundless.network/docs/api-compatibility#anthropic-messages-api).
- After a `502`, a timeout or a dropped stream, check `GET /v1/requests/{id}` before you resend. See [Retrying safely](https://inference.boundless.network/docs/errors#retrying-safely).
