该 消息视图 将代理的跟踪渲染为聊天式对话:用户提示、模型响应、工具调用和工具结果,按顺序显示。它自动适用于本页表格中列出的任何集成。部分集成(wrap_anthropic 单独使用,以及 JavaScript Claude Agent SDK)需要手动设置一个元数据键,这些内容在表格和 已知限制.
支持的集成
| 集成 | 追踪 SDK | 所需设置 |
|---|---|---|
| LangChain 聊天模型 | langchain-core | 无 |
| LangGraph | langgraph | 无 |
langchain.create_agent | langchain | 无(见 已知限制) |
| Deep Agents | deepagents | 无 |
| OpenAI 聊天补全 | wrap_openai | 无 |
| OpenAI Responses | wrap_openai (responses API) | 无 |
| OpenAI Agents SDK | Python 追踪处理器 | 无 |
| Vercel AI SDK | wrapAISDK | 无 |
Anthropic 消息(wrap_anthropic) | wrap_anthropic | 设置 |
| Claude Agent SDK (Python) | claude-agent-sdk | 无 |
| Claude Agent SDK (JS) | claude-agent-sdk-js | 设置 |
| Claude Code | claude-code | 无 |
有关每个集成的完整检测规则、预期负载格式和 JSON 示例,请参阅 追踪格式参考.
已知限制
部分集成需要元数据覆盖才能被识别:
- - **
wrap_anthropicalone:** 该包装器未设置ls_message_format,因此当前检测不匹配。设置metadata={"ls_message_format": "anthropic"}在调用时(通过RunnableConfig)以声明该运行。 - Claude Agent SDK (JS): 自动检测当前仅允许
"claude-agent-sdk"和"claude-code",而非 JS 发出的"claude-agent-sdk-js"。设置ls_message_format: "anthropic"在 JS 追踪上。 - - **
langchain.create_agent:** 不在显式检测允许列表中。当代理在 LangGraph 内运行时,它通过ls_provider/ls_message_formatfallthroughs in the matching OpenAI/Anthropic detection, or viagraph_id/langgraph_node来声明。如果路由不可靠,请设置metadata.ls_message_format: "langchain"explicitly.
从消息视图排除运行
设置 LS_MESSAGE_VIEW_EXCLUDE 在运行的元数据上会指示消息视图跳过该运行。关键在于该键的存在; True 是常规值。过滤器在任何提取策略之前运行,因此被排除的 LLM 或工具运行不会影响检测、消息提取或工具调用配对。
LS_MESSAGE_VIEW_EXCLUDE 是从导出的顶级常量 langsmith (Python 和 JS),其值为字符串 "ls_message_view_exclude"。建议使用该常量以避免拼写错误;字面字符串仍然有效。
Use it for LLM subspans that are not conversational turns, such as classification calls, embedding lookups, safety filters, or routing/guardrail decisions, that you still want visible elsewhere in LangSmith but do not want cluttering the conversation transcript.
Python
**1. On a @traceable 装饰器**:排除整个函数的运行。
from langsmith import LS_MESSAGE_VIEW_EXCLUDE, traceable
@traceable(run_type="llm", metadata={LS_MESSAGE_VIEW_EXCLUDE: True})
def classify_intent(query: str) -> str:
# This LLM call is internal routing, not part of the chat
return llm.predict(f"Classify the intent of: {query}")
**2. 通过 trace 上下文管理器**:排除一个临时 span。
from langsmith import LS_MESSAGE_VIEW_EXCLUDE, trace
with trace(
"safety_check",
run_type="llm",
metadata={LS_MESSAGE_VIEW_EXCLUDE: True},
) as run:
result = safety_model.score(text)
run.end(outputs={"score": result})
3. 从正在运行的函数内部:在运行被修补之前的任何时间点,在当前运行树中设置密钥。
from langsmith import LS_MESSAGE_VIEW_EXCLUDE, get_current_run_tree, traceable
@traceable(run_type="llm")
def maybe_internal(query: str) -> str:
result = llm.predict(query)
if _looks_like_routing(query):
rt = get_current_run_tree()
if rt is not None:
rt.add_metadata({LS_MESSAGE_VIEW_EXCLUDE: True})
return result
**4. 每次调用时使用 wrap_openai / wrap_anthropic**:传递 langsmith_extra 到被包装的客户端调用中。
from langsmith import LS_MESSAGE_VIEW_EXCLUDE
from langsmith.wrappers import wrap_openai
client = wrap_openai(openai.Client())
resp = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Classify: ..."}],
langsmith_extra={"metadata": {LS_MESSAGE_VIEW_EXCLUDE: True}},
)
**5. LangChain RunnableConfig**:排除链或聊天模型的单个调用。
from langchain_openai import ChatOpenAI
from langsmith import LS_MESSAGE_VIEW_EXCLUDE
llm = ChatOpenAI(model="gpt-4o")
result = llm.invoke(
"Classify this query",
config={"metadata": {LS_MESSAGE_VIEW_EXCLUDE: True}},
)
TypeScript
**1. On a traceable 包装器**:排除整个函数的运行。
const classifyIntent = traceable(
async (query: string) => {
return await llm.predict(`Classify the intent of: ${query}`);
},
{
name: "classify_intent",
run_type: "llm",
metadata: { [LS_MESSAGE_VIEW_EXCLUDE]: true },
},
);
2. 从正在运行的函数内部:修改当前运行树。
const maybeInternal = traceable(
async (query: string) => {
const result = await llm.predict(query);
if (looksLikeRouting(query)) {
const rt = getCurrentRunTree();
rt.extra = rt.extra ?? {};
rt.extra.metadata = { ...rt.extra.metadata, [LS_MESSAGE_VIEW_EXCLUDE]: true };
}
return result;
},
{ run_type: "llm" },
);
**3. 每次调用时使用 wrapOpenAI**:传递 langsmithExtra 在调用上。
const client = wrapOpenAI(new OpenAI());
const resp = await client.chat.completions.create(
{
model: "gpt-4o-mini",
messages: [{ role: "user", content: "Classify: ..." }],
},
{ langsmithExtra: { metadata: { [LS_MESSAGE_VIEW_EXCLUDE]: true } } },
);
4. Vercel AI SDK 中间件:通过以下方式传递密钥 lsConfig.metadata on wrapAISDK。中间件将此合并到每个发出的 LLM 运行中。
const { generateText } = wrapAISDK(ai, {
metadata: { [LS_MESSAGE_VIEW_EXCLUDE]: true },
});
要仅排除某些调用而不是其他调用,请用 wrapAISDK 进行正常包装,然后修改 getCurrentRunTree() 从父级内部 traceable 调用 AI SDK 的父级,或使用子级 RunTree 使用 createChild({ extra: { metadata: { [LS_MESSAGE_VIEW_EXCLUDE]: true } } }).
**5. 手动 RunTree.createChild**:当你手动构建运行时。
const parent = new RunTree({ name: "agent", run_type: "chain" });
const child = parent.createChild({
name: "safety_check",
run_type: "llm",
extra: { metadata: { [LS_MESSAGE_VIEW_EXCLUDE]: true } },
});
注意事项
- - 过滤器检查的是 **键的存在**,而不是真值性。
{LS_MESSAGE_VIEW_EXCLUDE: false}仍然排除该运行。完全省略该键以包含该运行。 - - 在
@traceable(Python) 或traceable(JS) 父级中执行的子运行通过共享追踪上下文继承排除规则:Python 的_METADATAContextVar和 JS 的AsyncLocalStorage。子级自己的装饰器时元数据层叠在继承值之上。 - - 被排除的运行仍然会出现在常规追踪视图、运行浏览器和指标中。只有消息视图会过滤掉它们。
手动插桩
如果你在没有以下包装器的情况下进行追踪 支持的集成——例如,通过 RunTree、REST API 或围绕提供商 SDK 的自定义包装器发出运行——在每个 LLM 运行的元数据上设置 ls_message_format 以将追踪路由到正确的提取器:
| 追踪形状 | 在元数据上设置 |
|---|---|
| LangChain 消息(构造函数包装器) | ls_message_format: "langchain" |
| OpenAI 聊天补全 | ls_message_format: "completions" |
| OpenAI 响应 API | ls_message_format: "responses" |
| Anthropic 消息 API | ls_message_format: "anthropic" |
有关每个提取器期望的JSON结构,请参阅 跟踪格式参考.