Set Up Agent Tracing for CloudflareNEW
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 Sentry.As of SDK version 11.0.0, Sentry auto-instruments the most popular AI SDKs and frameworks out of the box — no manual setup is required. Just enable tracing, then pick your AI stack below. Each integration page has the details.
A Cloudflare AI application can need three independent layers. Inspect the host, every model client it calls, and its conversation IDs instead of stopping after the first integration:
- Instrument the host. Use the Vite plugin, which wraps your Worker and your Agents SDK classes at build time. If you deploy with
wranglerdirectly, wrap them yourself as shown in the Wrangler setup. - Instrument each model client. Workers AI (
env.AI) is automatic when the Sentry host wrapper supplies the instrumentedenv. For bundled provider SDKs, use Vite build-time instrumentation or the provider's manual wrapper. Follow the matching integration page for Vercel AI, LangChain, LangGraph, or another library. - Group the conversation. The Agents SDK can infer a conversation ID. For other chat applications, including Workers AI without the Agents SDK, set one before the model or tool calls. See Tracking Conversations.
For example, an Agents SDK class that calls env.AI.run() needs the Agents SDK host setup and Workers AI model instrumentation. The host wrapper supplies the instrumented binding, so do not add another provider wrapper around the same env.AI call.
Flue is the exception to this composition. Its blueprint owns the host and model instrumentation. Do not add another Sentry provider integration to Flue-generated model calls, or token and cost totals will be counted twice.
Conversations groups multi-turn AI activity into a single replay of messages and tool calls. Use setConversationId() so every AI span in a chat session shares the same gen_ai.conversation.id.
Some integrations infer a conversation ID automatically. For everything else (including Workers AI), 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 isolation scope (which is typically request-scoped). Pass null to unset it.
Sentry.setConversationId("conv_abc123");
Sentry.setConversationId("conv_abc123");
The Conversations view includes a User column. To populate it, call setUser once per request or session, before any AI calls:
Sentry.setUser({
id: "user_123",
email: "jane@example.com",
username: "jane",
});
Sentry.setUser({
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. See the setUser API reference for the full list of supported fields.
The SDK records generative AI inputs and outputs by default. To turn them off:
src/instrument.server.tsimport { defineCloudflareOptions } from "@sentry/cloudflare";
export default defineCloudflareOptions((env) => ({
dsn: "___PUBLIC_DSN___",
tracesSampleRate: 1.0,
dataCollection: {
genAI: { inputs: false, outputs: false },
},
}));
import { defineCloudflareOptions } from "@sentry/cloudflare";
export default defineCloudflareOptions((env) => ({
dsn: "___PUBLIC_DSN___",
tracesSampleRate: 1.0,
dataCollection: {
genAI: { inputs: false, outputs: false },
},
}));
On Cloudflare, control Vercel AI I/O with integration-level record* options or with experimental_telemetry per call. See each integration page.
From SDK 10.61.0, gen_ai spans are sent as standalone envelope items (avoids large-payload drops; required for Conversations).
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 Monitoring for setup instructions.
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").