@contextcompany/mastra package provides automatic instrumentation for Mastra AI agents via Mastra’s built-in AI tracing system. It exports all agent traces, LLM calls, and tool executions to The Context Company for observability.
Installation
Quick Start
Register the TCC exporter in your Mastra configuration:API Reference
TCCMastraExporter
The Mastra AI tracing exporter that captures and exports traces to The Context Company.Configuration options for the exporter.
TCCMastraExporterConfig
Configuration object for the exporter.TCC API key. If not provided, reads from
TCC_API_KEY environment variable.Keys starting with dev_ automatically route to the development endpoint.Custom TCC endpoint URL. Defaults to
https://api.thecontext.company/v1/mastra (or dev endpoint for dev API keys).Enable debug logging to console. Logs all span events and export payloads.
submitFeedback
Submit user feedback for a specific run.Usage Patterns
Basic Configuration
Minimal setup with environment variable for API key:With Explicit API Key
Provide the API key directly in configuration:With Custom Run IDs
Attach custom run IDs using Mastra’s metadata:If
tcc.runId is not provided in metadata, a random UUID will be generated automatically.With Additional Metadata
Attach contextual metadata to your runs:All metadata fields (except
tcc.runId) are forwarded to The Context Company and appear in the dashboard.With Debug Logging
Enable detailed logging for troubleshooting:Multiple Exporters
Use TCC alongside other exporters:How It Works
- Span Collection: The exporter implements Mastra’s
AITracingExporterinterface and listens to all tracing events - Event Types: It handles three event types:
SPAN_STARTED: When a span begins (agent call, LLM call, tool call)SPAN_UPDATED: When span data changesSPAN_ENDED: When a span completes
- Batching: Spans are collected in memory and grouped by trace ID
- Root Span Detection: The exporter waits for the root span (top-level agent call) to complete
- Batch Export: When the root span ends, all collected spans for that trace are sent to TCC in a single batch
- Metadata Extraction: Custom run IDs and metadata from the root span are attached to all spans in the trace
Captured Data
The exporter automatically captures:- Agent invocations
- LLM calls (prompts, responses, tokens, costs)
- Tool calls (arguments, results)
- Span timing (start/end times)
- Span hierarchy (parent-child relationships)
- Custom metadata from the root span
- Error states and exceptions
Environment Variables
Custom ingestion endpoint URL. Overrides the default endpoint.
Graceful Shutdown
The exporter provides ashutdown method to ensure all traces are exported before your application exits:
Type Safety
The exporter is fully typed and compatible with Mastra’s TypeScript types:Next Steps
Mastra Documentation
Learn more about Mastra AI framework
View Traces
View your traces in the dashboard
