Tracing
Capture spans for prompts, retrieval, tools, and tokens to debug production AI.
In plain words
Tracing saves a step-by-step diary of everything the AI did for one question: what it looked up, what it asked the model, how long each part took and what it cost.
Think of it like…
The flight data recorder of a request: every instrument reading in order, so after an incident you can replay what happened rather than inferring it from the crash site.
Where you’ve seen it
One support request takes 4.2 seconds. Its trace shows a root span for the request with child spans: retrieval 0.35 s (8 chunks), rerank 0.15 s, an LLM generation call 3.40 s (3,100 input tokens, 420 output tokens, prompt version v14, model recorded), post-processing 0.05 s and 0.25 s of orchestration overhead. Cost: 3,100 × $2.50/M + 420 × $10/M = $0.00775 + $0.0042 ≈ $0.012. When the user flags a wrong answer, the engineer opens the trace, sees the retrieved chunk was from the outdated 2023 policy and adds a date filter; the same trace is added to the golden set as a regression case.
Words you’ll hear
- Trace
- The complete record of one request, identified by a trace id.
- Span
- One timed operation inside a trace (an LLM call, retrieval, tool call) with attributes.
- Attributes / metadata
- Key-value details on spans: model, prompt version, tokens, cost, user segment.
- Sampling and redaction
- Choosing which traces to keep and removing sensitive data from them.
Visual diagram
Theory & deep dive
What to capture
Instrument every meaningful step as a span with a start and end time, a parent span, a type (LLM, retrieval, tool, guardrail) and attributes. For LLM spans record the model and parameters, the full prompt messages (or a reference to the versioned template plus variables), the completion, token counts for input, output and cached tokens, latency including time-to-first-token for streaming, cost and finish reason. For retrieval spans record the query (and rewritten query), filters, returned document ids with scores and the chunk text. For tool spans record the tool name, arguments, result, errors and retries.
Add metadata that makes traces searchable and comparable: prompt version, model, feature flag or experiment variant, user or session id (hashed), environment, customer tier and release version. Attach scores to traces (user thumbs, LLM-judge results, rule checks), so you can filter "all traces with negative feedback this week". Use consistent naming so dashboards group correctly, and propagate the trace id through background jobs and downstream services to keep one coherent trace across them.
{"trace_id": "b19f…", "span": "llm.generate", "parent": "handle_question", "model": "provider-model-x", "prompt_version": "v14", "input_tokens": 3100, "output_tokens": 420, "latency_ms": 3400, "cost_usd": 0.0119, "finish": "stop"}
Operating tracing responsibly
Traces contain user data, so design for privacy: redact or hash PII and secrets before export, restrict access by role, set retention limits (for example 30 to 90 days for full payloads), and honour deletion requests. Control volume and cost with sampling (keep 100% of errors, negative-feedback and slow traces, and 5% to 10% of the rest), compression and asynchronous batched export so tracing never adds latency to the request path or fails the request when the backend is down.
Make traces useful day to day: a trace viewer to read a request end to end, filters by version, tag and score, dashboards for latency (p50 and p95), cost and error rates per span type, and one-click add to dataset to turn a bad trace into a regression case. Standardise on open formats such as OpenTelemetry's GenAI semantic conventions or a platform SDK (Langfuse, LangSmith, Arize Phoenix) to avoid lock-in. Tracing complements, but doesn't replace, evals and aggregate metrics: it explains individual cases, while evals and monitors tell you how common they are.
Keep 100% of traces with errors, thumbs-down, latency above p95 or guardrail triggers, plus 5% of everything else. At 50,000 requests a day that's roughly 2,500 normal traces plus the interesting ones, instead of storing all 50,000 payloads.
How it works, step by step
Step 1 of 41. Instrument each step
Wrap retrieval, rerank, LLM and tool calls in spans under a root request span.
- Overhead
- async export
When to use it & trade-offs
What it is
Tracing records a structured, hierarchical log of everything that happened while serving one request: a trace containing spans for each step (retrieval, reranking, each LLM call, each tool call, guardrails), with inputs, outputs, timing, token counts, cost, model and prompt versions, errors and metadata. For LLM applications the trace is the unit you debug, evaluate and monitor, because the final answer alone doesn't tell you *why* it was wrong, slow or expensive.
Why it matters: LLM systems are multi-step and non-deterministic. When a user reports a bad answer, you need to see the exact prompt, retrieved chunks and tool results that produced it. Traces enable error analysis, trajectory evals, cost and latency attribution, regression debugging and dataset building from real failures. Without them, teams are guessing; with them, a vague complaint becomes a specific span to fix.
When to reach for it
- Debugging bad answers and slow requests
- Attributing cost and latency to steps
- Building eval datasets from real traffic
When to avoid or be careful
- Logging raw PII and secrets
- Synchronous tracing that adds latency or fails requests
- Keeping only the final answer
Observability signals for LLM apps
Strengths: Full context and timing; links steps.
Watch out: Heavy payloads; privacy and cost concerns.
Strengths: Cheap; alertable.
Watch out: Doesn't explain individual failures.
Strengths: Simple and familiar.
Watch out: Hard to reconstruct a multi-step request.
Interview signals & numbers to know
When this topic comes up
- “A user got a wrong answer and we can't tell why”
- “Where does the time go?”
- “Which step costs the most?”
- Spans and trace ids
- Prompt versions in logs
Benchmarks & rules of thumb
- 4.2 s
- end-to-end latency of the example request
- ~$0.012
- cost of 3,100 input and 420 output tokens
- 5%
- sample of ordinary traces, with all problem traces kept
Common mistakes & follow-ups
Common interview mistakes
- Tracing the LLM call but not retrieval, so stale-context failures stay invisible.
- Forgetting to record the prompt version and being unable to reproduce a regression.
- Storing every full payload forever.
Likely interviewer follow-ups
- What would you put in a trace for a RAG application?
- How do you handle privacy and cost of tracing?
- How do traces help evaluation?
Knowledge check
Put it into practice
Practice related interview Q&A
Apply Tracing with generative AI interview Q&A and hands-on practice.