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 john@example.com" }]
});
默认情况下,跟踪信息将被记录到名为 default 的项目中。要配置自定义项目名称,请参阅记录到项目