Skip to main content
可观测性对于理解智能体在生产环境中的行为至关重要。通过 LangChain 的 @[create_agent],您可以通过 LangSmith 获得内置的可观测性功能 - 这是一个用于追踪、调试、评估和监控 LLM 应用程序的强大平台。 追踪记录会捕获智能体执行的每个步骤,从初始用户输入到最终响应,包括所有工具调用、模型交互和决策点。这使您能够调试智能体、评估性能并监控使用情况。

先决条件

开始之前,请确保您具备以下条件:

启用追踪

所有 LangChain 智能体都自动支持 LangSmith 追踪。要启用此功能,请设置以下环境变量:
export LANGSMITH_TRACING=true
export LANGSMITH_API_KEY=<your-api-key>
您可以从 LangSmith 设置 获取 API 密钥。

快速开始

无需额外代码即可将追踪记录上传到 LangSmith。只需照常运行智能体代码:
import { createAgent } from "@langchain/agents";

function sendEmail(to: string, subject: string, body: string): string {
    // ... email sending logic
    return `Email sent to ${to}`;
}

function searchWeb(query: string): string {
    // ... web search logic
    return `Search results for: ${query}`;
}

const agent = createAgent({
    model: "openai:gpt-4o",
    tools: [sendEmail, searchWeb],
    systemPrompt: "You are a helpful assistant that can send emails and search the web."
});

// Run the agent - all steps will be traced automatically
const response = await agent.invoke({
    messages: [{ role: "user", content: "Search for the latest AI news and email a summary to [email protected]" }]
});
默认情况下,追踪记录会被记录到名为 default 的项目中。要配置自定义项目名称,请参阅 记录到项目

Trace selectively

You may opt to trace specific invocations or parts of your application using LangSmith’s tracing_context context manager:
import langsmith as ls

# This WILL be traced
with ls.tracing_context(enabled=True):
    agent.invoke({"messages": [{"role": "user", "content": "Send a test email to [email protected]"}]})

# This will NOT be traced (if LANGSMITH_TRACING is not set)
agent.invoke({"messages": [{"role": "user", "content": "Send another email"}]})

Log to a project

You can set a custom project name for your entire application by setting the LANGSMITH_PROJECT environment variable:
export LANGSMITH_PROJECT=my-agent-project
You can set the project name programmatically for specific operations:
import langsmith as ls

with ls.tracing_context(project_name="email-agent-test", enabled=True):
    response = agent.invoke({
        "messages": [{"role": "user", "content": "Send a welcome email"}]
    })

Add metadata to traces

You can annotate your traces with custom metadata and tags:
response = agent.invoke(
    {"messages": [{"role": "user", "content": "Send a welcome email"}]},
    config={
        "tags": ["production", "email-assistant", "v1.0"],
        "metadata": {
            "user_id": "user_123",
            "session_id": "session_456",
            "environment": "production"
        }
    }
)
tracing_context also accepts tags and metadata for fine-grained control:
with ls.tracing_context(
    project_name="email-agent-test",
    enabled=True,
    tags=["production", "email-assistant", "v1.0"],
    metadata={"user_id": "user_123", "session_id": "session_456", "environment": "production"}):
    response = agent.invoke(
        {"messages": [{"role": "user", "content": "Send a welcome email"}]}
    )
This custom metadata and tags will be attached to the trace in LangSmith.
To learn more about how to use traces to debug, evaluate, and monitor your agents, see the LangSmith documentation.

Connect these docs programmatically to Claude, VSCode, and more via MCP for real-time answers.