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. Simply change the base URL configuration option for the Anthropic SDK to route all requests through your gateway, get automatic failover, key rotation, and observability. The gateway will forward requests for Anthropic SDKs unchanged in passthrough mode, enabling you to use any new Anthropic features immediately.
This page is about Anthropic’s SDKs, not the provider Anthropic. To access models provided by Anthropic, you can use either OpenAI or Anthropic SDKs.

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="your-api-key"
)

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

print(message.content)

Streaming

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

client = anthropic.Anthropic(
    base_url="https://your-ai-gateway.ngrok.app",
    api_key="your-api-key"
)

with client.messages.stream(
    model="claude-opus-4-5",
    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="your-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="your-api-key"
)

message = client.messages.create(
    model="claude-opus-4-5",
    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="your-api-key"
)

try:
    message = client.messages.create(
        model="claude-opus-4-5",
        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