Observability and Traceability: Explain Where Any Result Came From
"Traceability" in the pipeline: when a customer asks "why is this amount this number?", you can replay the whole process within a minute.
Core concepts
| Pillar | Question it answers | Example |
|---|---|---|
| Logs | What happened | Structured JSON logs carrying trace_id and task_id |
| Metrics | What the overall trend is | Error rate, P95 latency, token cost, human intervention rate |
| Traces | Which stages a request went through, and how long each took | Frontend request → API → worker → LLM call |
| Business traceability / audit | How this result came about and who changed it | Input snapshot, prompt version, model, every step's output, human edits |
Traces vs logs: logs are discrete events; a trace is made of spans with parent-child relationships, expressing the causality and timing of calls. Link them with the same trace_id.
How it works
OpenTelemetry
- A vendor-neutral observability standard: API + SDK + Collector, exporting to Jaeger, Grafana Tempo and all kinds of APM tools.
- GenAI semantic conventions: define the attribute names for LLM call spans (e.g.
gen_ai.operation.name,gen_ai.request.model, token usage), and recommend the span name{operation name} {model name}. - ⚠️ As of 2026-11 the GenAI semantic conventions are still in Development status (not yet stable), so attribute names may change.
LLM-specific observability platforms
Platforms such as Langfuse add on top of traces: prompt version management, comparing results by version, evaluation scores, cost tracking and session replay. Langfuse is open source and can be self-hosted (important for on-premises B2B delivery).
In practice
Which fields to store for traceability
| Level | Fields |
|---|---|
| task | Input snapshot (original file URI + sha256), customer, creator, final result, state history |
| task_step | Step name, input, output, duration, error, retry count |
| llm_call | trace_id, task_id, step, provider, model, prompt name and version, parameters, input/output (or references to them), stop_reason, token usage, cost, latency, whether it was a fallback |
| review | Reviewer, AI draft, human result, diff, time |
Tying the whole pipeline together with trace_id (FastAPI middleware)
import uuid, structlog
from starlette.middleware.base import BaseHTTPMiddleware
class TraceMiddleware(BaseHTTPMiddleware):
async def dispatch(self, request, call_next):
trace_id = request.headers.get("x-trace-id") or uuid.uuid4().hex
request.state.trace_id = trace_id
structlog.contextvars.bind_contextvars(trace_id=trace_id) # every log after this carries it automatically
response = await call_next(request)
response.headers["x-trace-id"] = trace_id
return responseWrite the trace_id to the task table when the task is created and bind it again when the worker runs; show the trace_id when the frontend displays an error, so a user's bug report can be located directly.
Task timeline replay (frontend)
Ingested 10:02:01 → parsed 3.2s → extracted (prompt contract_extract@v3, model-a, 4.1s, 3.2k tokens) → validation failed → regenerated → handed to a human (low confidence: payment term) → reviewer Zhang San changed payment_days 120→90 → pushed to ERP successfully
Common pitfalls
- Storing full prompts and outputs in logs without thinking about privacy: customer data must be masked or encrypted, with a retention period.
- Logs with no trace_id: logs from different workers can't be correlated.
- Not recording the prompt version: when results change you can't find the cause.
- High-cardinality metrics: using task_id as a metric label blows up the monitoring system's storage. That kind of information belongs in logs/traces.
Common interview questions
- Which fields should the trace record of each task store?
- What's the difference between traces and logs? How do you use a trace_id to tie together the frontend, backend, async tasks and LLM calls?
- Which LLM-related metrics would you monitor?
- What are the OpenTelemetry GenAI semantic conventions? Are they stable yet?
- The trace data contains sensitive customer information. How do you handle it?
How it relates to my projects
- AI Document Review Pipeline W4: task step timeline replay + Langfuse integration; acceptance criterion "explain where any result came from within 1 minute".
