Skip to main content
Mastra does not have a dedicated auto-instrument yet. Until it lands, use the TruLayer TypeScript SDK’s generic trace / span API — the pattern below covers every Mastra primitive.

Install

Instrument an agent run

Wrap each agent.generate() or agent.stream() call in a trace. If you also call the Vercel AI SDK or OpenAI SDK from within tools, stack the corresponding TruLayer instrumentations so nested spans show up in the waterfall.

Instrument a workflow step

What gets captured

  • A trace per workflow or agent run.
  • One span per workflow step or tool invocation (whatever you wrap).
  • Nested llm spans automatically when the Vercel AI SDK or OpenAI SDK is also instrumented — no extra code required.

Known gotchas

  • No auto-discovery yet. You must wrap each Mastra entry point explicitly. A dedicated instrumentMastra helper is on the roadmap.
  • Next.js Edge runtime is unsupported. Use export const runtime = "nodejs" on any Route Handler that invokes instrumented code.
  • Streaming. When using agent.stream(), keep the span open until the stream completes (for await loop inside the span(...) callback) so token counts are recorded.