Skip to main content

BraintrustExporter

Sends Tracing data to Braintrust for eval and observability.

Constructor
Direct link to Constructor

new BraintrustExporter(config: BraintrustExporterConfig)

BraintrustExporterConfig
Direct link to braintrustexporterconfig

interface BraintrustExporterConfig extends BaseExporterConfig {
apiKey?: string
endpoint?: string
projectName?: string
braintrustLogger?: Logger<true>
currentSpan?: () => Span | undefined
tuningParameters?: Record<string, any>
}

Extends BaseExporterConfig, which includes:

  • logger?: IMastraLogger - Logger instance
  • logLevel?: LogLevel | 'debug' | 'info' | 'warn' | 'error' - Log level (default: INFO)

Methods
Direct link to Methods

exportTracingEvent
Direct link to exporttracingevent

async exportTracingEvent(event: TracingEvent): Promise<void>

Exports a tracing event to Braintrust.

export
Direct link to export

async export(spans: ReadOnlySpan[]): Promise<void>

Batch exports spans to Braintrust.

flush
Direct 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.

shutdown
Direct link to shutdown

async shutdown(): Promise<void>

Flushes pending data and shuts down the client.

Usage
Direct 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 Configuration
Direct 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 mapping
Direct link to Span type mapping

Span TypeBraintrust Type
MODEL_GENERATIONllm
MODEL_CHUNKllm
TOOL_CALLtool
MCP_TOOL_CALLtool
PROVIDER_TOOL_CALLtool
WORKFLOW_CONDITIONAL_EVALfunction
WORKFLOW_WAIT_EVENTfunction
All otherstask

Cost tracking
Direct 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.source is 'provider_reported', meaning the model provider returned the cost, as OpenRouter and Vercel AI Gateway do.
  • costContext.costUnit is 'USD'.
  • costContext.estimatedCost is a finite number greater than 0.

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.