create_agent 构建的 LangChain 代理自动支持通过 LangSmith 进行跟踪,这是一个用于捕获、调试、评估和监控 LLM 应用程序行为的平台。
追踪记录 记录了代理执行的每个步骤,从初始用户输入到最终响应,包括所有工具调用、模型交互和决策点。这些执行数据帮助您调试问题、评估不同输入的性能,并监控生产环境中的使用模式。
本指南向您展示如何为 LangChain 代理启用跟踪并使用 LangSmith 分析其执行。
前置条件
在开始之前,请确保您具备以下条件:- LangSmith 账户:在 smith.langchain.com 注册(免费)或登录。
- LangSmith API 密钥:按照创建 API 密钥指南操作。
启用跟踪
所有 LangChain 代理自动支持 LangSmith 跟踪。要启用它,请设置以下环境变量:快速开始
无需额外代码即可将追踪记录到 LangSmith。只需像平常一样运行您的代理代码:default 的项目中。要配置自定义项目名称,请参阅记录到项目。
Trace selectively
You may opt to trace specific invocations or parts of your application using LangSmith’stracing_context context manager:
Log to a project
Statically
Statically
You can set a custom project name for your entire application by setting the
LANGSMITH_PROJECT environment variable:Dynamically
Dynamically
You can set the project name programmatically for specific operations:
Add metadata to traces
You can annotate your traces with custom metadata and tags:tracing_context also accepts tags and metadata for fine-grained control:
通过 MCP 将这些文档连接到 Claude、VSCode 等,获取实时答案。

