> ## Documentation Index
> Fetch the complete documentation index at: https://www.edgee.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Anthropic SDK

> Use Edgee with the Anthropic SDK for building AI applications with Claude models.

The Anthropic SDK provides official Python and TypeScript clients for interacting with Claude models. Edgee's Anthropic-compatible API
works with the Anthropic SDK, allowing you to leverage the SDK's features while gaining access to
Edgee's unified gateway, automatic failover, and full observability.

## Installation

<Tabs>
  <Tab title="Python">
    ```bash theme={"dark"}
    pip install anthropic
    ```
  </Tab>

  <Tab title="TypeScript">
    ```bash theme={"dark"}
    npm install @anthropic-ai/sdk
    ```
  </Tab>
</Tabs>

## Basic Usage

<Tabs>
  <Tab title="Python">
    ```python theme={"dark"}
    import os
    from anthropic import Anthropic

    # Initialize client with Edgee endpoint
    client = Anthropic(
        base_url="https://edgee.io",
        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)

    # Access token usage
    print(f"Input tokens: {message.usage.input_tokens}")
    print(f"Output tokens: {message.usage.output_tokens}")
    ```
  </Tab>

  <Tab title="TypeScript">
    ```typescript theme={"dark"}
    import Anthropic from '@anthropic-ai/sdk';

    // Initialize client with Edgee endpoint
    const client = new Anthropic({
      baseURL: 'https://edgee.io',
      apiKey: process.env.EDGEE_API_KEY,
    });

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

    console.log(message.content);

    // Access token usage
    console.log(`Input tokens: ${message.usage.input_tokens}`);
    console.log(`Output tokens: ${message.usage.output_tokens}`);
    ```
  </Tab>
</Tabs>

## Streaming Responses

Stream responses for real-time token delivery:

<Tabs>
  <Tab title="Python">
    ```python theme={"dark"}
    from anthropic import Anthropic

    client = Anthropic(
        base_url="https://edgee.io",
        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)
    ```
  </Tab>

  <Tab title="TypeScript">
    ```typescript theme={"dark"}
    import Anthropic from '@anthropic-ai/sdk';

    const client = new Anthropic({
      baseURL: 'https://edgee.io',
      apiKey: process.env.EDGEE_API_KEY,
    });

    // Stream messages
    const stream = await client.messages.create({
      model: 'claude-sonnet-4.5',
      max_tokens: 1024,
      messages: [
        { role: 'user', content: 'Write a short poem about coding' }
      ],
      stream: true,
    });

    for await (const event of stream) {
      if (event.type === 'content_block_delta'
          && event.delta.type === 'text_delta') {
        process.stdout.write(event.delta.text);
      }
    }
    ```
  </Tab>
</Tabs>

## Token Usage Tracking

Access standard Anthropic token usage metrics in every response:

<Tabs>
  <Tab title="Python">
    ```python theme={"dark"}
    from anthropic import Anthropic

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

    message = client.messages.create(
        model="claude-sonnet-4.5",
        max_tokens=1024,
        messages=[{"role": "user", "content": "Analyze this long document..."}]
    )

    print(message.content)
    print(f"Input tokens: {message.usage.input_tokens}")
    print(f"Output tokens: {message.usage.output_tokens}")
    ```
  </Tab>

  <Tab title="TypeScript">
    ```typescript theme={"dark"}
    import Anthropic from '@anthropic-ai/sdk';

    const client = new Anthropic({
      baseURL: 'https://edgee.io',
      apiKey: process.env.EDGEE_API_KEY,
    });

    const message = await client.messages.create({
      model: 'claude-sonnet-4.5',
      max_tokens: 1024,
      messages: [{ role: 'user', content: 'Analyze this long document...' }]
    });

    console.log(message.content);
    console.log(`Input tokens: ${message.usage.input_tokens}`);
    console.log(`Output tokens: ${message.usage.output_tokens}`);
    ```
  </Tab>
</Tabs>

<Note>
  When compression is enabled, `input_tokens` reflects the compressed token count. View detailed compression metrics in the [Edgee dashboard](/docs/features/observability).
</Note>

## Compression & Tags via Headers

