Anthropic Python SDK with MagmaRouter.

The Anthropic Python SDK posts to /v1/messages under a configurable base URL. Point it at MagmaRouter and pass your key as the bearer auth token.

base_url and auth_token

The SDK default base URL is https://api.anthropic.com and it appends /v1/messages, so set base_url to https://api.magmarouter.com with no path. Pass the key as auth_token (sent as Authorization: Bearer), not api_key.

# pip install anthropic
from anthropic import Anthropic

client = Anthropic(
    base_url="https://api.magmarouter.com",
    auth_token="rl-...",
)

msg = client.messages.create(
    model="deepseek/deepseek-chat-v3.1",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Hello"}],
)
print(msg.content[0].text)
One endpoint gap messages.count_tokens calls /v1/messages/count_tokens, which MagmaRouter does not implement. Avoid that helper; ordinary messages.create works.

Related guides