Skip to main content
The Anthropic SDK provides official Python and TypeScript clients for interacting with Claude models. Edgee’s OpenAI-compatible API works seamlessly with the Anthropic SDK, allowing you to leverage the SDK’s features while gaining access to Edgee’s unified gateway, cost tracking, automatic failover, and observability.

Installation

pip install anthropic

Basic Usage

import os
from anthropic import Anthropic

# Initialize client with Edgee endpoint
client = Anthropic(
    base_url="https://api.edgee.ai/v1",
    api_key=os.environ.get("EDGEE_API_KEY"),
)

# Send a message
message = client.messages.create(
    model="claude-sonnet-4.5",
    max_tokens=1024,
    messages=[
        {"role": "user", "content": "What is the capital of France?"}
    ]
)

print(message.content[0].text)
# "The capital of France is Paris."

Streaming Responses

Stream responses for real-time token delivery:
from anthropic import Anthropic

client = Anthropic(
    base_url="https://api.edgee.ai/v1",
    api_key=os.environ.get("EDGEE_API_KEY"),
)

# Stream messages
with client.messages.stream(
    model="claude-sonnet-4.5",
    max_tokens=1024,
    messages=[
        {"role": "user", "content": "Write a short poem about coding"}
    ]
) as stream:
    for text in stream.text_stream:
        print(text, end="", flush=True)

Multi-Provider Access

With Edgee, you can access models from multiple providers using the same Anthropic SDK client:
from anthropic import Anthropic

client = Anthropic(
    base_url="https://api.edgee.ai/v1",
    api_key=os.environ.get("EDGEE_API_KEY"),
)

# Use Claude
claude_response = client.messages.create(
    model="claude-sonnet-4.5",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Hello!"}]
)

# Use GPT-4 through the same client
gpt_response = client.messages.create(
    model="gpt-4o",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Hello!"}]
)

# Use Mistral
mistral_response = client.messages.create(
    model="mistral-large",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Hello!"}]
)

Function Calling (Tools)

Use Claude’s tool calling with Edgee:
from anthropic import Anthropic

client = Anthropic(
    base_url="https://api.edgee.ai/v1",
    api_key=os.environ.get("EDGEE_API_KEY"),
)

# Define a tool
tools = [
    {
        "name": "get_weather",
        "description": "Get the current weather in a given location",
        "input_schema": {
            "type": "object",
            "properties": {
                "location": {
                    "type": "string",
                    "description": "The city and state, e.g. San Francisco, CA"
                }
            },
            "required": ["location"]
        }
    }
]

# Send message with tools
message = client.messages.create(
    model="claude-sonnet-4.5",
    max_tokens=1024,
    tools=tools,
    messages=[
        {"role": "user", "content": "What's the weather like in Paris?"}
    ]
)

print(message.content)

Tags for Observability

Add custom tags to track and filter requests in Edgee’s dashboard:
from anthropic import Anthropic

client = Anthropic(
    base_url="https://api.edgee.ai/v1",
    api_key=os.environ.get("EDGEE_API_KEY"),
    default_headers={
        "x-edgee-tags": "production,anthropic-sdk,user-123"
    }
)

# All requests from this client will include these tags
message = client.messages.create(
    model="claude-sonnet-4.5",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Hello!"}]
)
Tags are comma-separated strings that help you categorize and filter requests in Edgee’s analytics dashboard.

Error Handling and Retries

The Anthropic SDK includes built-in retry logic, which works seamlessly with Edgee’s automatic failover:
from anthropic import Anthropic, APIError

client = Anthropic(
    base_url="https://api.edgee.ai/v1",
    api_key=os.environ.get("EDGEE_API_KEY"),
    max_retries=3,  # SDK will retry up to 3 times
)

try:
    message = client.messages.create(
        model="claude-sonnet-4.5",
        max_tokens=1024,
        messages=[{"role": "user", "content": "Hello!"}]
    )
    print(message.content[0].text)
except APIError as e:
    print(f"API Error: {e}")

Authentication

Edgee uses standard Bearer token authentication. Set your API key as an environment variable:
export EDGEE_API_KEY="sk-edgee-..."
Or in your .env file:
EDGEE_API_KEY=sk-edgee-...
The SDK automatically formats the authentication header as:
Authorization: Bearer {api_key}

Benefits of Using Anthropic SDK with Edgee

Multi-Provider Access

Use the familiar Anthropic SDK to access Claude, GPT-4, Mistral, and 200+ other models through one interface.

Real-Time Cost Tracking

Every response includes detailed cost information. Track spending across all providers in one dashboard.

Automatic Failover

If Claude is rate-limited or unavailable, Edgee automatically routes to backup models without code changes.

Full Observability

Monitor latency, token usage, error rates, and costs for all requests in Edgee’s unified dashboard.

Complete Example

Here’s a complete application example:
#!/usr/bin/env python3
import os
from anthropic import Anthropic

def main():
    # Initialize client
    client = Anthropic(
        base_url="https://api.edgee.ai/v1",
        api_key=os.environ.get("EDGEE_API_KEY"),
        default_headers={
            "x-edgee-tags": "production,chat-app"
        }
    )

    # Chat loop
    conversation = []
    print("Chat with Claude (type 'quit' to exit)")

    while True:
        user_input = input("\nYou: ")
        if user_input.lower() == 'quit':
            break

        conversation.append({
            "role": "user",
            "content": user_input
        })

        # Stream response
        print("\nClaude: ", end="", flush=True)
        with client.messages.stream(
            model="claude-sonnet-4.5",
            max_tokens=1024,
            messages=conversation
        ) as stream:
            assistant_message = ""
            for text in stream.text_stream:
                print(text, end="", flush=True)
                assistant_message += text

        conversation.append({
            "role": "assistant",
            "content": assistant_message
        })

if __name__ == "__main__":
    main()

Next Steps