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

LangSmith

LangSmith is LangChain's platform for monitoring and evaluating LLM applications. The LangSmith exporter sends your traces to LangSmith, providing insights into model performance and debugging capabilities, plus evaluation workflows.

Installation
Installation的直接链接

npm install @mastra/langsmith@latest

Configuration
Configuration的直接链接

Prerequisites
Prerequisites的直接链接

  1. LangSmith Account: Sign up at smith.langchain.com
  2. API Key: Generate an API key in LangSmith Settings → API Keys
  3. Environment Variables: Set your credentials
.env
# Required
LANGSMITH_API_KEY=ls-xxxxxxxxxxxx

# Optional
LANGCHAIN_PROJECT=my-project # Default project for traces
LANGSMITH_BASE_URL=https://api.smith.langchain.com # For self-hosted

Zero-Config Setup
Zero-Config Setup的直接链接

With environment variables set, use the exporter with no configuration:

src/mastra/index.ts
import { Mastra } from '@mastra/core'
import { Observability } from '@mastra/observability'
import { LangSmithExporter } from '@mastra/langsmith'

export const mastra = new Mastra({
observability: new Observability({
configs: {
langsmith: {
serviceName: 'my-service',
exporters: [new LangSmithExporter()],
},
},
}),
})

Explicit Configuration
Explicit Configuration的直接链接

You can also pass credentials directly (takes precedence over environment variables):

src/mastra/index.ts
import { Mastra } from '@mastra/core'
import { Observability } from '@mastra/observability'
import { LangSmithExporter } from '@mastra/langsmith'

export const mastra = new Mastra({
observability: new Observability({
configs: {
langsmith: {
serviceName: 'my-service',
exporters: [
new LangSmithExporter({
apiKey: process.env.LANGSMITH_API_KEY,
}),
],
},
},
}),
})

Configuration options
Configuration options的直接链接

Complete Configuration
Complete Configuration的直接链接

new LangSmithExporter({
// Required credentials
apiKey: process.env.LANGSMITH_API_KEY!,

// Optional settings
apiUrl: process.env.LANGSMITH_BASE_URL, // Default: https://api.smith.langchain.com
projectName: 'my-project', // Project to send traces to (overrides LANGCHAIN_PROJECT env var)
callerOptions: {
// HTTP client options
timeout: 30000, // Request timeout in ms
maxRetries: 3, // Retry attempts
},
logLevel: 'info', // Diagnostic logging: debug | info | warn | error

// LangSmith-specific options
hideInputs: false, // Hide input data in UI
hideOutputs: false, // Hide output data in UI
})

Environment variables
Environment variables的直接链接

VariableDescription
LANGSMITH_API_KEYYour LangSmith API key (required)
LANGCHAIN_PROJECTDefault project name for traces (optional, defaults to "default")
LANGSMITH_BASE_URLAPI URL for self-hosted instances (optional)

The projectName config option takes precedence over the LANGCHAIN_PROJECT environment variable, allowing you to programmatically route traces to different projects.

Dynamic configuration
Dynamic configuration的直接链接

You can override at runtime LangSmith settings per-span using withLangsmithMetadata. This is useful for routing traces to different projects based on runtime conditions (e.g., customer, environment, or feature).

Using the Helper
Using the Helper的直接链接

Use withLangsmithMetadata with buildTracingOptions to set LangSmith-specific options:

src/agents/support-agent.ts
import { Agent } from '@mastra/core/agent'
import { buildTracingOptions } from '@mastra/observability'
import { withLangsmithMetadata } from '@mastra/langsmith'

export const supportAgent = new Agent({
id: 'support-agent',
name: 'support-agent',
instructions: 'You are a helpful support agent.',
model: 'openai/gpt-5.6-sol',
defaultOptions: {
tracingOptions: buildTracingOptions(withLangsmithMetadata({ projectName: 'customer-support' })),
},
})

Dynamic Project Routing
Dynamic Project Routing的直接链接

Use requestContext to route traces to different projects based on runtime conditions.

src/mastra/agents/support-agent.ts
import { Agent } from '@mastra/core/agent'
import { buildTracingOptions } from '@mastra/observability'
import { withLangsmithMetadata } from '@mastra/langsmith'

export const supportAgent = new Agent({
id: 'support-agent',
name: 'support-agent',
instructions: 'You are a helpful support agent.',
model: 'openai/gpt-5.6-sol',
defaultOptions: ({ requestContext }) => {
const userTier = requestContext?.get('user-tier') as string
const userId = requestContext?.get('user-id') as string

return {
tracingOptions: buildTracingOptions(
withLangsmithMetadata({
projectName: userTier === 'enterprise' ? 'enterprise-traces' : 'standard-traces',
sessionId: userId,
}),
),
}
},
})

Available Fields
Available Fields的直接链接

The withLangsmithMetadata helper accepts these fields:

FieldTypeDescription
projectNamestringOverride the project for this trace
sessionIdstringGroup related traces by session
sessionNamestringDisplay name for the session

All fields are optional. The helper merges with any existing metadata, so you can call it multiple times or combine with other tracing options.