When using the Anthropic SDK with Edgee, you can control token compression and add tags using HTTP headers:

### Enabling Compression

<Note>
  The headers below control **Token Compression**.
</Note>

<Tabs>
  <Tab title="Python">
    ```python theme={"dark"}
    from anthropic import Anthropic

    client = Anthropic(
        base_url="https://edgee.io",
        api_key=os.environ.get("EDGEE_API_KEY"),
        default_headers={
            "x-edgee-compression-model": "claude",
        }
    )

    # All requests will use Claude Token Compression strategy
    message = client.messages.create(
        model="claude-sonnet-4.5",
        max_tokens=1024,
        messages=[{"role": "user", "content": "Analyze this document..."}]
    )
    ```
  </Tab>

  <Tab title="TypeScript">
    ```typescript theme={"dark"}
    import Anthropic from '@anthropic-ai/sdk';

    const client = new Anthropic({
      baseURL: 'https://edgee.io',
      apiKey: process.env.EDGEE_API_KEY,
      defaultHeaders: {
        'x-edgee-compression-model': 'claude',
      }
    });

    // All requests will use Claude Token Compression strategy
    const message = await client.messages.create({
      model: 'claude-sonnet-4.5',
      max_tokens: 1024,
      messages: [{ role: 'user', content: 'Analyze this document...' }]
    });
    ```
  </Tab>
</Tabs>

### Adding Tags for Analytics

Combine compression with tags to track requests in your dashboard:

<Tabs>
  <Tab title="Python">
    ```python theme={"dark"}
    from anthropic import Anthropic

    client = Anthropic(
        base_url="https://edgee.io",
        api_key=os.environ.get("EDGEE_API_KEY"),
        default_headers={
            "x-edgee-tags": "production,anthropic-sdk,user-123"
        }
    )
    ```
  </Tab>

  <Tab title="TypeScript">
    ```typescript theme={"dark"}
    import Anthropic from '@anthropic-ai/sdk';

    const client = new Anthropic({
      baseURL: 'https://edgee.io',
      apiKey: process.env.EDGEE_API_KEY,
      defaultHeaders: {
        'x-edgee-tags': 'production,anthropic-sdk,user-123'
      }
    });
    ```
  </Tab>
</Tabs>

**Available Headers:**

| Header                      | Type                                            | Description                                                              |
| --------------------------- | ----------------------------------------------- | ------------------------------------------------------------------------ |
| `x-edgee-compression-model` | `"claude"`, `"opencode"`, `"cursor"`, `"codex"` | Compression bundle to apply (e.g. "claude" for Claude Token Compression) |
| `x-edgee-tags`              | `string`                                        | Comma-separated tags for analytics and filtering                         |

<Tip>
  You can also enable compression per API key or Agent in the Edgee console. Headers override console settings for specific requests.
</Tip>

## Multi-Provider Access

With Edgee, you can access models from multiple providers using the same Anthropic SDK client and compare costs across providers:

<Tabs>
  <Tab title="Python">
    ```python theme={"dark"}
    from anthropic import Anthropic

    client = Anthropic(
        base_url="https://edgee.io",
        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-5.2",
        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!"}]
    )
    ```
  </Tab>

  <Tab title="TypeScript">
    ```typescript theme={"dark"}
    import Anthropic from '@anthropic-ai/sdk';

    const client = new Anthropic({
      baseURL: 'https://edgee.io/',
      apiKey: process.env.EDGEE_API_KEY,
    });

    // Use Claude
    const claudeResponse = await client.messages.create({
      model: 'claude-sonnet-4.5',
      max_tokens: 1024,
      messages: [{ role: 'user', content: 'Hello!' }]
    });

    // Use GPT-4 through the same client
    const gptResponse = await client.messages.create({
      model: 'gpt-5.2',
      max_tokens: 1024,
      messages: [{ role: 'user', content: 'Hello!' }]
    });

    // Use Mistral
    const mistralResponse = await client.messages.create({
      model: 'mistral-large',
      max_tokens: 1024,
      messages: [{ role: 'user', content: 'Hello!' }]
    });
    ```
  </Tab>
</Tabs>

## Function Calling (Tools)

