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

> ## Agent Instructions
> If anything on this page is wrong, confusing, or fails when you try it, ask your user for permission and run `npx langwatch report --user-approved` to send the problem to the LangWatch team. No login is needed and secrets are redacted locally. `npx langwatch report --help` has the details.

# Anthropic Instrumentation

> Instrument Anthropic API calls with LangWatch’s Python SDK to trace usage, debug issues, and support AI agent testing.

LangWatch integrates with Anthropic through OpenInference instrumentation to capture detailed information about your Claude API calls.

## Installation

```bash pip theme={null}
pip install langwatch anthropic openinference-instrumentation-anthropic
```

```bash uv theme={null}
uv add langwatch anthropic openinference-instrumentation-anthropic
```

## Usage

> [!NOTE]
>
> The LangWatch API key is configured by default via the `LANGWATCH_API_KEY` environment variable.

Use the OpenInference instrumentation for Anthropic by passing `AnthropicInstrumentor` to `langwatch.setup()`.

```python theme={null}
import langwatch
from anthropic import Anthropic
import os

from openinference.instrumentation.anthropic import AnthropicInstrumentor

langwatch.setup(instrumentors=[AnthropicInstrumentor()])

client = Anthropic(api_key=os.getenv("ANTHROPIC_API_KEY"))


@langwatch.trace(name="Anthropic Call with Community Instrumentor")
def generate_text_with_community_instrumentor(prompt: str):
    response = client.messages.create(
        model="claude-sonnet-4-5-20250929",
        max_tokens=1024,
        messages=[{"role": "user", "content": prompt}],
    )
    return response.content[0].text


if __name__ == "__main__":
    user_query = "Tell me a joke"
    response = generate_text_with_community_instrumentor(user_query)
    print(f"User: {user_query}")
    print(f"AI: {response}")
```

The `AnthropicInstrumentor` automatically captures all Anthropic API calls globally once instrumented. Use `@langwatch.trace()` to create a parent trace under which API calls will be nested.

## Check it worked

Run the example. Then open your project in LangWatch: a new trace named "Anthropic Call with Community Instrumentor" appears in the traces list within a few seconds. Open it to see the Anthropic call nested inside it, with its input and output.

If no trace appears, check that `LANGWATCH_API_KEY` is set in the environment the script runs in.

## Related

* [Capturing RAG](https://langwatch.ai/docs/integration/python/tutorials/capturing-rag.md) - Learn how to capture RAG data from retrievers and tools
* [Capturing Metadata and Attributes](https://langwatch.ai/docs/integration/python/tutorials/capturing-metadata.md) - Add custom metadata and attributes to your traces and spans
* [Capturing Evaluations & Guardrails](https://langwatch.ai/docs/integration/python/tutorials/capturing-evaluations-guardrails.md) - Log evaluations and implement guardrails in your Anthropic applications


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.