Skip to main content
当你使用 LangChain 构建和运行智能体时,你需要了解它们的行为:它们调用了哪些工具、生成了什么提示词以及它们如何做出决策。使用 createAgent 构建的 LangChain 智能体通过 LangSmith 自动支持追踪,LangSmith 是一个用于捕获、调试、评估和监控 LLM 应用行为的平台。 追踪记录智能体执行的每一步,从初始用户输入到最终响应,包括所有工具调用、模型交互和决策点。这些执行数据帮助你调试问题、评估不同输入的性能,并监控生产中的使用模式。 本指南向你展示如何为 LangChain 智能体启用追踪并使用 LangSmith 分析其执行。

前提条件

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

启用追踪

所有 LangChain 智能体自动支持 LangSmith 追踪。要启用它,设置以下环境变量:

快速入门

无需额外代码即可将追踪记录到 LangSmith。只需像平常一样运行你的智能体代码:
默认情况下,追踪将记录到名为 default 的项目中。要配置自定义项目名称,请参阅记录到项目

Trace selectively

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

Log to a project

You can set a custom project name for your entire application by setting the LANGSMITH_PROJECT environment variable:
You can set the project name programmatically for specific operations:

Add metadata to traces

You can annotate your traces with custom metadata and tags:
This custom metadata and tags will be attached to the trace in LangSmith.
要了解更多关于 how to use traces to debug, evaluate, and monitor your agents, see the LangSmith documentation.