---
title: "Agent Tracing"
description: "Debug agent runs and see where they fail or slow down."
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.md), you can debug how an agent run flows through your application, and where it fails or slows down. Token usage, latency, and errors show up on that same trace, next to your requests, jobs, and logs. Multi-turn chats are grouped in [Conversations](https://docs.sentry.io/product/agents/conversations.md).

## 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 captured by default — use the `gen_ai` key of the [`data_collection` option](https://docs.sentry.io/platforms/python/configuration/options.md#data_collection) to turn it off. In SDK versions prior to `3.0`, it's off by default unless you use `data_collection` or set `send_default_pii=True`.

```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, such as prompts, messages, responses, and tool definitions, are recorded by default. See each integration page for details. To control this, 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:

```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},
    },
)
```

In SDK versions prior to `3.0`, LLM inputs and outputs are treated as PII and are **off** by default unless you pass a `data_collection` dictionary or set `send_default_pii=True`. To keep other PII on but turn AI content off with `send_default_pii=True`, set `include_prompts=False` on the integration:

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

# SDK 2.x only
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)

Only available in SDK versions prior to `3.0`. Starting with `3.0`, all spans, including `gen_ai` spans, are streamed, and the `stream_gen_ai_spans` option was removed.

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)
- [Manual Instrumentation](https://docs.sentry.io/platforms/python/agent-tracing/manual-instrumentation__v2.x.md)

---

Find other pages: [Sitemap](https://docs.sentry.io/sitemap.md) lists every page with a one-line summary, and [llms.txt](https://docs.sentry.io/llms.txt) gives an overview with guidance for agents.
