langsmith-trace
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseLangSmith Trace
LangSmith 追踪
Add tracing to your agent and query traces for debugging. Supports Python and TypeScript.
[!IMPORTANT] This skill is tuned for AgentSeek template backends (LangGraph + middleware stacks). For general LangSmith concepts, see the upstream langsmith-skills repo.
[!CAUTION] Never passas a CLI flag or expose API keys in shell commands / tool calls. The CLI reads--api-keyfrom the environment automatically. UsingLANGSMITH_API_KEYleaks secrets into shell history, process listings, and agent tool-call logs. Always rely on the environment variable set in your shell profile or--api-key <value>file..env
为你的Agent添加追踪功能,并通过查询追踪记录进行调试。支持Python和TypeScript。
[!IMPORTANT] 此技能专为AgentSeek模板后端(LangGraph + 中间件栈)优化。如需了解通用LangSmith概念,请查看上游的langsmith-skills仓库。
[!CAUTION] 切勿将作为CLI参数传递,也不要在shell命令/工具调用中暴露API密钥。 CLI会自动从环境变量中读取--api-key。使用LANGSMITH_API_KEY会将密钥泄露到shell历史记录、进程列表和Agent工具调用日志中。请始终依赖shell配置文件或--api-key <value>文件中设置的环境变量。.env
Installation & Setup
安装与配置
1. Install the CLI
1. 安装CLI
bash
curl -sSL https://raw.githubusercontent.com/langchain-ai/langsmith-cli/main/scripts/install.sh | shThe binary installs to . If is not found after install, add to your shell profile:
~/.local/bin/langsmithlangsmithbash
echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.zshrc
source ~/.zshrcbash
curl -sSL https://raw.githubusercontent.com/langchain-ai/langsmith-cli/main/scripts/install.sh | sh二进制文件将安装到。如果安装后找不到命令,请将其添加到你的shell配置文件中:
~/.local/bin/langsmithlangsmithbash
echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.zshrc
source ~/.zshrc2. Set your API key (environment variable only)
2. 设置API密钥(仅通过环境变量)
Add to (or the template's file) — the user must fill in their own key:
~/.zshrc.envbash
export LANGSMITH_API_KEY=<your-key-here> # starts with lsv2_pt_Key format must start with . Get yours at https://smith.langchain.com/settings.
lsv2_pt_The CLI reads this env var automatically — never use flags. Do not echo, print, or pass the key value in any shell command or tool call argument.
--api-key将以下内容添加到(或模板的文件)中——用户需自行填写密钥:
~/.zshrc.envbash
export LANGSMITH_API_KEY=<your-key-here> # 以lsv2_pt_开头密钥格式必须以开头。可前往https://smith.langchain.com/settings获取你的密钥。
lsv2_pt_CLI会自动读取此环境变量——切勿使用参数。 不要在任何shell命令或工具调用参数中回显、打印或传递密钥值。
--api-key3. Verify
3. 验证
bash
langsmith project listIf you see a JSON array of projects, you're set. Common failures:
- —
command not foundnot in PATH (see step 1)~/.local/bin - 401 Unauthorized — key is wrong or expired; regenerate at LangSmith settings
- Empty array — valid auth but no projects yet; create one in the UI or run a traced app
[]
bash
langsmith project list如果看到项目的JSON数组,则配置完成。常见失败原因:
- ——
command not found未加入PATH(请查看步骤1)~/.local/bin - 401 Unauthorized —— 密钥错误或过期;请在LangSmith设置页面重新生成
- 空数组—— 认证有效但暂无项目;可在UI中创建一个,或运行已添加追踪的应用
[]
Adding Tracing
添加追踪功能
LangGraph / LangChain apps (automatic)
LangGraph / LangChain应用(自动追踪)
Just set environment variables — no code changes needed:
bash
export LANGSMITH_TRACING=true
export LANGSMITH_API_KEY=<your-key-here> # must be set; do NOT pass via --api-key flag
export LANGSMITH_PROJECT=my-project # optional, defaults to "default"For serverless (Python): also set to ensure traces flush before function exit.
LANGCHAIN_CALLBACKS_BACKGROUND=false只需设置环境变量——无需修改代码:
bash
export LANGSMITH_TRACING=true
export LANGSMITH_API_KEY=<your-key-here> # 必须设置;请勿通过--api-key参数传递
export LANGSMITH_PROJECT=my-project # 可选,默认值为"default"对于无服务器(Python)场景:还需设置,确保在函数退出前完成追踪记录的刷新。
LANGCHAIN_CALLBACKS_BACKGROUND=falseNon-LangChain apps
非LangChain应用
Use the decorator (Python) or wrapper (TypeScript) and wrap your LLM client:
@traceabletraceable()python
from langsmith import traceable
from langsmith.wrappers import wrap_openai
from openai import OpenAI
client = wrap_openai(OpenAI())
@traceable
def my_pipeline(question: str) -> str:
resp = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": question}],
)
return resp.choices[0].message.contenttypescript
import { traceable } from "langsmith/traceable";
import { wrapOpenAI } from "langsmith/wrappers";
import OpenAI from "openai";
const client = wrapOpenAI(new OpenAI());
const myPipeline = traceable(async (question: string) => {
const resp = await client.chat.completions.create({
model: "gpt-4o-mini",
messages: [{ role: "user", content: question }],
});
return resp.choices[0].message.content || "";
}, { name: "my_pipeline" });使用装饰器(Python)或包装器(TypeScript)来包裹你的LLM客户端:
@traceabletraceable()python
from langsmith import traceable
from langsmith.wrappers import wrap_openai
from openai import OpenAI
client = wrap_openai(OpenAI())
@traceable
def my_pipeline(question: str) -> str:
resp = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": question}],
)
return resp.choices[0].message.contenttypescript
import { traceable } from "langsmith/traceable";
import { wrapOpenAI } from "langsmith/wrappers";
import OpenAI from "openai";
const client = wrapOpenAI(new OpenAI());
const myPipeline = traceable(async (question: string) => {
const resp = await client.chat.completions.create({
model: "gpt-4o-mini",
messages: [{ role: "user", content: question }],
});
return resp.choices[0].message.content || "";
}, { name: "my_pipeline" });Debugging a Trace (Step-by-Step)
追踪记录调试(分步指南)
This is the recommended workflow for investigating agent behavior:
以下是排查Agent行为的推荐工作流:
Step 1: Find the right project
步骤1:找到目标项目
bash
langsmith project listLook at to find which project has recent activity. Don't assume — LangGraph apps default to the project, not a named one.
last_run_start_time"default"bash
langsmith project list查看以找到有近期活动的项目。不要想当然——LangGraph应用默认会追踪到项目,而非命名项目。
last_run_start_time"default"Step 2: List recent traces
步骤2:列出近期追踪记录
bash
langsmith trace list --project default --limit 5bash
langsmith trace list --project default --limit 5Or with full hierarchy inline (combines steps 2+3):
或直接显示完整层级结构(合并步骤2+3):
langsmith trace list --project default --limit 5 --show-hierarchy
undefinedlangsmith trace list --project default --limit 5 --show-hierarchy
undefinedStep 3: Get the trace hierarchy
步骤3:获取追踪层级结构
bash
langsmith trace get <trace-id> --project <name>This returns the full run tree — use it to understand the agent's execution flow.
bash
langsmith trace get <trace-id> --project <name>此命令会返回完整的运行树——可用于理解Agent的执行流程。
Step 4: Get IO for all runs in a trace
步骤4:获取追踪中所有运行的IO数据
bash
langsmith run list --trace-ids <trace-id> --project <name> --include-ioThis gives you inputs and outputs for every run (LLM calls, tool calls, middleware).
bash
langsmith run list --trace-ids <trace-id> --project <name> --include-io此命令会返回每个运行(LLM调用、工具调用、中间件)的输入和输出数据。
Step 5: Drill into a specific run
步骤5:深入查看特定运行
bash
langsmith run get <run-id> --include-iobash
langsmith run get <run-id> --include-ioAgentSeek Trace Structure
AgentSeek追踪记录结构
Our templates produce traces with this typical hierarchy:
<agent_name> (root chain)
├── SkillsMiddleware.before_agent
├── PatchToolCallsMiddleware.before_agent
├── MemoryMiddleware.before_agent
├── model (chain) ← LLM turn
│ ├── TodoListMiddleware.awrap_model_call
│ ├── SkillsMiddleware.awrap_model_call
│ ├── FilesystemMiddleware.awrap_model_call
│ ├── SubAgentMiddleware.awrap_model_call
│ ├── SummarizationMiddleware.awrap_model_call
│ ├── AnthropicPromptCachingMiddleware.awrap_model_call
│ ├── MemoryMiddleware.awrap_model_call
│ └── ChatOpenAI (llm) ← actual LLM call (inputs/outputs here)
├── TodoListMiddleware.after_model
├── tools (chain) ← tool execution
│ ├── FilesystemMiddleware.awrap_tool_call
│ └── <tool_name> (tool) ← actual tool (inputs/outputs here)
├── model (chain) ← next LLM turn
│ └── ... (same middleware stack)
└── TodoListMiddleware.after_modelKey points:
- The actual LLM call is always the innermost run
ChatOpenAI - Tool results are in the run (e.g.,
<tool_name>,generate_cover)execute - Middleware wrappers are transparent — they add latency but the IO you care about is at the leaf nodes
我们的模板生成的追踪记录通常具有以下层级结构:
<agent_name> (根链)
├── SkillsMiddleware.before_agent
├── PatchToolCallsMiddleware.before_agent
├── MemoryMiddleware.before_agent
├── model (chain) ← LLM轮次
│ ├── TodoListMiddleware.awrap_model_call
│ ├── SkillsMiddleware.awrap_model_call
│ ├── FilesystemMiddleware.awrap_model_call
│ ├── SubAgentMiddleware.awrap_model_call
│ ├── SummarizationMiddleware.awrap_model_call
│ ├── AnthropicPromptCachingMiddleware.awrap_model_call
│ ├── MemoryMiddleware.awrap_model_call
│ └── ChatOpenAI (llm) ← 实际LLM调用(输入/输出在此处)
├── TodoListMiddleware.after_model
├── tools (chain) ← 工具执行
│ ├── FilesystemMiddleware.awrap_tool_call
│ └── <tool_name> (tool) ← 实际工具(输入/输出在此处)
├── model (chain) ← 下一个LLM轮次
│ └── ...(相同的中间件栈)
└── TodoListMiddleware.after_model关键点:
- 实际LLM调用始终是最内层的运行
ChatOpenAI - 工具结果位于运行中(例如
<tool_name>、generate_cover)execute - 中间件包装器是透明的——它们会增加延迟,但你关心的IO数据在叶子节点中
Gotchas
常见陷阱
--full
vs --include-io
on individual runs
--full--include-io单个运行中的--full
与--include-io
差异
--full--include-ioProblem: can return null for inputs/outputs, even though the data exists. Despite being documented as equivalent to , the underlying API behavior differs for individual run fetches in some CLI versions.
langsmith run get <id> --full--full--include-metadata --include-io --include-feedbackSolution: Always use explicitly when inspecting specific runs:
--include-iobash
undefined问题: 可能会返回输入/输出为空值,即使数据实际存在。尽管文档说明等同于,但在某些CLI版本中,底层API在获取单个运行时的行为有所不同。
langsmith run get <id> --full--full--include-metadata --include-io --include-feedback解决方案: 查看特定运行时,请始终显式使用:
--include-iobash
undefinedDO THIS
推荐用法
langsmith run get <run-id> --include-io
langsmith run get <run-id> --include-io
NOT THIS (may return null IO despite docs saying it includes --include-io)
不推荐(尽管文档说明包含--include-io,但可能返回空IO)
langsmith run get <run-id> --full
`--full` works reliably on `trace export` and `run list`, but has inconsistent behavior on individual `run get` calls. If this is fixed in a future CLI version, `--include-io` still works correctly — it's always safe.langsmith run get <run-id> --full
`--full`在`trace export`和`run list`中能可靠工作,但在单个`run get`调用中行为不一致。如果未来CLI版本修复了此问题,`--include-io`仍能正常工作——始终使用它是安全的。Null IO even with --include-io
--include-io使用--include-io
仍返回空IO
--include-ioIf inputs/outputs come back null even with , the project has IO logging disabled. Check for these environment variables in the template's :
--include-io.envbash
LANGCHAIN_HIDE_INPUTS=true # hides inputs from traces
LANGCHAIN_HIDE_OUTPUTS=true # hides outputs from tracesRemove or set to to enable IO capture for debugging.
false如果使用后输入/输出仍为空,说明项目已禁用IO日志记录。请检查模板文件中的以下环境变量:
--include-io.envbash
LANGCHAIN_HIDE_INPUTS=true # 在追踪记录中隐藏输入
LANGCHAIN_HIDE_OUTPUTS=true # 在追踪记录中隐藏输出删除这些变量或设置为,以启用调试所需的IO捕获功能。
falseProject confusion
项目混淆
LangGraph apps trace to the project unless is explicitly set. Always run first and check to find where your traces actually landed.
"default"LANGSMITH_PROJECTlangsmith project listlast_run_start_time除非显式设置,否则LangGraph应用会将追踪记录发送到项目。请始终先运行,并查看以确认追踪记录实际所在的项目。
LANGSMITH_PROJECT"default"langsmith project listlast_run_start_timeTips
提示
- Add for human-readable output during interactive debugging
--format pretty - Use env var if connecting to a self-hosted LangSmith instance
LANGSMITH_ENDPOINT - The middleware ordering in the trace tree is configurable — your template may differ slightly from the diagram above
- 在交互式调试时,添加参数以获得人类可读的输出
--format pretty - 如果连接到自托管的LangSmith实例,请使用环境变量
LANGSMITH_ENDPOINT - 追踪树中的中间件顺序是可配置的——你的模板可能与上述示意图略有不同
CLI Reference
CLI参考
Full command reference: reference/cli-commands.md
完整命令参考:reference/cli-commands.md