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

# Anthropic SDK

> Use the ngrok AI Gateway with Anthropic's official SDKs.

<Note>
  **Prerequisite**: Complete the [Quickstart](/ai-gateway/quickstart) to get an [access key](/ai-gateway/concepts/access-keys). All requests go to `https://gateway.ngrok.ai`.
</Note>

The AI Gateway is compatible with Anthropic's official SDKs. Set the base URL to `https://gateway.ngrok.ai` and authenticate with your [access key](/ai-gateway/concepts/access-keys).

<Note>
  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.
</Note>

## Installation

<CodeGroup>
  ```bash Python theme={null}
  pip install anthropic
  ```

  ```bash TypeScript theme={null}
  npm install @anthropic-ai/sdk
  ```
</CodeGroup>

## Basic usage

Point the SDK at your AI Gateway endpoint:

<CodeGroup>
  ```python Python highlight={4} theme={null}
  import anthropic

  client = anthropic.Anthropic(
      base_url="https://gateway.ngrok.ai",
      api_key="ng-xxxxx-g1-xxxxx"  # Your access key
  )

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

  print(message.content)
  ```

  ```typescript TypeScript highlight={4} theme={null}
  import Anthropic from "@anthropic-ai/sdk";

  const client = new Anthropic({
    baseURL: "https://gateway.ngrok.ai",
    apiKey: "ng-xxxxx-g1-xxxxx",  // Your access key
  });

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

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

<Prompt description="Try using this prompt on your agent to verify your Anthropic SDK connection: 'State your exact model name and provider.'" actions={["copy", "cursor"]}>
  State your exact model name and provider.
</Prompt>

<Note>
  The Anthropic SDK sends the API key via the `x-api-key` header. Access keys work with both `x-api-key` and `Authorization: Bearer` headers.
</Note>

## Streaming

The AI Gateway supports streaming responses with the Anthropic SDK:

<CodeGroup>
  ```python Python highlight={8} theme={null}
  import anthropic

  client = anthropic.Anthropic(
      base_url="https://gateway.ngrok.ai",
      api_key="ng-xxxxx-g1-xxxxx"  # Your access 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)
  ```

  ```typescript TypeScript highlight={12} theme={null}
  import Anthropic from "@anthropic-ai/sdk";

  const client = new Anthropic({
    baseURL: "https://gateway.ngrok.ai",
    apiKey: "ng-xxxxx-g1-xxxxx",  // Your access key
  });

  const stream = await client.messages.create({
    max_tokens: 1024,
    messages: [{ role: 'user', content: 'Hello, Claude' }],
    model: 'claude-opus-4-6',
    stream: true,
  });
  for await (const messageStreamEvent of stream) {
    console.log(messageStreamEvent.type);
  }
  ```
</CodeGroup>

<Prompt description="Try using this prompt on your agent to test streaming — tokens should appear one by one: 'Write a haiku about APIs'" actions={["copy", "cursor"]}>
  Write a haiku about APIs
</Prompt>

## Model failover

Specify fallback models in the request body. See [Configure fallback models](/ai-gateway/guides/configure-fallback-models).

<Warning>
  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.
</Warning>

## Tool use

Tool calling works as documented by Anthropic:

<CodeGroup>
  ```python Python highlight={12-22} theme={null}
  import anthropic

  client = anthropic.Anthropic(
      base_url="https://gateway.ngrok.ai",
      api_key="ng-xxxxx-g1-xxxxx"  # Your access 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)
  ```

  ```typescript TypeScript highlight={12-22} theme={null}
  import Anthropic from "@anthropic-ai/sdk";

  const client = new Anthropic({
    baseURL: "https://gateway.ngrok.ai",
    apiKey: "ng-xxxxx-g1-xxxxx",  // Your access key
  });

  const message = await 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"],
        },
      },
    ],
  });

  console.log(message);
  ```
</CodeGroup>

## Error handling

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

<CodeGroup>
  ```python Python theme={null}
  import anthropic

  client = anthropic.Anthropic(
      base_url="https://gateway.ngrok.ai",
      api_key="ng-xxxxx-g1-xxxxx"  # Your access 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}")
  ```

  ```typescript TypeScript theme={null}
  import Anthropic from "@anthropic-ai/sdk";

  const client = new Anthropic({
    baseURL: "https://gateway.ngrok.ai",
    apiKey: "ng-xxxxx-g1-xxxxx",  // Your access key
  });

  try {
    const message = await client.messages.create({
      model: "claude-opus-4-6",
      max_tokens: 1024,
      messages: [{ role: "user", content: "Hello!" }],
    });
  } catch (error) {
    if (error instanceof Anthropic.RateLimitError) {
      console.log("Rate limited across all providers");
    } else if (error instanceof Anthropic.APIError) {
      console.log(`API error: ${error.message}`);
    }
  }
  ```
</CodeGroup>

## Supported endpoints

The AI Gateway supports these Anthropic Claude API endpoints:

| Endpoint       | Description |
| -------------- | ----------- |
| `/v1/messages` | Messages    |

## Next steps

* [Choose a model](/ai-gateway/guides/model-selection-strategies): Model IDs and provider prefixes
* [Configure fallback models](/ai-gateway/guides/configure-fallback-models): Try another model when the first one fails
* [Choose how to reach providers](/ai-gateway/guides/configuring-providers): Credits, provider keys, and self-run models
