Skip to main content
Mastra is a TypeScript framework for building AI agents. Braintrust integrates with Mastra’s observability system to automatically trace agent executions, LLM calls, and tool usage.

Setup

Install Mastra with the Braintrust exporter:

Trace with Mastra

Braintrust traces Mastra agent runs, workflows, steps, tool calls, and LLM calls. Enable tracing without code changes through auto-instrumentation, or configure a Braintrust exporter manually.

Auto-instrumentation

To trace Mastra without modifying your application code, run your app with Braintrust’s import hook. The hook adds a Braintrust exporter to Mastra’s observability automatically, so you don’t configure one yourself. Requires @mastra/core v1.20.0 or later and the @mastra/observability package.
1

Build your Mastra app

mastra-app.js
2

Run with the import hook

The auto-instrumentation example uses plain JavaScript so node --import can run the file directly. The Braintrust APIs work the same in TypeScript projects — compile your TypeScript to JavaScript, then run the compiled file with the import hook.
If you’re using a bundler, see Trace LLM calls for plugin and loader setup.

Manual instrumentation

To configure tracing explicitly, add a Braintrust exporter to Mastra’s Observability settings yourself. Use the BraintrustObservabilityExporter built into the braintrust SDK:
trace-mastra.ts
Alternatively, use the BraintrustExporter from the Mastra-maintained @mastra/braintrust package, which accepts a Braintrust logger directly:
trace-mastra-exporter.ts
The BraintrustExporter constructor can accept a braintrust.Span, braintrust.Experiment, or braintrust.Logger as the braintrustLogger option. This enables automatic nesting of Mastra traces within Braintrust contexts like evals or traced functions.

What Braintrust traces

Braintrust captures:
  • Agent run spans (agent run: <name>) and workflow run spans (workflow run: <name>) as task spans, with inputs and outputs.
  • LLM spans (llm: <model>) for model generations, with the messages as input and the response as output.
  • Workflow step spans (workflow step: <id>) and tool call spans for the steps and tools an agent or workflow runs.
  • Token usage metrics (prompt, completion, total, plus cached, cache-creation, and reasoning tokens when reported) on LLM spans.
  • Span metadata, including entity_id, entity_name, and entity_type, plus the model and provider on LLM spans.
  • Errors captured on every span.

Evaluate with Mastra

Use Mastra agents as the task in a Braintrust Eval to build and evaluate agentic workflows:
eval-mastra.ts

Resources