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

# Anthropic SDK

> Use Anthropic SDK through Keywords AI

<Note> This integration is for the **Keywords AI gateway**. </Note>

<Warning>
  This is only a pass-through integration. So with this integration, some features are not available.
</Warning>

<Warning>
  **Default max\_tokens**: Anthropic requests sent through Keywords AI have a default of 4096 max\_tokens. Make sure to explicitly set `max_tokens` in your requests if you need a different value.
</Warning>

## Pros and Cons

### Pros

* **Easy Setup**: Only 2 lines of code to integrate.
* **Use Claude's `Thinking` Feature**: You can use the `Thinking` feature from `Claude 3.7 Sonnet` or Claude 4 models.

### Cons

* **No Direct Prompt Access**: You can't fetch prompts stored in Keywords AI directly. You'll need to use the [Prompts API](/api-endpoints/develop/prompts/create-prompts) instead.
* **Limited Gateway Features**: You can't use features like [user rate limits](/documentation/products/users/user_rate_limit), [fallbacks](/documentation/products/gateway/traffic_management/fallbacks) or [load balancing](/documentation/products/gateway/traffic_management/load-balancing).

## Integration examples

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

  client = anthropic.Anthropic(
      base_url="https://api.keywordsai.co/api/anthropic/",
      api_key="Your_Keywords_AI_API_Key",
  )

  message = client.messages.create(
      model="claude-3-opus-20240229",
      max_tokens=1000,
      system="Respond only in Yoda-speak.",
      messages=[
          {"role": "user", "content": "How are you today?"}
      ],
      metadata={
          "keywordsai_params": {
              "customer_identifier": "something" # You need to wrap the customer_identifier into the "keywordsai_params" key
          }
      },
  )

  print(message.content)
  ```

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

  const anthropic = new Anthropic({
    baseUrl: "https://api.keywordsai.co/api/anthropic/",  
    apiKey: 'YOUR_KEYWORDS_AI_API_KEY',
  });

  const msg = await anthropic.messages.create({
  model: "claude-3-5-sonnet-20240620",
  max_tokens: 1000,
  temperature: 0,
  system: "Respond only with short poems.",
  messages: [
      {
      "role": "user",
      "content": [
          {
          "type": "text",
          "text": "Why is the ocean salty?"
          }
      ]
      }
  ],
  metadata: {
      "keywordsai_params": {
          "customer_identifier": "something"
      }
  }
  });
  console.log(msg);
  ```
</CodeGroup>

## Extra Headers

You can pass additional headers to be sent with your LLM requests using the `extra_headers` parameter. This is useful for sending custom headers required by specific models or configurations.

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

  client = anthropic.Anthropic(
      base_url="https://api.keywordsai.co/api/anthropic/",
      api_key="Your_Keywords_AI_API_Key",
  )

  message = client.messages.create(
      model="claude-sonnet-4-20250514",
      max_tokens=1000,
      system="Respond only in Yoda-speak.",
      messages=[
          {"role": "user", "content": "Are there an infinite number of prime numbers such that n mod 4 == 3?"}
      ],
      metadata={
          "extra_headers": {
              "anthropic-beta": "context-1m-2025-08-07"
          }
      },
  )

  print(message.content)
  ```

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

  const anthropic = new Anthropic({
    baseUrl: "https://api.keywordsai.co/api/anthropic/",  
    apiKey: 'YOUR_KEYWORDS_AI_API_KEY',
  });

  const msg = await anthropic.messages.create({
    model: "claude-sonnet-4-20250514",
    max_tokens: 1000,
    temperature: 0,
    system: "Respond only with short poems.",
    messages: [
      {
        "role": "user",
        "content": "Are there an infinite number of prime numbers such that n mod 4 == 3?"
      }
    ],
    metadata: {
      "extra_headers": {
        "anthropic-beta": "context-1m-2025-08-07"
      }
    }
  });
  console.log(msg);
  ```
</CodeGroup>

<Note>
  **Extra Headers**: The `extra_headers` parameter allows you to pass additional headers that will be sent with your LLM request. This is particularly useful for accessing beta features like Claude Sonnet 4's 1M token context window using the `anthropic-beta: context-1m-2025-08-07` header.
</Note>

## Keywords AI parameters

To use [Keywords AI parameters](/api-endpoints/develop/gateway/chat-completions#keywords-ai-parameters), you can pass them in the `metadata` parameter. In the above example, `customer_identifier` is a Keywords AI parameter. These parameters will take precedence over the Anthropic parameters if they are conflicting.

## Nested Metadata for KeywordsAI Parameters

You can pass multiple KeywordsAI-specific parameters using a nested metadata structure. This allows you to include `custom_identifier`, `customer_identifier`, and additional custom metadata all in one structure:

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

  client = anthropic.Anthropic(
      base_url="https://api.keywordsai.co/api/anthropic/",
      api_key="Your_Keywords_AI_API_Key",
  )

  message = client.messages.create(
      model="claude-3-opus-20240229",
      max_tokens=1000,
      system="Respond only in Yoda-speak.",
      messages=[
          {"role": "user", "content": "How are you today?"}
      ],
      metadata={
          "keywordsai_params": {
              "custom_identifier": "session_id",
              "customer_identifier": "customer_id",
              "metadata": {
                  "agent": "orchestrator"
              }
          }
      },
  )

  print(message.content)
  ```

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

  const anthropic = new Anthropic({
    baseUrl: "https://api.keywordsai.co/api/anthropic/",  
    apiKey: 'YOUR_KEYWORDS_AI_API_KEY',
  });

  const msg = await anthropic.messages.create({
    model: "claude-3-5-sonnet-20240620",
    max_tokens: 1000,
    temperature: 0,
    system: "Respond only with short poems.",
    messages: [
      {
        "role": "user",
        "content": [
          {
            "type": "text",
            "text": "Why is the ocean salty?"
          }
        ]
      }
    ],
    metadata: {
      "keywordsai_params": {
        "custom_identifier": "session_id",
        "customer_identifier": "customer_id",
        "metadata": {
          "agent": "orchestrator"
        }
      }
    }
  });
  console.log(msg);
  ```
</CodeGroup>

<Note>
  **Nested Metadata Structure**: The `metadata` object should contain a `keywordsai_params` key, which itself contains:

  * `custom_identifier`: A custom identifier for tracking specific requests
  * `customer_identifier`: An identifier for the customer making the request
  * `metadata`: An additional nested object for custom metadata fields (e.g., agent type, workflow stage, etc.)
</Note>
