Skip to content

Lesson 12 of 13 · 18m

Traces, Not Vibes: Debugging in Production

Log every span with model and prompt versions, replay failures into fixtures, and watch cost per completed task.

When the feature misbehaves at 2am, the only artifact that matters is the log. "The model said something weird" is not debuggable. A trace of every input, retrieval result, tool call, token count, and output on request req_8f31 is.

What a trace looks like

One request to your feature fans out into spans: retrieval query, rerank, the LLM call(s), tool calls, retries. Each span records model version, prompt template version, input and output token counts, latency, and the raw I/O. OpenTelemetry's GenAI semantic conventions (gen_ai.system, gen_ai.request.model, gen_ai.usage.input_tokens) standardize the attribute names; Langfuse (open source, self-hostable), LangSmith, and Helicone ingest the spans and give you the drill-down. If you're not ready for a platform, one structured JSON log per call gets you 80% of the value.

The minimum viable trace is one JSON line per span:

{"ts": "...", "req": "req_8f31", "span": "llm.classify", "model": "gpt-4o-2024-08-06",
 "prompt_v": "classifier.v14", "in_tok": 1482, "out_tok": 23, "ms": 940, "tool_calls": ["lookup_order"]}

Nondeterminism is not a defense

The model varies; your recording doesn't. With the exact request logged — messages, tools, temperature, model version — you can replay it against a pinned snapshot, reproduce the failure, and demote the mystery into an eval fixture. That pipeline — production surprise → trace → fixture → regression test — is how eval suites grow teeth. The seed parameter narrows variance further where the provider supports it.

Where to put the logging boundary

Wrap the client, don't sprinkle console.log through handlers. One thin wrapper around your provider SDK — a call_model() that takes the request, stamps prompt version and request ID, times the call, logs the span, and returns the response — covers every call site including the ones you'll add next quarter. Interceptor beats call-site logging for the same reason middleware beats per-route auth checks: coverage isn't optional when the code grows.

The dashboards that pay

  • Cost per completed task, per user, per feature — not per API call. Retries and tool loops multiply the number on the invoice.
  • Error and refusal rate by prompt version. A jump after a prompt deploy is your canary.
  • Tool-call distribution — which tools, what args, how often they error. An agent suddenly calling send_email 40% more is a finding, not a statistic.
  • Latency split into TTFT and total. Users experience the former; your budget probably tracks the latter.

User feedback belongs in the same pipeline: a thumbs-down button is a labeling interface, and its events should land on the trace so a bad answer can be replayed, diagnosed, and fixture-ified — the cheapest eval data you'll ever collect.

Privacy in the logs

Raw I/O includes whatever users pasted — which is to say, PII. Redact or hash sensitive fields at the logging boundary, set retention windows, and give the tracing platform the same access controls as your databases. A trace backend full of unredacted customer emails is an incident report waiting for a date.

Field notes

  • Log the prompt template version with every call; "the prompt changed Tuesday" explains half of weird output.
  • Tag eval fixtures with their origin trace ID. Six months later you want to know which prod failure a fixture immortalizes.
  • Billing alerts on the provider account are ops hygiene. Set one.
  • Sample don't dump: on high-volume features, full-trace sampling (say 5%) plus always-on error traces keeps the log bill under the model bill.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.