Sentry for Mastra (Cloudflare)

Learn how to send Mastra agent traces, model calls, tool executions, and errors to Sentry from Cloudflare Workers.

Mastra is a TypeScript framework for building AI applications and agents. This guide sets up the Sentry SDK in a Mastra app running on Cloudflare Workers so agent runs, model generations, tool calls, workflows, and errors flow into Sentry Agent Tracing.

Before you begin, you need:

  • A Sentry account and project. The project's DSN tells the SDK where to send data.
  • A Mastra application using @mastra/core version 1.63.2 or later.
  • @mastra/observability. Sentry's Mastra integration needs it to attach to Mastra's telemetry pipeline.
  • @sentry/cloudflare version 11.0.0 or later.

Choose the features you want to configure, and this guide will show you how:

Install the Sentry Cloudflare SDK and Mastra's observability package:

Copied
npm install @sentry/cloudflare@^11.0.0 @mastra/observability@latest

If Mastra can't load @mastra/observability, the integration warns and creates no spans.

Add the Sentry Vite plugin to your Vite config. It wraps your Worker entry with Sentry at build time:

vite.config.mts
Copied
import { cloudflare } from "@cloudflare/vite-plugin";
import { sentryCloudflareVitePlugin } from "@sentry/cloudflare/vite";
import { defineConfig } from "vite";

export default defineConfig({
  plugins: [cloudflare(), sentryCloudflareVitePlugin()],
});

Create an instrument.server.ts file next to your Worker entry (the file named in Wrangler's main). The plugin picks it up by convention and uses its default export as the Sentry options:

src/instrument.server.ts
Copied
export default (env: Env) => ({
  dsn: "___PUBLIC_DSN___",
  // ___PRODUCT_OPTION_START___ performance

  // Set tracesSampleRate to 1.0 to capture 100%
  // of spans for tracing.
  // We recommend adjusting this value in production.
  tracesSampleRate: 1.0,
  // ___PRODUCT_OPTION_END___ performance
});

Enable Node.js compatibility in your wrangler.toml so Mastra and the SDK can run on Workers:

wrangler.toml
Copied
main = "src/index.ts"
compatibility_date = "2026-04-26"
compatibility_flags = ["nodejs_compat"]

With the plugin in place, Sentry.init() runs automatically — you don't add an instrument file or --import flag as you do for Node.js.

With this setup, Sentry captures errors thrown by your agents and AI spans for every run (agent runs, model generations, tool calls, workflows, token usage, and latency) along with outgoing HTTP and fetch requests. Prompts and model outputs are recorded on your AI spans by default.

To review what's captured and turn recording of prompts and responses off, see Control the Data You Send to Sentry below.

By default, the SDK sends the inputs and outputs of your LLM and tool calls, such as prompts, responses, and tool arguments. This will give you rich debugging context. Review the data your agent handles before you deploy to production.

The SDK always filters sensitive values whose keys match a built-in denylist, such as auth or password, and sends [Filtered] instead.

To send less data, turn off the categories you don't need in the dataCollection option:

src/instrument.server.ts
Copied
export default (env: Env) => ({
  dsn: "___PUBLIC_DSN___",
dataCollection: { genAI: { inputs: false, outputs: false }, },
});

For the full list of categories and their defaults, see the dataCollection options.

Sentry groups a multi-turn chat into a single Conversation automatically — there's no Sentry-specific setup. Mastra maps its memory thread id to gen_ai.conversation.id, so agent runs that share a thread id are grouped into one conversation.

Threads come from Mastra's own memory feature. If your app already passes a thread when it calls an agent — as multi-turn chat apps do — Sentry picks it up as the conversation id with no extra work:

Copied
const result = await agent.generate(message, {
  memory: {
    thread: "session-123",
    resource: "user-456",
  },
});

Your agent needs a storage provider configured before Mastra accepts the memory option. Reuse the same thread id across the messages in a session.

Run one of your Mastra agents, then open Explore → Agents in Sentry. Select the trace to open its detail. The timeline shows the model generations, tool calls, token usage, latency, and any errors your agent threw.

If no data appears, confirm that:

  • The dsn belongs to the Sentry project you're viewing.
  • tracesSampleRate is greater than 0.
  • @mastra/observability is installed, so the integration can attach to Mastra's telemetry.
  • Your application completed at least one agent run after you added the setup.

What gets captured?

Sentry's Mastra integration maps Mastra's span types to Sentry operations for the Agents dashboards:

Mastra Span TypeSentry Operation
AGENT_RUNgen_ai.invoke_agent
WORKFLOW_RUNgen_ai.invoke_agent
MODEL_GENERATIONgen_ai.chat
TOOL_CALLgen_ai.execute_tool
MCP_TOOL_CALLgen_ai.execute_tool
PROVIDER_TOOL_CALLgen_ai.execute_tool
CLIENT_TOOL_CALLgen_ai.execute_tool
RAG_EMBEDDINGgen_ai.embeddings
CLASSIFIER_EVALUATIONgen_ai.decide

Spans are tagged with sentry.origin: auto.ai.mastra.

  • @sentry/cloudflare: >=11.0.0
  • @mastra/core: >=1.63.2
  • @mastra/observability: required for the integration to create spans
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").