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

# Anthropic

> Auto-trace Anthropic SDK calls with patch_anthropic_client

Install the integration extra:

```bash theme={null}
pip install "agentx-python[anthropic]"
```

## Usage

Call `patch_anthropic_client()` once after creating your Anthropic client. All subsequent `client.messages.create()` and `client.messages.stream()` calls are traced automatically — no changes to individual API calls are needed.

```python theme={null}
from agentx import AgentX
from agentx.integrations.anthropic import patch_anthropic_client
import anthropic

agentx = AgentX.from_env()
client = anthropic.Anthropic()

patch_anthropic_client(
    client,
    tracer=client.tracer,
    name="claude-support-agent",
    metadata={"env": "production"},
    session_id="session-xyz-789",
)

# Regular call — traced automatically
response = client.messages.create(
    model="claude-haiku-4-5-20251001",
    max_tokens=256,
    messages=[{"role": "user", "content": "How do I cancel my subscription?"}],
)

# Streaming call — also traced automatically
with client.messages.stream(
    model="claude-haiku-4-5-20251001",
    max_tokens=256,
    messages=[{"role": "user", "content": "What is your refund policy?"}],
) as stream:
    for text in stream.text_stream:
        print(text, end="", flush=True)
```

## What gets traced

Each `messages.create()` or `messages.stream()` call produces one trace.

| Field       | Source                                                      |
| ----------- | ----------------------------------------------------------- |
| `input`     | `messages` kwarg                                            |
| `output`    | `response.content[0].text` (or streamed final message text) |
| `latencyMs` | Wall-clock time of the API call                             |
| `model`     | `model` kwarg                                               |
| `error`     | Exception message if the API call raises                    |

## `patch_anthropic_client()` reference

```python theme={null}
patch_anthropic_client(
    client: anthropic.Anthropic,
    tracer: Tracer,
    name: str = "anthropic-agent",
    metadata: dict | None = None,
    session_id: str | None = None,
) -> None
```

| Parameter    | Description                                       |
| ------------ | ------------------------------------------------- |
| `client`     | The `anthropic.Anthropic()` instance to patch     |
| `tracer`     | `agentx.tracer` from your `AgentX` instance       |
| `name`       | Label shown in the AgentX UI for every trace      |
| `metadata`   | Static key-value metadata attached to every trace |
| `session_id` | Links traces from the same conversation thread    |

Calling `patch_anthropic_client()` on an already-patched client is a no-op — it is safe to call multiple times.

## Full example

```python theme={null}
from agentx import AgentX
from agentx.integrations.anthropic import patch_anthropic_client
import anthropic

agentx = AgentX.from_env()
client = anthropic.Anthropic()

patch_anthropic_client(
    client,
    tracer=agentx.tracer,
    name="claude-support-agent",
    metadata={"env": "production"},
    session_id="session-xyz-789",
)

response = client.messages.create(
    model="claude-haiku-4-5-20251001",
    max_tokens=256,
    messages=[{"role": "user", "content": "How do I cancel my subscription?"}],
)
print(response.content[0].text)

agentx.tracer.flush(timeout=10)
```
