使用 LangGraph agent observability 发送并验证事件
在受控应用程序边界使用 LangGraph agent observability,保持事件契约较小,并在构建聚合视图之前验证已知结果。
- 1
选择结果
状态智能体监控
- 2
定义合同
thread_id、invocation_id、graph_name 和调用状态
- 3
在结果处发出
通过受控包装或回调记录节点事件;本示例仅记录每次图调用。
- 4
核实证据
Exercise a known fixture, then inspect agent_graph_invocation for one correctly typed terminal row.
开始之前
开始之前
- @langchain/langgraph 和 @langchain/core
- 恢复检查点时保留不变的线程 ID
- 服务器端TELEMETRY_API_KEY
- 支持 fetch 和 AbortSignal.timeout 的服务端运行环境
交货设置
安装并初始化服务器端
此服务端示例通过 Log API 发送事件,使用服务端密钥和有时限的 fetch 请求;无需初始化 Telemetry SDK。 将摄取凭据保留在浏览器包、客户端可见的环境变量、源代码控制、日志和异常消息之外。
- 1准备一个具有有限网络行为的可重用服务器端交付客户端。
- 2在成功、失败、重试或超时边界处添加结果事件。
- 3在启用警报之前发送受控装置并检查存储的行。
片段
从一个结构化事件开始
在工作流程完成、失败或重试的位置添加此形状。然后从真实的字段构建仪表板。
LangGraph agent observability事件
async function emitGraphInvocation(data) {
try {
const delivery = await fetch("https://api.telemetry.sh/log", {
method: "POST",
headers: {
Authorization: process.env.TELEMETRY_API_KEY,
"Content-Type": "application/json",
},
body: JSON.stringify({ table: "agent_graph_invocation", data }),
signal: AbortSignal.timeout(1000),
});
if (!delivery.ok) throw new Error("telemetry_delivery_unconfirmed");
} catch {
try { console.warn("Telemetry delivery unconfirmed"); } catch {}
}
}
async function observedInvoke(graph, input, { threadId, config = {} }) {
const startedAt = performance.now();
const outcome = {
thread_id: threadId,
invocation_id: crypto.randomUUID(),
graph_name: "support_agent",
status: "failed",
release: process.env.APP_RELEASE,
};
try {
const result = await graph.invoke(input, {
...config,
configurable: { ...config.configurable, thread_id: threadId },
});
outcome.status = "returned";
// Metadata inspection must not replace the graph's result.
try {
const paused = Array.isArray(result?.__interrupt__) &&
result.__interrupt__.length > 0;
if (paused) outcome.status = "paused";
if (Array.isArray(result?.messages)) {
outcome.message_count = result.messages.length;
}
} catch {}
return result;
} finally {
outcome.duration_ms = Math.round(performance.now() - startedAt);
await emitGraphInvocation(outcome);
}
}
const threadId = crypto.randomUUID(); // Keep this ID when resuming this thread.
const result = await observedInvoke(graph, { messages }, { threadId });事件结构
thread_id、invocation_id、graph_name 和调用状态
tool_name、尝试、latency_ms 和 error_type
checkpoint_count、human_handoff、reviewer_outcome 和释放
检查配置
检查站 1
通过受控包装或回调记录节点事件;本示例仅记录每次图调用。
检查站 2
恢复检查点时保持线程 ID 不变,每次调用使用新 ID。传入原始输入或恢复 Command,并保留其他调用配置。
检查站 3
不要批量发送图形状态、消息、检查点内容或工具参数。
检查站 4
调用返回并不证明任务成功。__interrupt__ 中返回的动态中断记为暂停;抛出异常的调用记为失败。静态断点、节点和工具事件以及经评审的任务质量需要另行埋点。
检查站 5
直接调用 Log API 使用一秒的协作式超时,并保护诊断输出。发送失败不会替换图的结果或错误;等待发送仍可能增加延迟。本示例不实现持久队列、重试或关闭时排空。
验证
证明事件已到达
在演练已知的成功和失败案例后运行此命令。如果您的最终事件契约与代码片段不同,请替换后备表名称。
LangGraph agent observability 验证查询
SELECT *
FROM agent_graph_invocation
ORDER BY timestamp_utc DESC
LIMIT 20;实施参考
在启用新的生产路径之前,请检查事件合同、数据安全指南和上游主要文档。
在哪里记录事件
保持结果事件小且可恢复
该模式提供了
- 除了上游工作流程之外,还有一个有界的、SQL 就绪的结果。
- 用于仪表板、警报和跨事件关联的稳定字段。
- 用于验证成功、失败、重试和超时行为的夹具驱动路径。
该模式不提供
- OTLP 导出器、自动收集管道或详细跟踪和诊断日志的替代品。
- 仅因为有效负载包含事件 ID,所以仅传送一次。
- 收集原始提供商有效负载、用户内容、凭证或受监管数据的权限。
事件模式示例
此工作流的事件模式
在将查询或代码片段适应生产之前,请检查行粒度、发出边界、所需类型、隐私类、示例有效负载和验证清单。
相关产品功能
继续此工作流程 AI代理监控
通过可审查的 SQL 连接智能体运行、工具使用、模型成本、质量和产品结果。
相关 SQL 查询示例
更多 SQL 示例
针对此工作流程中的结构化字段运行查询,检查示例结果,并将有用的答案转换为仪表板或警报。
按实施系列浏览
比较相关集成模式
与此集成配对的模板
更多集成