19Agent 可观测性

L3P1

Agent 出问题极难复现,可观测性是生产 harness 的命脉。要 capture 的核心数据:

Trace:每次 LLM call(input messages + response + 用时)+ 每次 tool call(name + args + result + 用时)+ stop_reason 链。一条 trace 就是 agent 这一次 run 的完整传记。

Token 用量:input / output / cache_creation / cache_read 分桶统计,cache 命中率监控。命中率 <50% 报警(说明 prompt 不稳)。

错误回放:trace 存 S3 或 ClickHouse,生产事故时回放定位。配合 #31 Determinism & Replay 做 incident postmortem。

工具栈:Langfuse(开源)、Helicone、Anthropic Console(免费基本面板)、自建 ClickHouse(高吞吐 + 复杂查询)。

// 关键 metric:
- 每个 task 的 token 总数、cost
- tool call 次数分布(单次 task > 50 次要警惕)
- p50/p95/p99 latency
- cache hit rate per task
- error rate per tool
速查
"线上 agent 慢怎么排?"——先看 trace 哪一步耗时长。LLM call 慢 → 看 input token 数(可能 prompt 膨胀);tool call 慢 → 看具体 tool;看 cache 命中率下降没。