BraintrustExporter
Sends Tracing data to Braintrust for eval and observability.
ConstructorDirect link to Constructor
new BraintrustExporter(config: BraintrustExporterConfig)
BraintrustExporterConfigDirect link to braintrustexporterconfig
interface BraintrustExporterConfig extends TrackingExporterConfig {
apiKey?: string
endpoint?: string
projectName?: string
braintrustLogger?: BraintrustLogger
currentSpan?: () => BraintrustSpan | undefined
tuningParameters?: Record<string, any>
}
Extends TrackingExporterConfig, which builds on BaseExporterConfig and includes:
logger?: IMastraLogger- Logger instancelogLevel?: LogLevel | 'debug' | 'info' | 'warn' | 'error'- Log level (default: INFO)earlyQueueMaxAttempts?: number- Attempts to process a queued event before dropping it (default: 5)earlyQueueTTLMs?: number- Maximum age in milliseconds of a queued event (default: 30000). The age is checked when the queue is replayed. An older event is dropped at the next replay or trace cleanup.traceCleanupDelayMs?: number- Milliseconds to keep trace data after all its spans end, so late data can still be processed (default: 30000)maxPendingCleanupTraces?: number- Soft cap on finished traces awaiting cleanup; the oldest are cleaned first when exceeded (default: 100)maxTotalTraces?: number- Hard cap on tracked traces, including active ones; the oldest are aborted when exceeded (default: 500)
MethodsDirect link to Methods
exportTracingEventDirect link to exporttracingevent
async exportTracingEvent(event: TracingEvent): Promise<void>
Exports a tracing event to Braintrust.
exportDirect link to export
async export(spans: ReadOnlySpan[]): Promise<void>
Batch exports spans to Braintrust.
flushDirect link to flush
async flush(): Promise<void>
Force flushes any buffered spans to Braintrust without shutting down the exporter. Useful in serverless environments where you need to ensure spans are exported before the runtime terminates.
shutdownDirect link to shutdown
async shutdown(): Promise<void>
Flushes pending data and shuts down the client.
UsageDirect link to Usage
Zero-config (using environment variables)Direct link to Zero-config (using environment variables)
import { BraintrustExporter } from '@mastra/braintrust'
// Reads from BRAINTRUST_API_KEY, BRAINTRUST_ENDPOINT
const exporter = new BraintrustExporter()
Explicit configurationDirect link to Explicit configuration
import { BraintrustExporter } from '@mastra/braintrust'
const exporter = new BraintrustExporter({
apiKey: process.env.BRAINTRUST_API_KEY,
projectName: 'my-ai-project',
})
Span type mappingDirect link to Span type mapping
| Span Type | Braintrust Type |
|---|---|
MODEL_GENERATION | llm |
MODEL_CHUNK | task |
TOOL_CALL | tool |
MCP_TOOL_CALL | tool |
PROVIDER_TOOL_CALL | tool |
WORKFLOW_CONDITIONAL_EVAL | function |
WORKFLOW_WAIT_EVENT | function |
| All others | task |
Cost trackingDirect link to Cost tracking
Braintrust uses a span's metrics.estimated_cost as its cost when the field is set, and estimates the cost from token metrics and its own model prices when it isn't. The exporter sets metrics.estimated_cost on a model generation span only when the generation's cost metadata meets every condition:
costContext.costMetadata.sourceis'provider_reported', meaning the model provider returned the cost, as OpenRouter and Vercel AI Gateway do.costContext.costUnitis'USD'.costContext.estimatedCostis a finite number greater than0.
Mastra's own cost estimates aren't exported, so Braintrust prices those spans with its model prices. A reported cost of 0 is skipped as well. Vercel AI Gateway reports 0 for bring-your-own-key requests, which your model provider still bills, and Braintrust's estimate is closer to that charge. The span metadata keeps the full costContext in every case.