> ## Documentation Index
> Fetch the complete documentation index at: https://traceroot.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Mastra

> Export traces from Mastra agents to traceroot via the OTLP exporter

Export traces from [Mastra](https://mastra.ai) agents to traceroot using the `@traceroot-ai/mastra` exporter package. This integrates with Mastra's built-in OpenTelemetry observability system.

## Install

> [tab] TypeScript
>
> ```bash theme={null}
> npm install @traceroot-ai/mastra @mastra/core @mastra/observability
> ```

## Set up

Configure traceroot as an exporter in your Mastra instance:

```typescript theme={null}
import { Mastra } from '@mastra/core';
import { Observability } from '@mastra/observability';
import { TraceRootExporter } from '@traceroot-ai/mastra';

const exporter = new TraceRootExporter({
  apiKey: process.env.TRACEROOT_API_KEY,
});

const mastra = new Mastra({
  agents: { /* your agents */ },
  observability: new Observability({
    configs: {
      traceroot: {
        serviceName: 'my-mastra-app',
        exporters: [exporter],
      },
    },
  }),
});
```

## Usage

Once configured, all agent calls are traced automatically:

```typescript theme={null}
import { Agent } from '@mastra/core/agent';
import { anthropic } from '@ai-sdk/anthropic';

const weatherAgent = new Agent({
  id: 'weatherAgent',
  name: 'Weather Agent',
  instructions: 'You are a helpful weather assistant.',
  model: anthropic('claude-haiku-4-5-20251001'),
  tools: { /* your tools */ },
});

const mastra = new Mastra({
  agents: { weatherAgent },
  observability: new Observability({
    configs: {
      traceroot: {
        serviceName: 'mastra-weather-agent',
        exporters: [exporter],
      },
    },
  }),
});

const agent = mastra.getAgent('weatherAgent');

// Pass a consistent threadId to group calls under the same session
const result = await agent.generate("What's the weather in Tokyo?", {
  threadId: 'session-123',
  resourceId: 'user-456',
});

console.log(result.text);

// Flush traces before process exit
await exporter.flush();
```

## Verify

Run your code and confirm the trace arrives:

1. **In the traceroot UI**: Navigate to **Tracing > Traces** in [traceroot Cloud](https://app.traceroot.ai). You should see a trace with root span `agent run: 'weatherAgent'`, named after the agent id.
2. **Via the CLI**: Run `traceroot traces list` to inspect your recent traces from the terminal.

## What gets captured

| Attribute | Description |
| - | - |
| Agent name | The Mastra agent ID |
| Model | Underlying LLM model name |
| Messages | Input messages and conversation history |
| Response | Generated text output |
| Tool calls | Each tool invocation with input and output |
| Tokens | Input and output token counts |
| Cost | Calculated when the model ID matches traceroot's pricing table |
| Latency | Request duration per span |

## Limitations

* Tracing requires exporting spans through the `@traceroot-ai/mastra` exporter package configured in Mastra's `Observability` system.

## Run the example

Clone the repo and run a complete agent end-to-end.

> [card] [TypeScript](https://github.com/traceroot-ai/traceroot/tree/main/examples/typescript/mastra)
>
> Run the TypeScript example

## Next steps

> [card] [TypeScript SDK Reference](https://traceroot.ai/docs/tracing/typescript-sdk.md)
>
> Explore the full TypeScript SDK reference and configuration options.

> [card] [Cost Tracking](https://traceroot.ai/docs/tracing/cost-tracking.md)
>
> Learn how traceroot calculates token costs and tracks model spend.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.