Set Up Agent TracingNEW

Monitor agents with token usage, latency, tool execution, and error tracking.

With Sentry Agent Tracing, 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. Agent Tracing data is fully connected to your other Sentry data like logs, errors, and traces.

Agent-Assisted Setup
Use curl to download, read and follow https://skills.sentry.dev/instrument to set up the Sentry Python SDK.
Your agent will set up Sentry in your Python app automatically. Works with Cursor, Claude Code, Codex, and more.View docs ↗
Install the full plugin

Install the Sentry plugin to give your assistant every skill. See the installation docs for more details.

Copied
npx @sentry/agent-plugin install

Enable tracing. 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).

Copied
import sentry_sdk

sentry_sdk.init(
    dsn="___PUBLIC_DSN___",
    traces_sample_rate=1.0,
    send_default_pii=True,
)

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

Conversations 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.

Copied
import sentry_sdk.ai

sentry_sdk.ai.set_conversation_id("conv_abc123")

The Conversations view includes a User column. To populate it, call set_user once per request or session, before any AI calls:

Copied
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 view displays whichever fields are present.

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:

Copied
import sentry_sdk
from sentry_sdk.integrations.openai import OpenAIIntegration

sentry_sdk.init(
    dsn="___PUBLIC_DSN___",
    send_default_pii=True,
    integrations=[
        OpenAIIntegration(include_prompts=False),
    ],
)

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

From SDK 2.64.0, gen_ai spans are sent as standalone envelope items (avoids large-payload drops; required for Conversations).

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

Copied
import sentry_sdk

sentry_sdk.init(
    dsn="___PUBLIC_DSN___",
    stream_gen_ai_spans=False,
)

You can also instrument agent spans yourself. See manual instrumentation.

If you're building MCP (Model Context Protocol) servers, Sentry can also track tool executions, prompt retrievals, and resource access. See MCP integration or manual MCP instrumentation.

Was this helpful?
Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").