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.
Install
Section titled “Install”pip install anthropicnpm install @anthropic-ai/sdkPoint the client at the gateway
Section titled “Point the client at the gateway”Give the SDK the gateway’s address without /v1: it adds the path itself.
import osfrom 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)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.
Or set it in the environment
Section titled “Or set it in the environment”The SDK reads its base URL and key from two variables. Tools built on it, Claude Code among them, read the same ones.
export ANTHROPIC_BASE_URL="https://api.exoticrouter.com"export ANTHROPIC_API_KEY="sk-er-..."$env:ANTHROPIC_BASE_URL = "https://api.exoticrouter.com"$env:ANTHROPIC_API_KEY = "sk-er-..."The models
Section titled “The models”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.
What you can call
Section titled “What you can call”| 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-versionandanthropic-betaheaders the SDK sends are passed on. - Errors come in Anthropic’s shape, so the SDK raises its own exceptions:
AuthenticationErroron a 401,RateLimitErroron a 429, and so on.
Streaming
Section titled “Streaming”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)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();Where next
Section titled “Where next”- Messages: The endpoint this SDK calls.
- Claude Code: Set up the agent built on this API.