Skip to content
Goal: Architecture-level✓ Sources verified 2026-11-24OpenTelemetryLangfuseStructured LoggingAudit

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 ​

PillarQuestion it answersExample
LogsWhat happenedStructured JSON logs carrying trace_id and task_id
MetricsWhat the overall trend isError rate, P95 latency, token cost, human intervention rate
TracesWhich stages a request went through, and how long each tookFrontend request → API → worker → LLM call
Business traceability / auditHow this result came about and who changed itInput 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 ​

LevelFields
taskInput snapshot (original file URI + sha256), customer, creator, final result, state history
task_stepStep name, input, output, duration, error, retry count
llm_calltrace_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
reviewReviewer, AI draft, human result, diff, time

Tying the whole pipeline together with trace_id (FastAPI middleware) ​

python
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 response

Write 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 ​

  1. Storing full prompts and outputs in logs without thinking about privacy: customer data must be masked or encrypted, with a retention period.
  2. Logs with no trace_id: logs from different workers can't be correlated.
  3. Not recording the prompt version: when results change you can't find the cause.
  4. 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 ​

  1. Which fields should the trace record of each task store?
  2. 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?
  3. Which LLM-related metrics would you monitor?
  4. What are the OpenTelemetry GenAI semantic conventions? Are they stable yet?
  5. 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".

Sources ​

Vin Amory · AI development × content creation · Technical notes cite official docs in each page’s sources section