LangSmithExporter
Sends Tracing data to LangSmith for observability.
ConstructorDirect link to Constructor
new LangSmithExporter(config: LangSmithExporterConfig)
LangSmithExporterConfigDirect link to langsmithexporterconfig
interface LangSmithExporterConfig extends ClientConfig, BaseExporterConfig {
client?: Client
projectName?: string
}
Extends both ClientConfig (from LangSmith SDK) and BaseExporterConfig:
- From
BaseExporterConfig:logger?: IMastraLogger,logLevel?: LogLevel | 'debug' | 'info' | 'warn' | 'error' - From
ClientConfig:apiKey,apiUrl,callerOptions,hideInputs,hideOutputs, etc.
MethodsDirect link to Methods
exportTracingEventDirect link to exporttracingevent
async exportTracingEvent(event: TracingEvent): Promise<void>
Exports a tracing event to LangSmith.
flushDirect link to flush
async flush(): Promise<void>
Force flushes any pending spans to LangSmith without shutting down the exporter. Useful in serverless environments where you need to ensure spans are exported before the runtime terminates.
Waits for the LangSmith client's queued and in-flight batches. When the client uses manualFlushMode: true, this calls client.flush() for you, so you don't need to flush the client yourself. Overlapping calls wait for the earlier upload to finish.
shutdownDirect link to shutdown
async shutdown(): Promise<void>
Ends all active spans and clears the trace map. Spans that are still open are ended with a shutdown error, and their updates are sent to LangSmith before the promise resolves.
UsageDirect link to Usage
import { LangSmithExporter } from '@mastra/langsmith'
const exporter = new LangSmithExporter({
apiKey: process.env.LANGSMITH_API_KEY,
projectName: 'my-project', // Optional: specify which project to send traces to
apiUrl: 'https://api.smith.langchain.com',
logLevel: 'info',
})
Environment variablesDirect link to Environment variables
| Variable | Description |
|---|---|
LANGSMITH_API_KEY | Your LangSmith API key |
LANGCHAIN_PROJECT | Default project name for traces (used if projectName not specified) |
LANGSMITH_BASE_URL | API URL for self-hosted instances |
Span type mappingDirect link to Span type mapping
| Span Type | LangSmith Type |
|---|---|
MODEL_INFERENCE | llm |
MODEL_GENERATION | chain, or llm when it has usage that no MODEL_INFERENCE run reports |
TOOL_CALL | tool |
MCP_TOOL_CALL | tool |
PROVIDER_TOOL_CALL | tool |
| All others | chain |
Token usage is only set on llm runs, and each token is counted once. A MODEL_GENERATION run is counted for calls without a run of their own, as with generateLegacy() and streamLegacy(), or when excludeSpanTypes or spanFilter removes MODEL_INFERENCE spans.
The inputs.messages of a MODEL_INFERENCE run use the OpenAI Chat Completions format: the messages the generation started with, then each earlier call and its tool results.
langsmithRunIdForDirect link to langsmithrunidfor
function langsmithRunIdFor(spanId: string): string
Returns the LangSmith run id the exporter uses for a Mastra span id. Scores submitted with mastra.observability.addScore() are attached to these runs as feedback, so no in-process state is needed.
withLangsmithMetadataDirect link to withlangsmithmetadata
Helper function to add LangSmith-specific metadata to tracing options.
function withLangsmithMetadata(metadata: LangSmithMetadataInput): TracingOptionsUpdater
LangSmithMetadataInputDirect link to langsmithmetadatainput
UsageDirect link to Usage
import { buildTracingOptions } from '@mastra/observability'
import { withLangsmithMetadata } from '@mastra/langsmith'
// Route to a specific project
const tracingOptions = buildTracingOptions(withLangsmithMetadata({ projectName: 'my-project' }))
// Set multiple fields
const tracingOptions = buildTracingOptions(
withLangsmithMetadata({
projectName: 'my-project',
sessionId: 'user-123',
}),
)