DEV Community

Cover image for Why Every AI Agent Needs Tracing: OpenTelemetry and LangSmith for FastAPI & LangGraph
Gaurav Shinde
Gaurav Shinde

Posted on Edited on

Why Every AI Agent Needs Tracing: OpenTelemetry and LangSmith for FastAPI & LangGraph


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

Top comments (0)