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

# Anthropic

> Auto-instrument Anthropic API calls

Automatically capture all Anthropic Messages API calls.

## Setup

<Tabs>
  <Tab title="Python">
    ```python theme={null}
    import traceroot
    from traceroot import Integration

    traceroot.initialize(integrations=[Integration.ANTHROPIC])
    ```
  </Tab>

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

    TraceRoot.initialize({ instrumentModules: { anthropic: anthropicSDK } });

    const client = new Anthropic();
    ```
  </Tab>
</Tabs>

## Usage

Once initialized, all Anthropic calls are captured automatically:

<Tabs>
  <Tab title="Python">
    ```python theme={null}
    import anthropic

    client = anthropic.Anthropic()

    # This call is automatically traced
    response = client.messages.create(
        model="claude-sonnet-4-20250514",
        max_tokens=1024,
        messages=[
            {"role": "user", "content": "What is the capital of France?"},
        ],
    )

    print(response.content[0].text)
    ```
  </Tab>

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

    const client = new Anthropic();

    // This call is automatically traced
    const response = await client.messages.create({
      model: 'claude-sonnet-4-20250514',
      max_tokens: 1024,
      messages: [
        { role: 'user', content: 'What is the capital of France?' },
      ],
    });

    console.log(response.content[0].text);
    ```
  </Tab>
</Tabs>

## What Gets Captured

| Attribute | Description                                                   |
| --------- | ------------------------------------------------------------- |
| Model     | `claude-sonnet-4-20250514`, `claude-haiku-4-5-20251001`, etc. |
| Messages  | Input messages array                                          |
| Response  | Completion content                                            |
| Tokens    | Input and output tokens                                       |
| Cost      | Calculated from token usage and model pricing                 |
| Latency   | Request duration                                              |

## Run the example

Clone the repo and run a complete agent end-to-end.

<CardGroup cols={2}>
  <Card title="Python" icon="python" href="https://github.com/traceroot-ai/traceroot/tree/main/examples/python/anthropic-tool-agent">
    Run the Python example
  </Card>

  <Card title="TypeScript" icon="js" href="https://github.com/traceroot-ai/traceroot/tree/main/examples/typescript/anthropic">
    Run the TypeScript example
  </Card>
</CardGroup>
