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

# Set Up Agent Tracing | Sentry 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.

## [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 — set `send_default_pii=True` to capture it (or configure `include_prompts` per integration).

```python
import sentry_sdk

sentry_sdk.init(
    dsn="https://<key>@o<orgId>.ingest.sentry.io/<projectId>",
    traces_sample_rate=1.0,
    send_default_pii=True,
)
```

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

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

* [Anthropic](https://docs.sentry.io/platforms/python/integrations/anthropic.md)
* [Google Gen AI](https://docs.sentry.io/platforms/python/integrations/google-genai.md)
* [OpenAI](https://docs.sentry.io/platforms/python/integrations/openai.md)
* [OpenAI Agents SDK](https://docs.sentry.io/platforms/python/integrations/openai-agents.md)
* [LangChain](https://docs.sentry.io/platforms/python/integrations/langchain.md)
* [LangGraph](https://docs.sentry.io/platforms/python/integrations/langgraph.md)
* [LiteLLM](https://docs.sentry.io/platforms/python/integrations/litellm.md)
* [Pydantic AI](https://docs.sentry.io/platforms/python/integrations/pydantic-ai.md)
* [Hugging Face Hub](https://docs.sentry.io/platforms/python/integrations/huggingface_hub.md)

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

Tracking Conversations has **beta** stability. Configuration options and behavior may change.

[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 — Conversations 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. 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:

```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

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