Skip to main content

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).

from anthropic import Anthropic

client = Anthropic(
base_url="http://localhost:8080/anthropic",
api_key="sk-gw-your-virtual-key",
)

[!NOTE] Avoid setting both api_key (x-api-key) and auth_token (Authorization: Bearer) to different values. GoRoute rejects conflicting credential headers with 400 ambiguous_gateway_credentials.


2. Messages (POST /anthropic/v1/messages)​

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)

3. Streaming Messages (stream: true)​

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)

4. Token Counting (POST /anthropic/v1/messages/count_tokens)​

GoRoute computes pre-flight token counts locally without forwarding to the upstream provider:

count = client.messages.count_tokens(
model="claude-sonnet-4-20250514",
messages=[{"role": "user", "content": "Estimate tokens for this prompt."}],
)
print(count.input_tokens)

See Anthropic Messages Ingress API for details on extended thinking blocks, tool schema translation, and alternating turn consolidation.