跳到主要内容
非官方简体中文翻译,内容版权归 Mastra 所有,遵循 Apache-2.0 许可。

LangfuseExporter

Sends Tracing data to Langfuse for observability.

Constructor
Constructor的直接链接

new LangfuseExporter(config: LangfuseExporterConfig)

LangfuseExporterConfig
langfuseexporterconfig的直接链接

interface LangfuseExporterConfig extends BaseExporterConfig {
publicKey?: string
secretKey?: string
baseUrl?: string
additionalHeaders?: Record<string, string>
realtime?: boolean
flushAt?: number
flushInterval?: number
environment?: string
release?: string
}

Extends BaseExporterConfig, which includes:

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

Properties
Properties的直接链接

client
client的直接链接

get client(): LangfuseClient | undefined

The LangfuseClient instance for advanced Langfuse features. Returns undefined when the exporter is disabled (missing credentials). Use this for prompt management, evaluations, datasets, and direct API access.

Methods
Methods的直接链接

flush
flush的直接链接

async flush(): Promise<void>

Force flushes any buffered spans and scores to Langfuse without shutting down. Useful in serverless environments where you need to ensure data is exported before the runtime terminates.

shutdown
shutdown的直接链接

async shutdown(): Promise<void>

Flushes pending data and shuts down both the span processor and the client.

Usage
Usage的直接链接

Zero-Config (using environment variables)
Zero-Config (using environment variables)的直接链接

import { LangfuseExporter } from '@mastra/langfuse'

// Reads from LANGFUSE_PUBLIC_KEY, LANGFUSE_SECRET_KEY, LANGFUSE_BASE_URL
const exporter = new LangfuseExporter()

Explicit Configuration
Explicit Configuration的直接链接

import { LangfuseExporter } from '@mastra/langfuse'

const exporter = new LangfuseExporter({
publicKey: process.env.LANGFUSE_PUBLIC_KEY,
secretKey: process.env.LANGFUSE_SECRET_KEY,
baseUrl: 'https://cloud.langfuse.com',
additionalHeaders: { 'x-custom-header': 'custom-value' },
realtime: true,
})

Span mapping
Span mapping的直接链接

Mastra spans are converted to OTel format and sent to Langfuse via LangfuseSpanProcessor. Langfuse automatically maps spans based on their gen_ai.* attributes:

  • Spans with a model attribute → Langfuse generations
  • All other spans → Langfuse spans
  • Parent-child relationships are preserved via OTel trace/span IDs

Prompt linking
Prompt linking的直接链接

Link LLM generations to Langfuse Prompt Management using the withLangfusePrompt helper:

import { buildTracingOptions } from '@mastra/observability'
import { LangfuseExporter, withLangfusePrompt } from '@mastra/langfuse'

const exporter = new LangfuseExporter()

// Fetch prompt via the Langfuse client
const prompt = await exporter.client.prompt.get('customer-support', { type: 'text' })

const agent = new Agent({
id: 'support-agent',
name: 'support-agent',
instructions: prompt.compile(),
model: 'openai/gpt-5.6-sol',
defaultGenerateOptions: {
tracingOptions: buildTracingOptions(
withLangfusePrompt({ name: prompt.name, version: prompt.version }),
),
},
})

Helper Functions
Helper Functions的直接链接

withLangfusePrompt(prompt)
withlangfusepromptprompt的直接链接

Adds Langfuse prompt metadata to tracing options.

// Link by name and version (required for Langfuse v5)
withLangfusePrompt({ name: 'my-prompt', version: 1 })

The prompt metadata is passed through as span attributes and Langfuse links the generation to the corresponding prompt.