Module 5 · Reliability, observability and cost · Chapter 17 · Free
Observability for AI services: traces, tokens and quality signals
- An AI service can return 200 OK and still give wrong, stale or rambling answers. Health and correctness differ.
- Log one rich record per request: model, prompt version, tokens, timing, cache, retrieval and finish reason, tied to one trace id.
- Priya keeps three dashboards: latency, cost per request and quality. Few alerts page. The rest file a ticket.
The picture: ordinary metrics say the shop is open. AI observability says whether the advice was any good.
- Why CPU, request rate and error codes miss real failures.
- The per request record to log at the gateway.
- How one trace follows a request across gateway, retrieval, engine and tools.
- Quality signals on a sample, plus refusal and fallback rates.
- A lab where you design the telemetry and alerts for a stated symptom.
Copperline is a made up B2B SaaS company. One Tuesday, Priya Nair, its staff platform engineer, watched every graph stay green while Ana, the support lead, escalated copilot complaints.
- Uptime was 100 percent, errors near zero, CPU calm. All figures here are illustrative.
- Users said answers cited an old refund policy. The copilot was up, fast and confidently wrong.
- Tom, the SRE on Priya's team, could not say which model, prompt and documents produced those answers.
The platform measured the machine, not the work.
- Why ordinary service metrics miss AI failures.
- The per request record.
- One trace across gateway, retrieval, engine and tools.
- Quality signals, dashboards and alerts.
- Log hygiene for data carrying prompts.
- The picture, a worked example, and a design lab.
Next: the gap between a healthy service and a helpful one.
Step 1: Up and wrong, the failure ordinary metrics miss
Ordinary metrics suit deterministic services: fast code usually means a right answer. A model breaks that link.
- CPU and GPU use: hardware is busy, not whether the work helped anyone.
- Request rate: demand arrived. Nothing about length, cost or usefulness.
- Error codes: catch crashes. A wrong policy citation is a success response with bad content.
- Average latency: hides time to first token versus a slow tail of output tokens.
Copperline failed quietly: stale retrieval, a prompt change lengthening answers, a fallback answering on timeouts, refusals rising for one topic. None raised a 5xx.
Think of a restaurant check that counts clean plates but never tastes food. Where the picture breaks down:
- You can taste every dish. You cannot read every answer, so quality needs sampling (Step 4).
- AI quality also means groundedness: did the answer use the retrieved documents?
Chapter 4, SLOs for AI services: latency, throughput and quality, set the targets. This chapter builds the signals. For token basics, see Tokens and context windows.
Next: the single record that makes every later question answerable.
Step 2: The per request record, your unit of truth
Log one structured record per request, written at the end by the gateway. Keep it small and typed. Raw prompts do not belong in it (Step 6).
- Identity: trace id, workload (copilot, extraction, coding agent), tenant class, timestamp.
- Versions: model and version, prompt template version, index version. Without these, no bad answer can be reproduced.
- Tokens: in, out, and cached if reported. Tokens drive latency and cost.
- Timing: time to first token, total latency, stage splits where you have them.
- Path: cache hit or miss, retrieval count, tools called, fallback used or not.
- Outcome: finish reason (completed, length limit, refusal, timeout, error), status, cost estimate.
{
"trace_id": "illustrative-id-7f3a",
"workload": "support-copilot",
"model": "copilot-model", "model_version": "v12",
"prompt_template": "support-answer", "prompt_version": "p34",
"index_version": "helpdocs-2026-11-20",
"tokens_in": 1240, "tokens_out": 286,
"ttft_ms": 620, "latency_ms": 3810,
"cache": "hit", "retrieval_count": 5,
"tools": ["doc_search"], "fallback": false,
"finish_reason": "completed", "status": 200
}
Use the same field names on all three workloads. Count tokens from the engine response, not characters.
Next: following one request through four systems with one id.
Step 3: One trace id across gateway, retrieval, engine and tools
The record is the summary, the trace is the story. The gateway creates a trace id and passes it downstream.
- Gateway span: auth, routing, rate limit decision, total time.
- Retrieval span: query time, index version, chunks returned.
- Engine span: queue time, prefill, time to first token, decode, tokens in and out.
- Tool spans: each tool call, timed, with success or failure.
Support pastes the trace id and Tom sees the whole path. Without it, he greps four log streams and guesses.
Think of one parcel tracking number at every depot. Where it breaks down:
- A parcel moves in a line. Requests fan out in parallel, so spans form a tree.
- Each span must be timed by its own service, or gaps stay invisible.
Sample full traces if volume is high, but keep the record for every request and traces for every error.
Next: the signals that say whether answers are any good.
Step 4: Quality signals, measured on a sample
Priya layers four cheap quality signals, each catching a different failure.
- Sampled human review: Ana's team scores a small daily sample as correct, grounded and helpful.
- Thumbs feedback: thumbs down rate by topic and prompt version. Noisy, biased to unhappy users, but fast.
- Automated eval scores: a scorer grades a sample on checks such as citing a retrieved chunk. Chapter 18, Eval gates in CI/CD: shipping model and prompt changes safely, makes this a release gate. See also Evals.
- Refusal and fallback rates: rising refusals can mean safer or broken. Rising fallbacks mean the main path fails quietly. Track both by version.
Attach scores back to the trace id. That join lets you compare prompt versions on the same index.
Predict first:
Each signal tracks a different cause. Longer answers raise tokens, latency and cost. Refusals track scope and policy behaviour, which this change did not touch. That independence is the point: watch each signal for its own failure family.
Next: the three dashboards, and the line between a page and a ticket.
Step 5: Three dashboards, and alerts that earn a page
Priya keeps three dashboards.
- Latency: time to first token and total latency, p50 and p95, by workload and cache hit or miss.
- Cost per request: tokens in and out, cost per request, cache hit rate. Greg, the finance partner, reads this one.
- Quality: eval score, thumbs down rate, refusal rate, fallback rate, near zero retrieval rate.
Page only on live, fixable harm.
- Page: error or timeout spike; time to first token p95 breaks the SLO sustained; fallback rate jumps.
- Ticket: eval drift over days; cost creep; refusal shift after a release; cache hit rate slide.
- Quality alone never pages. One thumbs spike is a ticket.
Next: what must never land in those logs in the first place.
Step 6: Log hygiene, because prompts carry customer data
Prompts quote invoices and account details. Retrieved documents carry the same. Storing raw prompts makes logs a poorly guarded copy of customer data.
- Never log by default: raw prompts, raw chunks, tool arguments with account data, full answers on sensitive workloads.
- Log instead: ids, versions, counts, lengths, hashes for matching, and redacted samples under stricter access with short retention.
- Redact before storage: the pipeline strips emails and account numbers, not each developer remembering.
- Sample on purpose: the review sample follows your data policy, with logged access and scheduled deletion.
Priya's test: if this store leaked, what must we disclose? Keep that answer small.
Next: the picture, one request leaving a record at every stage.
Step 7: The picture, one request, four spans, one record
Press Next to follow one request.
- The gateway assigns the trace id, records routing and starts the clock.
- Retrieval logs index version and chunk count under the same id.
- The engine span carries queue time, time to first token, tokens in and out.
- Each tool call is a timed child span, so a slow tool cannot hide.
- Summary fields land in one record keyed by the trace id.
- Three dashboards are views over the same records, plus sampled scores.
Next: a worked example where the obvious suspect is innocent.
Step 8: Worked example, a latency rise that was not the engine
Thursday, copilot p95 latency rose from an illustrative 2.1 seconds to 3.5 seconds. The first guess was a sick engine. The records said otherwise.
1. Split the latency. Time to first token p95 barely moved: an illustrative 0.60 to 0.65 seconds. Queue time was flat. The extra 1.4 seconds sat in decode.
2. Check tokens out. Average tokens out rose from an illustrative 180 to 335. A Wednesday prompt release had asked for fuller answers with examples.
3. Decode math. Extra output: 335 minus 180 = 155 tokens. At an illustrative 110 tokens per second, extra time is 155 / 110, about 1.4 seconds. That matches the rise.
4. Cost check. Tokens out rose by 155 / 180, about 86 percent. Cost per request rose with it, as the cost dashboard showed.
5. Decision. No engine change, no extra GPU. Priya and Ana shortened the prompt, tokens out fell back, and latency and cost followed.
With tokens split from time to first token, this was a ten minute read, not a day of engine tuning.
Next: the lab, designing telemetry for a symptom.
Step 9: Hands-on lab, design the record and the alerts
Why this exercise: observability is designed before the incident. Turn a vague symptom into fields and alert levels.
The symptom: extraction users say results feel slower after lunch and some totals look wrong, but the pipeline shows no errors.
Lab: Copperline's dashboards stay green while real problems hide underneath. This lab runs in your browser and needs JavaScript enabled.
- Only live harm with a live fix earned a page. Slow signals became tickets or charts.
- Good added fields: page count band and confidence. Raw invoice text never belongs in the record.
Next: a field note and the mix-ups.
In production practice, the cheapest win is often a shared trace id and a disciplined per request record, not a new tool. Tooling varies, so test any vendor claim about automatic quality detection on your own sampled data.
Mix-ups to avoid
- "Green dashboards mean good answers." They mean a running service. Quality needs sampled signals.
- "Log every prompt, just in case." That copies customer data into logs. Log fields and redacted samples instead.
- "Average latency is enough." Split time to first token from total, and both by tokens out.
- "Page on every quality dip." Quality signals are noisy. Page on live harm, ticket drift.
- "Fallbacks are free resilience." A silent fallback can mask a dead main path. Record and rate it.
Chapter quiz
Five questions, 70% to pass.
Review this chapter in 5 minutes
Watch the whole chapter recapped: the per request record, one trace id across four systems, sampled quality signals, and page versus ticket alerts.
- Up is not right: error codes and CPU miss wrong, stale and rambling answers.
- One record per request: trace id, model, prompt and index versions, tokens in and out, time to first token, total latency, cache, retrieval count, finish reason.
- One trace id across gateway, retrieval, engine and tools. Records for all, full traces sampled and for all errors.
- Quality on a sample: human review, thumbs, eval scores, refusal and fallback rates.
- Three dashboards: latency, cost, quality. Page on live harm, ticket the drift.
- No raw prompts or documents by default. Redact in the pipeline.
Decision rule: user hurt now, human can act now? Page. Drifting? Ticket. Only explains? Chart it.
Review
Glossary
- Trace id
- One id shared by every span of a request.
- Span
- One timed stage in a trace. Example: the retrieval span.
- Per request record
- One summary row per request: versions, tokens, timing, outcome.
- Time to first token
- Wait until the first output token arrives.
- Finish reason
- Why generation stopped: completed, limit, refusal, timeout.
- Retrieval count
- Chunks returned for a request. Zero is a warning sign.
- Eval score
- Automated grade on a sampled answer against set checks.
- Fallback rate
- Share answered by a backup path. A jump signals main path trouble.
- Refusal rate
- Share of requests the model declines, by topic and version.
- Redaction
- Removing sensitive values before storage.
Next: "Eval gates in CI/CD: shipping model and prompt changes safely".