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

# Anthropic (Python)

> Automatic tracing for the Anthropic Python SDK via wrap_anthropic()

# Anthropic Integration (Python)

The `mutagent-anthropic` package provides zero-change tracing for the official Anthropic Python SDK. Wrap your client once; every `messages.create` call is automatically traced.

## Installation

<Warning>
  This package is coming soon to PyPI. The install command below will work once published.
</Warning>

```bash theme={null}
pip install mutagent-anthropic
```

This installs `mutagent-anthropic` along with its dependencies. Tracing transport is provided by `mutagent-sdk` via the `mutagent.tracing` module. The `anthropic` SDK (>= 0.40.0) is also required and installed automatically.

## Quick Start

<Steps>
  <Step title="Initialize tracing">
    ```python theme={null}
    from mutagent.tracing import init_tracing

    init_tracing(api_key="mt_xxxxxxxxxxxx")
    ```
  </Step>

  <Step title="Wrap your Anthropic client">
    ```python theme={null}
    from anthropic import Anthropic
    from mutagent_anthropic import wrap_anthropic

    client = wrap_anthropic(Anthropic())
    ```
  </Step>

  <Step title="Use the client normally">
    ```python theme={null}
    msg = client.messages.create(
        model="claude-opus-4-5",
        max_tokens=1024,
        messages=[{"role": "user", "content": "Hello!"}],
    )
    print(msg.content[0].text)
    ```
  </Step>
</Steps>

Every `messages.create` call is automatically traced and sent to MutagenT. No additional code changes required.

## Full Example

```python theme={null}
import os
from anthropic import Anthropic
from mutagent_anthropic import wrap_anthropic
import asyncio
from mutagent.tracing import init_tracing, shutdown_tracing

# Initialize MutagenT tracing
init_tracing(api_key=os.environ["MUTAGENT_API_KEY"])

# Wrap once — all subsequent calls are traced
client = wrap_anthropic(Anthropic())

msg = client.messages.create(
    model="claude-opus-4-5",
    max_tokens=1024,
    messages=[
        {"role": "user", "content": "Explain observability for AI applications in 3 sentences."},
    ],
)
print(msg.content[0].text)

# Flush remaining spans on exit (shutdown_tracing is async)
asyncio.run(shutdown_tracing())
```

## Async Client

```python theme={null}
from anthropic import AsyncAnthropic
from mutagent_anthropic import wrap_anthropic
from mutagent.tracing import init_tracing

init_tracing(api_key="mt_xxxxxxxxxxxx")

client = wrap_anthropic(AsyncAnthropic())

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

## Optional: Session and Tags

Pass metadata to every span emitted by a client instance:

```python theme={null}
client = wrap_anthropic(
    Anthropic(),
    generation_name="my-pipeline-call",
    session_id="user-session-abc",
    tags=["prod", "v2"],
)
```

## What Gets Traced

Each non-streaming `messages.create` call emits one `llm.chat` span with:

| Field                   | Source                                               |
| ----------------------- | ---------------------------------------------------- |
| `input.messages`        | `messages` param + `system` param (as `role=system`) |
| `output.text`           | First text block in response                         |
| `metrics.model`         | `model` from response                                |
| `metrics.provider`      | `"anthropic"`                                        |
| `metrics.input_tokens`  | `usage.input_tokens`                                 |
| `metrics.output_tokens` | `usage.output_tokens`                                |
| `metrics.total_tokens`  | `input_tokens + output_tokens`                       |
| `attributes.session_id` | `session_id` kwarg (if set)                          |
| `attributes.tags`       | `tags` kwarg (if set)                                |

## `wrap_anthropic` API

```python theme={null}
wrap_anthropic(client, *, generation_name=None, session_id=None, tags=None)
```

Patches `client.messages.create` in-place and returns the same client instance.

| Arg               | Type                          | Default                       | Description                          |
| ----------------- | ----------------------------- | ----------------------------- | ------------------------------------ |
| `client`          | `Anthropic \| AsyncAnthropic` | required                      | Client to patch                      |
| `generation_name` | `str \| None`                 | `"anthropic.messages.create"` | Span name override                   |
| `session_id`      | `str \| None`                 | `None`                        | Session ID stored in span attributes |
| `tags`            | `list[str] \| None`           | `None`                        | Tags stored in span attributes       |

## Limitations

* **Streaming (`stream=True`) is not traced in v0.1.0.** Streaming requests pass through transparently. Tracking as a follow-up.

## TypeScript Equivalent

MutagenT does not currently ship an Anthropic integration for TypeScript. For TypeScript LLM tracing, see [OpenAI (TypeScript)](/integrations/openai) or [Vercel AI SDK](/integrations/vercel-ai).

## Related

<CardGroup cols={2}>
  <Card title="Python Integrations" icon="python" href="/integrations/python/overview">
    Overview of all Python integration packages
  </Card>

  <Card title="Python Tracing" icon="radar" href="/sdk/python/tracing">
    Low-level tracing API and `@trace` decorator
  </Card>

  <Card title="OpenAI (Python)" icon="bolt" href="/integrations/python/openai">
    Trace OpenAI calls in Python
  </Card>

  <Card title="API Reference" icon="book" href="/api-reference/overview">
    REST API documentation
  </Card>
</CardGroup>
