Skip to main content
Prerequisite: You need an AI Gateway endpoint before continuing. Create one using the dashboard quickstart or follow the manual setup guide.
The AI Gateway is compatible with Anthropic’s official SDKs. Change the base URL to route requests through your gateway. In passthrough mode, the gateway forwards Anthropic SDK requests unchanged, so new Anthropic features are immediately available without gateway updates.
Anthropic models also work through OpenAI-compatible SDKs (OpenAI SDK, Vercel AI SDK, LangChain, etc.) — just use the model name directly, for example model: "claude-opus-4-6". You don’t need the Anthropic SDK to access Anthropic models.

Installation

pip install anthropic

Basic usage

Point the SDK at your AI Gateway endpoint:
import anthropic

client = anthropic.Anthropic(
    base_url="https://your-ai-gateway.ngrok.app",
    api_key="ng-xxxxx-g1-xxxxx"  # Your AI Gateway API Key
)

message = client.messages.create(
    model="claude-opus-4-6",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Hello!"}]
)

print(message.content)
The Anthropic SDK sends the API key via the x-api-key header. AI Gateway API Keys work with both x-api-key and Authorization: Bearer headers.

Streaming

The AI Gateway supports streaming responses with the Anthropic SDK:
import anthropic

client = anthropic.Anthropic(
    base_url="https://your-ai-gateway.ngrok.app",
    api_key="ng-xxxxx-g1-xxxxx"  # Your AI Gateway API Key
)

with client.messages.stream(
    model="claude-opus-4-6",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Write a haiku about APIs"}]
) as stream:
    for text in stream.text_stream:
        print(text, end="", flush=True)

Automatic model selection

Let the gateway choose the best model:
import anthropic

client = anthropic.Anthropic(
    base_url="https://your-ai-gateway.ngrok.app",
    api_key="ng-xxxxx-g1-xxxxx"  # Your AI Gateway API Key
)

message = client.messages.create(
    model="ngrok/auto",  # Gateway selects based on your strategy
    max_tokens=1024,
    messages=[{"role": "user", "content": "Hello!"}]
)

print(message.content)
When using Anthropic SDKs with the gateway, model selection is limited to providers that support the Anthropic Claude API. Currently in the ngrok model catalog Anthropic is the only provider that supports this format.

Tool use

Tool calling works as documented by Anthropic:
import anthropic

client = anthropic.Anthropic(
    base_url="https://your-ai-gateway.ngrok.app",
    api_key="ng-xxxxx-g1-xxxxx"  # Your AI Gateway API Key
)

message = client.messages.create(
    model="claude-opus-4-6",
    max_tokens=1024,
    messages=[{"role": "user", "content": "What's the weather in Paris?"}],
    tools=[{
        "name": "get_weather",
        "description": "Get weather for a location",
        "input_schema": {
            "type": "object",
            "properties": {
                "location": {"type": "string"}
            },
            "required": ["location"]
        }
    }]
)

print(message)

Error handling

The gateway handles many errors automatically through failover. For errors that reach your app:
import anthropic

client = anthropic.Anthropic(
    base_url="https://your-ai-gateway.ngrok.app",
    api_key="ng-xxxxx-g1-xxxxx"  # Your AI Gateway API Key
)

try:
    message = client.messages.create(
        model="claude-opus-4-6",
        max_tokens=1024,
        messages=[{"role": "user", "content": "Hello!"}]
    )
except anthropic.RateLimitError as e:
    print("Rate limited across all providers")
except anthropic.APIError as e:
    print(f"API error: {e}")

Supported endpoints

The AI Gateway supports these Anthropic Claude API endpoints:
EndpointDescription
/v1/messagesMessages

Next steps