---
title: "Agent Tracing"
description: "Monitor agents with token usage, latency, tool execution, and error tracking."
url: https://docs.sentry.io/platforms/python/agent-tracing/
---

# Set Up Agent Tracing for Python

With [Sentry Agent Tracing](https://docs.sentry.io/product/agents/dashboards.md), you can monitor and debug your AI systems with full-stack context. You'll be able to track key insights like token usage, latency, tool usage, and error rates — and group multi-turn chats in [Conversations](https://docs.sentry.io/product/agents/conversations.md). Agent Tracing data is fully connected to your other Sentry data like logs, errors, and traces.

## Set up with Sentry CLI

Set up the Sentry Python SDK with the Sentry CLI. Install the CLI with one of these commands, then follow the Sentry CLI docs to finish setup.

**cURL**

```bash
curl https://cli.sentry.dev/install -fsS | bash
```

**PowerShell**

```powershell
irm https://cli.sentry.dev/install.ps1 | iex
```

**Homebrew**

```bash
brew install getsentry/tools/sentry
```

**npx**

```bash
npx sentry@latest
```

[Learn more about the Sentry CLI](https://cli.sentry.dev/)

## [Getting Started](https://docs.sentry.io/platforms/python/agent-tracing.md#getting-started)

Enable [tracing](https://docs.sentry.io/platforms/python/tracing.md). Supported AI libraries are instrumented automatically when their packages are installed. Prompt and response content is off by default — use the `gen_ai` key of the [`data_collection` option](https://docs.sentry.io/platforms/python/configuration/options.md#data_collection) to capture it. Alternatively, set `send_default_pii=True` (and configure `include_prompts` per integration). `send_default_pii` will be removed in version 3.0 of the SDK.

```python
import sentry_sdk

sentry_sdk.init(
    dsn="https://<key>@o<orgId>.ingest.sentry.io/<projectId>",
    traces_sample_rate=1.0,
    # Check https://docs.sentry.io/platforms/python/data-management/data-collected/ to configure what data is collected
    data_collection={
        "gen_ai": {"inputs": True, "outputs": True},
    },
)
```

## [Instrumentation](https://docs.sentry.io/platforms/python/agent-tracing.md#instrumentation)

Pick your AI stack. Each page has install and verify details.

[![aws icon](https://docs.sentry.io/_next/static/media/aws.0bbeac02.svg)](https://docs.sentry.io/platforms/python/agent-tracing/bedrock.md)

[Amazon Bedrock](https://docs.sentry.io/platforms/python/agent-tracing/bedrock.md)

[![anthropic icon](https://docs.sentry.io/_next/static/media/anthropic.6478e781.svg)](https://docs.sentry.io/platforms/python/agent-tracing/anthropic.md)

[Anthropic](https://docs.sentry.io/platforms/python/agent-tracing/anthropic.md)

[![google-genai icon](https://docs.sentry.io/_next/static/media/gemini.6a0c6609.svg)](https://docs.sentry.io/platforms/python/agent-tracing/google-genai.md)

[Google Gen AI](https://docs.sentry.io/platforms/python/agent-tracing/google-genai.md)

[![openai icon](https://docs.sentry.io/_next/static/media/openai.2d7a0936.svg)](https://docs.sentry.io/platforms/python/agent-tracing/openai.md)

[OpenAI](https://docs.sentry.io/platforms/python/agent-tracing/openai.md)

[![openai icon](https://docs.sentry.io/_next/static/media/openai.2d7a0936.svg)](https://docs.sentry.io/platforms/python/agent-tracing/openai-agents.md)

[OpenAI Agents SDK](https://docs.sentry.io/platforms/python/agent-tracing/openai-agents.md)

[![langchain icon](https://docs.sentry.io/_next/static/media/langchain.00856afb.svg)](https://docs.sentry.io/platforms/python/agent-tracing/langchain.md)

[LangChain](https://docs.sentry.io/platforms/python/agent-tracing/langchain.md)

[![langgraph icon](https://docs.sentry.io/_next/static/media/langgraph.0ea576b7.svg)](https://docs.sentry.io/platforms/python/agent-tracing/langgraph.md)

[LangGraph](https://docs.sentry.io/platforms/python/agent-tracing/langgraph.md)

[![litellm icon](https://docs.sentry.io/_next/static/media/litellm.a596cfae.svg)](https://docs.sentry.io/platforms/python/agent-tracing/litellm.md)

[LiteLLM](https://docs.sentry.io/platforms/python/agent-tracing/litellm.md)

[![pydantic-ai icon](https://docs.sentry.io/_next/static/media/pydantic-ai.634c177b.svg)](https://docs.sentry.io/platforms/python/agent-tracing/pydantic-ai.md)

[Pydantic AI](https://docs.sentry.io/platforms/python/agent-tracing/pydantic-ai.md)

[![huggingface\_hub icon](https://docs.sentry.io/_next/static/media/huggingface.dc0db3de.svg)](https://docs.sentry.io/platforms/python/agent-tracing/huggingface_hub.md)

[Hugging Face Hub](https://docs.sentry.io/platforms/python/agent-tracing/huggingface_hub.md)

[![mistral icon](https://docs.sentry.io/_next/static/media/mistral.9a6254c9.svg)](https://docs.sentry.io/platforms/python/agent-tracing/mistral.md)

[Mistral](https://docs.sentry.io/platforms/python/agent-tracing/mistral.md)

[![typesafe-ai icon](https://docs.sentry.io/_next/static/media/typesafe-ai.bed5ab6d.svg)](https://docs.sentry.io/platforms/python/agent-tracing/typesafe.md)

[TypeSafe AI](https://docs.sentry.io/platforms/python/agent-tracing/typesafe.md)

## [Tracking Conversations](https://docs.sentry.io/platforms/python/agent-tracing.md#tracking-conversations)

[Conversations](https://docs.sentry.io/product/agents/conversations.md) groups multi-turn AI activity into a single replay of messages and tool calls. Use `set_conversation_id()` so every AI span in a chat session shares the same `gen_ai.conversation.id`.

Some integrations (for example OpenAI) infer a conversation ID automatically. For everything else, set it yourself at the start of **every request or operation that makes AI calls**, before those calls run. Reuse the same session ID across messages in the chat. The ID is applied to AI-related spans on the current scope. Call `Scope.remove_conversation_id()` to unset it. For ID format recommendations and limitations, see [Choosing a Conversation ID](https://docs.sentry.io/product/agents/conversations.md#choosing-a-conversation-id).

```python
import sentry_sdk.ai

sentry_sdk.ai.set_conversation_id("conv_abc123")
```

### [Identifying Users in Conversations](https://docs.sentry.io/platforms/python/agent-tracing.md#identifying-users-in-conversations)

The [Conversations](https://docs.sentry.io/product/agents/conversations.md) view includes a **User** column. To populate it, call `set_user` once per request or session, before any AI calls:

```python
import sentry_sdk

sentry_sdk.set_user({"id": "user_123", "email": "jane@example.com", "username": "jane"})
```

Any of `id`, `email`, or `username` is sufficient — the Conversations tab displays whichever fields are present.

## [Options](https://docs.sentry.io/platforms/python/agent-tracing.md#options)

### [Privacy Controls](https://docs.sentry.io/platforms/python/agent-tracing.md#privacy-controls)

LLM inputs and outputs are treated as PII and are **off** by default. Some integrations still attach tool data, such as tool definitions, by default. See each integration page for details. To capture them, use the `gen_ai` key of the [`data_collection` option](https://docs.sentry.io/platforms/python/configuration/options.md#data_collection), which lets you control inputs and outputs separately. When you pass a `data_collection` dictionary, both are collected by default and `include_prompts` is ignored:

```python
import sentry_sdk

sentry_sdk.init(
    dsn="https://<key>@o<orgId>.ingest.sentry.io/<projectId>",
    data_collection={
        # Record LLM inputs, but not outputs
        "gen_ai": {"inputs": True, "outputs": False},
    },
)
```

Alternatively, set `send_default_pii=True` to capture them. To keep other PII on but turn AI content off, set `include_prompts=False` on the integration. `send_default_pii` will be removed in version 3.0 of the SDK.

```python
import sentry_sdk
from sentry_sdk.integrations.openai import OpenAIIntegration

sentry_sdk.init(
    dsn="https://<key>@o<orgId>.ingest.sentry.io/<projectId>",
    send_default_pii=True,
    integrations=[
        OpenAIIntegration(include_prompts=False),
    ],
)
```

See each integration page for the options that apply to your stack.

### [Streaming Gen AI Spans](https://docs.sentry.io/platforms/python/agent-tracing.md#streaming-gen-ai-spans)

From SDK `2.64.0`, `gen_ai` spans are sent as standalone envelope items (avoids large-payload drops; required for [Conversations](https://docs.sentry.io/product/agents/conversations.md)).

Self-hosted Sentry users should set `stream_gen_ai_spans=False` if standalone `gen_ai` spans may not be ingested.

```python
import sentry_sdk

sentry_sdk.init(
    dsn="https://<key>@o<orgId>.ingest.sentry.io/<projectId>",
    stream_gen_ai_spans=False,
)
```

## [Manual Instrumentation](https://docs.sentry.io/platforms/python/agent-tracing.md#manual-instrumentation)

You can also instrument agent spans yourself. See [manual instrumentation](https://docs.sentry.io/platforms/python/agent-tracing/manual-instrumentation.md).

## [MCP Server Monitoring](https://docs.sentry.io/platforms/python/agent-tracing.md#mcp-server-monitoring)

If you're building MCP (Model Context Protocol) servers, Sentry can also track tool executions, prompt retrievals, and resource access. See [MCP integration](https://docs.sentry.io/platforms/python/integrations/mcp.md) or [manual MCP instrumentation](https://docs.sentry.io/platforms/python/tracing/instrumentation/custom-instrumentation/mcp-module.md).

## Pages in this section

- [Anthropic](https://docs.sentry.io/platforms/python/agent-tracing/anthropic.md)
- [Google Gen AI](https://docs.sentry.io/platforms/python/agent-tracing/google-genai.md)
- [OpenAI](https://docs.sentry.io/platforms/python/agent-tracing/openai.md)
- [OpenAI Agents](https://docs.sentry.io/platforms/python/agent-tracing/openai-agents.md)
- [LangChain](https://docs.sentry.io/platforms/python/agent-tracing/langchain.md)
- [LangGraph](https://docs.sentry.io/platforms/python/agent-tracing/langgraph.md)
- [LiteLLM](https://docs.sentry.io/platforms/python/agent-tracing/litellm.md)
- [Pydantic AI](https://docs.sentry.io/platforms/python/agent-tracing/pydantic-ai.md)
- [Hugging Face Hub](https://docs.sentry.io/platforms/python/agent-tracing/huggingface_hub.md)
- [Mistral](https://docs.sentry.io/platforms/python/agent-tracing/mistral.md)
- [TypeSafe AI](https://docs.sentry.io/platforms/python/agent-tracing/typesafe.md)
- [Amazon Bedrock](https://docs.sentry.io/platforms/python/agent-tracing/bedrock.md)
- [Manual Instrumentation](https://docs.sentry.io/platforms/python/agent-tracing/manual-instrumentation.md)
