Learn how to manually instrument your code to use Sentry's Agents module.
With Sentry AI Agent Monitoring, 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. AI Agent Monitoring data will be fully connected to your other Sentry data like logs, errors, and traces.
As a prerequisite to setting up AI Agent Monitoring with Python, you'll need to first set up tracing. Once this is done, the Python SDK will automatically instrument AI agents created with supported libraries. If that doesn't fit your use case, you can use custom instrumentation described below.
The Python SDK supports automatic instrumentation for some AI libraries. We recommend adding their integrations to your Sentry configuration to automatically capture spans for AI agents.
For your AI agents data to show up in the Sentry AI Agents Insights, two spans must be created and have well-defined names and data attributes. See below.
The text representation of the model's responses. [0]
"[\"The weather in Paris is rainy\", \"The weather in London is sunny\"]"
gen_ai.usage.input_tokens.cache_write
int
optional
The number of tokens written to the cache when processing the AI input (prompt).
100
gen_ai.usage.input_tokens.cached
int
optional
The number of cached tokens used in the AI input (prompt)
50
gen_ai.usage.input_tokens
int
optional
The number of tokens used in the AI input (prompt).
10
gen_ai.usage.output_tokens.reasoning
int
optional
The number of tokens used for reasoning.
30
gen_ai.usage.output_tokens
int
optional
The number of tokens used in the AI response.
100
gen_ai.usage.total_tokens
int
optional
The total number of tokens used to process the prompt. (input and output)
190
[0]: Span attributes only allow primitive data types. This means you need to use a stringified version of a list of dictionaries. Do NOT set [{"foo": "bar"}] but rather the string "[{\"foo\": \"bar\"}]".
[1]: Each message item uses the format {role:"", content:""}. The role can be "user", "assistant", or "system". The content can be either a string or a list of dictionaries.
The text representation of the model's responses. [0]
"[\"The weather in Paris is rainy\", \"The weather in London is sunny\"]"
gen_ai.usage.input_tokens.cache_write
int
optional
The number of tokens written to the cache when processing the AI input (prompt).
100
gen_ai.usage.input_tokens.cached
int
optional
The number of cached tokens used in the AI input (prompt)
50
gen_ai.usage.input_tokens
int
optional
The number of tokens used in the AI input (prompt).
10
gen_ai.usage.output_tokens.reasoning
int
optional
The number of tokens used for reasoning.
30
gen_ai.usage.output_tokens
int
optional
The number of tokens used in the AI response.
100
gen_ai.usage.total_tokens
int
optional
The total number of tokens used to process the prompt. (input and output)
190
[0]: Span attributes only allow primitive data types (like int, float, boolean, string). This means you need to use a stringified version of a list of dictionaries. Do NOT set [{"foo": "bar"}] but rather the string "[{\"foo\": \"bar\"}]".
[1]: Each message item uses the format {role:"", content:""}. The role can be "user", "assistant", or "system". The content can be either a string or a list of dictionaries.
This span marks the transition of control from one agent to another, typically when the current agent determines another agent is better suited to handle the task.
Handoff span attributes
A span that describes the handoff from one agent to another.
The spans op MUST be "gen_ai.handoff".
The spans name SHOULD be "handoff from {from_agent} to {to_agent}".
import sentry_sdk
with sentry_sdk.start_span(op="gen_ai.handoff", name="handoff from Weather Agent to Travel Agent"):pass# Handoff span just marks the transitionwith sentry_sdk.start_span(op="gen_ai.invoke_agent", name="invoke_agent Travel Agent"):# Run the target agent herepass
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").