為何每個 AI 智能體都需要追蹤:OpenTelemetry 與 LangSmith 如何配合 FastAPI 和 LangGraph
Why Every AI Agent Needs Tracing: OpenTelemetry and LangSmith for FastAPI & LangGraph
FastAPI 與 LangGraph 的 AI 智能體需要追蹤,才能查明檢索、工具呼叫或提示詞變更等環節的問題。OpenTelemetry 提供供應商中立的分散式追蹤,LangSmith 則支援智能體觀測與評估,並追蹤 token 成本及 P50/P99 延遲。文章建議記錄各步延遲、token 數量與成本、檢索片段相似度分數,以及工具錯誤和重試次數。

If you've ever shipped a RAG pipeline or a multi-step LangGraph agent, you know the hardest bug to fix isn't a Python KeyError. It's when the app returns HTTP 200, gives the user a slightly wrong answer, and you have no idea which step broke.
Did the retriever pull stale chunks? Did a tool call time out and silently fall back? Did a prompt tweak double your token bill?
On November 26, 2026, Hugging Face's new guide on building and training multi-agent AI systems highlighted the exact same lesson: once you move beyond single-turn prompts, extensive interaction logging and coordination-latency tracking become mandatory infrastructure.
In production AI Engineer roles today, two tools show up together in almost every job description: OpenTelemetry (OTel) and LangSmith.
OpenTelemetry vs. LangSmith in Plain Terms
OpenTelemetry (OTel): The vendor-neutral open standard for distributed tracing. Every HTTP request, database query, and LLM call becomes a span inside a single trace. You can see exact millisecond timings from your FastAPI endpoint down to your vector store and model API.
LangSmith: An LLM- and agent-native observability and evaluation platform. It natively traces LangGraph/LangChain (as well as OpenAI, Anthropic, or custom SDKs), tracks token cost and P50/P99 latency, runs online LLM-as-judge checks, and supports OpenTelemetry ingestion so you don't lock yourself into one vendor.
How to Instrument a FastAPI + LangGraph Agent in 20 Minutes
1. Trace the full request lifecycle: Wrap your FastAPI entry point and LangGraph nodes so one trace ID follows intent classification → retrieval → SQL/tool execution → retry loop.
2. Log the four signals that matter: Record per-step latency (P50/P99), token count/cost, retrieved chunk similarity scores, and tool error/retry counts.
3. Send traces asynchronously: Use an async callback handler (like LangSmith's SDK) or an OTel collector so observability never blocks user-facing latency.
4. Pair traces with offline evals: Export failing production traces straight into your pytest or RAGAS regression suite so every bug becomes a permanent test case.
What I Built
Demo
Partner Technologies
Hackathon Experience
來源:DEV Community · dev.to