Anthropic SDK · Python + TS
Anthropic SDK + Leanroute
Leanroute exposes an Anthropic-shaped endpoint at /anthropic/v1/messages (parallel to the OpenAI-shaped /v1/chat/completions). Point the official Anthropic SDK's base_url at https://api.leanroute.dev/anthropic and every SDK call routes through us without a wire-format translation. Same messages array, same tool_use / tool_result shapes, same content blocks.
The differentiator: native mcp_servers passthrough.
Anthropic's Messages API accepts an mcp_servers field that lists MCP tool servers Claude can call during a completion. Leanroute is the only gateway that forwards this field end-to-end, so your app keeps its MCP integration when you switch from direct-Anthropic to gateway-routed traffic.
Trust stays with you: configure your org's allowlist at /dashboard/mcp. Any MCP URL not on the allowlist returns 403 mcp_server_not_allowlisted before the upstream request fires.
1. Get a gateway key
From /dashboard/keys create a key labeled "anthropic-sdk". Copy the gw_live_* value.
2. Python: chat
pip install anthropicfrom anthropic import Anthropic
client = Anthropic(
api_key="gw_live_YOUR_KEY",
base_url="https://api.leanroute.dev/anthropic",
)
msg = client.messages.create(
model="claude-sonnet-4-6",
max_tokens=200,
messages=[{"role": "user", "content": "Hello in one sentence."}],
)
print(msg.content[0].text)Note the model name is claude-sonnet-4-6 (Anthropic's native form), NOT the canonical anthropic/claude-sonnet-4-6 form we use on the OpenAI-shaped endpoint. That's deliberate — the Anthropic wire is passed through unchanged, so upstream model strings apply.
3. Python: streaming
with client.messages.stream(
model="claude-sonnet-4-6",
max_tokens=200,
messages=[{"role": "user", "content": "Write a haiku about caching."}],
) as stream:
for text in stream.text_stream:
print(text, end="", flush=True)4. Python: MCP passthrough
Use extra_body on the Python SDK to include mcp_servers. The SDK doesn't model that field natively yet, but it forwards unknown top-level fields transparently.
# MCP passthrough — the differentiator. mcp_servers gets forwarded
# to Anthropic and Claude will call the tool servers directly.
#
# Only per-org-allowlisted MCP URLs will pass. Configure allowlist at
# https://leanroute.dev/dashboard/mcp — unlisted URLs return 403
# mcp_server_not_allowlisted before the upstream call.
msg = client.messages.create(
model="claude-sonnet-4-6",
max_tokens=1024,
messages=[{"role": "user", "content": "What are the top 3 issues on the deepwiki repo?"}],
extra_body={
"mcp_servers": [
{
"type": "url",
"url": "https://mcp.deepwiki.com/mcp",
"name": "deepwiki",
}
],
},
)
print(msg.content[0].text)Response header x-gateway-mcp-allowlist reports whether the allowlist passed or blocked. Detailed guide at /docs/mcp.
5. TypeScript
npm install @anthropic-ai/sdkimport Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic({
apiKey: "gw_live_YOUR_KEY",
baseURL: "https://api.leanroute.dev/anthropic",
});
const msg = await client.messages.create({
model: "claude-sonnet-4-6",
max_tokens: 200,
messages: [{ role: "user", content: "Hello in one sentence." }],
});
console.log((msg.content[0] as { text: string }).text);For MCP passthrough in TS use the SDK's additionalRequestOptions or pass extra fields directly — the SDK forwards unknown top-level fields.
Why route Anthropic through us at all?
Fair question — if you're only calling Claude, direct is fine. Reasons to route via Leanroute:
- Cross-provider failover: when Anthropic 529-overloads (which they do), Leanroute retries on the cheapest same-tier alternative (e.g. Sonnet 4.6 → GPT-4o / DeepSeek v4-flash) transparently.
- Per-org MCP allowlist: control which MCP servers your team can invoke centrally, without pushing config to every dev.
- Unified billing: one Stripe subscription for all your model providers instead of a separate one per vendor.
- Guardrails library: the same 7 curated rules (PII, secrets, prompt injection, moderation, data exfil, profanity, language) apply to Anthropic traffic without a separate integration.
Troubleshooting
- 401 unauthenticated: use your
gw_live_*Leanroute key, NOT your Anthropicsk-ant-*key. - 403 mcp_server_not_allowlisted: the MCP URL is not on your org's allowlist. Add it at /dashboard/mcp. The response header lists which URL failed the check.
- 404 or wrong base URL: the Anthropic endpoint lives at
/anthropic(no/v1in the base URL — the SDK appends/v1/messagesitself). - Model name rejected: use the Anthropic-native form (
claude-sonnet-4-6,claude-opus-5), not theanthropic/*canonical form.