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_email40% 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?
Keep reading
Related articles from the SudoSecurity blog.
Build Your Own AI Agent Stack: The Complete Self-Hosted Guide for 2026
Build a production-grade self-hosted AI agent from scratch using Docker Compose. Covers the full stack — Ollama, n8n, Flowise, Qdrant, SearXNG, Langfuse, and Redis.
AI Agents Are Breaking the Internet — And Nobody's Ready for the Bill
ChatGPT went down 28,000-report strong in February 2026. Claude had back-to-back outages within 24 hours in March. GitHub logged 37 incidents in a single month and is running at 90.21% uptime — one nine, not three. AI agents aren't just using infrastructure.
OpenClaw: The Lobster That Broke the Internet, Burned Through Every LLM's Compute Budget, and Started a War Between Anthropic and OpenAI
A retired Austrian developer built an open-source AI agent for fun in November 2025. Within 90 days, Jensen Huang called it the most successful open-source project in human history.
How AI Actually Works in Modern Video Games: FSMs to LLMs
Game AI isn't intelligent. It's state machines, search, and scoring arranged to feel like judgment, with real, runnable code from FSMs to local LLM dialogue.