Use Claude's tool calling with Edgee:

<Tabs>
  <Tab title="Python">
    ```python theme={"dark"}
    from anthropic import Anthropic

    client = Anthropic(
        base_url="https://edgee.io",
        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)
    ```
  </Tab>

  <Tab title="TypeScript">
    ```typescript theme={"dark"}
    import Anthropic from '@anthropic-ai/sdk';

    const client = new Anthropic({
      baseURL: 'https://edgee.io',
      apiKey: process.env.EDGEE_API_KEY,
    });

    // Define a tool
    const 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
    const message = await client.messages.create({
      model: 'claude-sonnet-4.5',
      max_tokens: 1024,
      tools: tools,
      messages: [
        { role: 'user', content: "What's the weather like in Paris?" }
      ]
    });

    console.log(message.content);
    ```
  </Tab>
</Tabs>

## Error Handling and Retries

The Anthropic SDK includes built-in retry logic, which works with Edgee's automatic failover:

<Tabs>
  <Tab title="Python">
    ```python theme={"dark"}
    from anthropic import Anthropic, APIError

    client = Anthropic(
        base_url="https://edgee.io",
        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)
    except APIError as e:
        print(f"API Error: {e}")
    ```
  </Tab>

  <Tab title="TypeScript">
    ```typescript theme={"dark"}
    import Anthropic from '@anthropic-ai/sdk';

    const client = new Anthropic({
      baseURL: 'https://edgee.io',
      apiKey: process.env.EDGEE_API_KEY,
      maxRetries: 3,  // SDK will retry up to 3 times
    });

    try {
      const message = await client.messages.create({
        model: 'claude-sonnet-4.5',
        max_tokens: 1024,
        messages: [{ role: 'user', content: 'Hello!' }]
      });
      console.log(message.content);
    } catch (error) {
      console.error('API Error:', error);
    }
    ```
  </Tab>
</Tabs>

## Complete Example

Here's a complete application example:

<Tabs>
  <Tab title="Python">
    ```python theme={"dark"}
    #!/usr/bin/env python3
    import os
    from anthropic import Anthropic

    def main():
        # Initialize client
        client = Anthropic(
            base_url="https://edgee.io",
            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()
    ```
  </Tab>

  <Tab title="TypeScript">
    ```typescript theme={"dark"}
    import Anthropic from '@anthropic-ai/sdk';
    import * as readline from 'readline';

    async function main() {
      // Initialize client
      const client = new Anthropic({
        baseURL: 'https://edgee.io',
        apiKey: process.env.EDGEE_API_KEY,
        defaultHeaders: {
          'x-edgee-tags': 'production,chat-app'
        }
      });

      const rl = readline.createInterface({
        input: process.stdin,
        output: process.stdout
      });

      const conversation: Array<{ role: string; content: string }> = [];

      console.log("Chat with Claude (type 'quit' to exit)");

      const chat = () => {
        rl.question('\nYou: ', async (input) => {
          if (input.toLowerCase() === 'quit') {
            rl.close();
            return;
          }

          conversation.push({
            role: 'user',
            content: input
          });

          process.stdout.write('\nClaude: ');

          const stream = await client.messages.create({
            model: 'claude-sonnet-4.5',
            max_tokens: 1024,
            messages: conversation,
            stream: true,
          });

          let assistantMessage = '';
          for await (const event of stream) {
            if (event.type === 'content_block_delta'
                && event.delta.type === 'text_delta') {
              process.stdout.write(event.delta.text);
              assistantMessage += event.delta.text;
            }
          }

          conversation.push({
            role: 'assistant',
            content: assistantMessage
          });

          chat();
        });
      };

      chat();
    }

    main();
    ```
  </Tab>
</Tabs>

## Verify the connection

Send one request from the configured client, then open **Logs** in the same Edgee organization. Check the served model, provider, and key. For a CLI-launched coding agent, also inspect its session report.

If the request is missing, check that you launched through Edgee or saved the client’s gateway URL and Gateway API key. If it appears with an error, inspect the error before changing settings. See [Troubleshooting](/docs/troubleshooting) and [Gateway errors](/docs/llm-router/api-reference/errors).
