Anthropic SDKs (Python & TypeScript)
The official Anthropic SDKs (anthropic in Python and @anthropic-ai/sdk in TypeScript) append /v1/messages and /v1/messages/count_tokens to their configured base URL. Pointing base_url / baseURL to http://localhost:8080/anthropic routes requests directly to GoRoute's /anthropic/v1/messages and /anthropic/v1/messages/count_tokens endpoints.
1. Client Initialization
Pass your GoRoute Virtual Key (sk-gw-...) as api_key / apiKey (sent via x-api-key) or auth_token / authToken (sent via Authorization: Bearer).
- Python (anthropic)
- TypeScript (@anthropic-ai/sdk)
from anthropic import Anthropic
client = Anthropic(
base_url="http://localhost:8080/anthropic",
api_key="sk-gw-your-virtual-key",
)
import Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic({
baseURL: "http://localhost:8080/anthropic",
apiKey: "sk-gw-your-virtual-key",
});
[!NOTE] Avoid setting both
api_key(x-api-key) andauth_token(Authorization: Bearer) to different values. GoRoute rejects conflicting credential headers with400 ambiguous_gateway_credentials.
2. Messages (POST /anthropic/v1/messages)
- Python
- TypeScript
message = client.messages.create(
model="claude-sonnet-4-20250514",
max_tokens=256,
system="You are a concise systems engineer.",
messages=[
{"role": "user", "content": "What is a sliding-window rate limiter?"}
],
)
print(message.content[0].text)
const message = await client.messages.create({
model: "claude-sonnet-4-20250514",
max_tokens: 256,
system: "You are a concise systems engineer.",
messages: [
{ role: "user", content: "What is a sliding-window rate limiter?" },
],
});
console.log(message.content[0]);
3. Streaming Messages (stream: true)
- Python
- TypeScript
with client.messages.stream(
model="claude-sonnet-4-20250514",
max_tokens=256,
messages=[{"role": "user", "content": "List three Go concurrency primitives."}],
) as stream:
for text in stream.text_stream:
print(text, end="", flush=True)
const stream = client.messages.stream({
model: "claude-sonnet-4-20250514",
max_tokens: 256,
messages: [{ role: "user", content: "List three Go concurrency primitives." }],
});
stream.on("text", (text) => process.stdout.write(text));
await stream.finalMessage();
4. Token Counting (POST /anthropic/v1/messages/count_tokens)
GoRoute computes pre-flight token counts locally without forwarding to the upstream provider:
- Python
- TypeScript
count = client.messages.count_tokens(
model="claude-sonnet-4-20250514",
messages=[{"role": "user", "content": "Estimate tokens for this prompt."}],
)
print(count.input_tokens)
const count = await client.messages.countTokens({
model: "claude-sonnet-4-20250514",
messages: [{ role: "user", content: "Estimate tokens for this prompt." }],
});
console.log(count.input_tokens);
See Anthropic Messages Ingress API for details on extended thinking blocks, tool schema translation, and alternating turn consolidation.