Skip to content
exoticrouter.com ↗

Anthropic SDK

Anthropic’s SDK works with the gateway once its base URL is changed. It speaks the Messages format, which the Claude models answer.

Python
pip install anthropic
TypeScript
npm install @anthropic-ai/sdk

Give the SDK the gateway’s address without /v1: it adds the path itself.

Python
import os
from anthropic import Anthropic
client = Anthropic(
base_url="https://api.exoticrouter.com",
api_key=os.environ["EXOTIC_API_KEY"],
)
message = client.messages.create(
model="anthropic/claude-sonnet-5-5",
max_tokens=400,
messages=[{"role": "user", "content": "Say hello in five words."}],
)
print(message.content[0].text)
TypeScript
import Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic({
baseURL: "https://api.exoticrouter.com",
apiKey: process.env.EXOTIC_API_KEY,
});
const message = await client.messages.create({
model: "anthropic/claude-sonnet-5-5",
max_tokens: 400,
messages: [{ role: "user", content: "Say hello in five words." }],
});
console.log(message.content[0].type === "text" ? message.content[0].text : "");

AsyncAnthropic takes the same two arguments.

The SDK reads its base URL and key from two variables. Tools built on it, Claude Code among them, read the same ones.

Shell
export ANTHROPIC_BASE_URL="https://api.exoticrouter.com"
export ANTHROPIC_API_KEY="sk-er-..."
PowerShell
$env:ANTHROPIC_BASE_URL = "https://api.exoticrouter.com"
$env:ANTHROPIC_API_KEY = "sk-er-..."

Every text model answers this API. Name a Claude model by its full id, anthropic/claude-sonnet-5-5, or by the name Anthropic’s own API uses, claude-sonnet-5-5: both reach the same model, and the answer names it by its full id.

Any other model, openai/gpt-6.2-astra or a Gemini or DeepSeek model, answers here too: the gateway translates the request to Chat Completions and the answer back, so the SDK reads it as it reads Claude’s. For the options of a model’s own format, the OpenAI SDK reaches every text model through Chat Completions.

SDK call Served
client.messages.create Yes
client.messages.stream Yes
client.messages.count_tokens Yes, an estimate counted by the gateway, free
client.messages.batches No
client.models.list Yes, the models served on /v1/messages
  • The body of a request reaches the model as the SDK builds it: system prompts, tools, thinking and the rest of the Messages format.
  • The anthropic-version and anthropic-beta headers the SDK sends are passed on.
  • Errors come in Anthropic’s shape, so the SDK raises its own exceptions: AuthenticationError on a 401, RateLimitError on a 429, and so on.
Python
with client.messages.stream(
model="anthropic/claude-sonnet-5-5",
max_tokens=400,
messages=[{"role": "user", "content": "Write a haiku about glass."}],
) as stream:
for text in stream.text_stream:
print(text, end="", flush=True)
TypeScript
const stream = client.messages.stream({
model: "anthropic/claude-sonnet-5-5",
max_tokens: 400,
messages: [{ role: "user", content: "Write a haiku about glass." }],
});
stream.on("text", (text) => process.stdout.write(text));
await stream.finalMessage();