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.
Use curl to download, read and follow https://skills.sentry.dev/instrument to set up the Sentry Python SDK.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).
import sentry_sdk
sentry_sdk.init(
dsn="___PUBLIC_DSN___",
traces_sample_rate=1.0,
send_default_pii=True,
)
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.
import sentry_sdk.ai
sentry_sdk.ai.set_conversation_id("conv_abc123")
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:
import sentry_sdk
sentry_sdk.set_user({"id": "user_123", "email": "jane@example.com", "username": "jane"})
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:
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),
],
)
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.
import sentry_sdk
sentry_sdk.init(
dsn="___PUBLIC_DSN___",
stream_gen_ai_spans=False,
)
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.
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").