Agent Development 10: Observability and Fault Localization
The tenth Agent development tutorial uses an order-query task to demonstrate how to distinguish a tool timeout from a result associated with the wrong order. It explains how Traces, Spans, structured logs, and metrics work together, and includes runnable Python code and unit tests covering parent-child relationships, error location, attribute filtering, and result validation. The example is a single-process synchronous teaching implementation using mock data; it does not implement cross-service Trace context propagation or a real OpenTelemetry or observability-platform integration. Token figures are fixed mock values rather than real tokenization, model billing, or production traffic.
The tutorial gives Agent builders a concrete method for distinguishing successful technical calls from incorrect business outcomes and for tracing failures to a verifiable execution boundary.
Agent开发 10|可观测性:怎样追踪一次任务并定位故障?
《Agent开发》系列 · 第 10 篇
用户反馈:“我问的是订单 1001,助手回答的却像是另一张订单。”
后端日志里有几行“模型调用成功”“工具调用成功”,没有异常堆栈。应该先改提示词、换模型,还是检查查询接口?如果不知道每一步实际发生了什么,排障很容易变成猜测。
Agent 的一次任务可能经历多轮模型调用、知识检索、工具执行、审批等待与恢复。我们需要把这些步骤关联起来,观察输入从哪里来、结果流向哪里、耗时花在哪里,以及错误发生在哪个边界。这就是 Observability(可观测性) 在 Agent 系统中的核心用途。
本篇用同一个订单查询任务,分别制造“工具超时”和“结果对应错误订单”两类故障。先看怎样定位,再解释 Trace、Span、日志与指标分别提供什么信息。