Trace Analysis at Scale: Tail-Sample to Read, Count Every Trace by Failure Cohort to Measure
600 traces, 62 failures, and each one looks like bad luck. A head sample of 30 keeps one failure; a tail sample of 89 keeps all 62. Counting every trace by tool and prompt version shows send_email + v2 failing 42 percent against 4 percent for the rest, and a rate taken from the tail sample alone (69.7 percent) is wrong by design.
What you will learn
- Compare a head sample and a tail sample on the same traces and say what each one can miss
- Group every trace into a cohort by a key, such as tool plus prompt version, and count failures per cohort
- Find a systemic failure that no single trace shows, then read a few of its traces
- Explain why a failure rate taken from a tail sample is inflated
Before you start
- Observability and tracing for LLM apps
- OpenTelemetry tracing for a Claude agent loop
SupportDesk's agent handles hundreds of tickets, and about one in ten fails. Three failures turn up in the traces: a rate limit, a reply that got cut off, a bad argument to a tool. Each gets opened on its own, one trace at a time, and each looks like plain bad luck.
The team keeps one trace in twenty. Out of 600 traces that's 30 to read, with one failure among them. Nobody sees a pattern, because no single trace says why. In this article we keep the right traces to read, and count every trace to know.
A quality check on a factory line
- Check every twentieth box, picked at the start of the belt. That's a head sample: cheap, but a rare bad line can slip through.
- Keep every box that comes back damaged, plus a few good ones to compare. That's a tail sample.
- Keep a tally sheet for each line: count every box, keep none. The one bad line stands out. That's a failure cohort.
Keep some to read. Count all to know. If head and tail sampling are new, observability and tracing for LLM apps introduces them; this article adds the counting half.
Nothing here calls a model: traces.py is a stand-in for the trace export, 600 made-up SupportDesk traces, each with the tool it ended on, its prompt version and its outcome (ok, bad_arguments, rate_limit or max_tokens).
sampler.py: decide before or after the trace ends
"""Two ways to keep about one trace in twenty."""
EVERY = 20
def head_sample(traces, every=EVERY):
"""Decided up front, before anyone knows how the trace ends."""
return [t for i, t in enumerate(traces) if i % every == 0]
def tail_sample(traces, every=EVERY):
"""Decided after the trace ends: keep every failure, and one ok trace in twenty."""
kept, ok_seen = [], 0
for t in traces:
if t.outcome != "ok":
kept.append(t)
else:
if ok_seen % every == 0:
kept.append(t)
ok_seen += 1
return kept
The head sample is one line, chosen before anyone knows how the trace ended. The tail sample waits: every failure goes in, plus one ok trace in twenty so you have something healthy to compare against.
cohorts.py: count every trace, keep none
"""Group traces into cohorts and count, so a pattern shows up that no single trace shows."""
from collections import Counter
KEYS = ("tool", "prompt_version")
def cohort_of(trace, keys=KEYS):
return tuple(getattr(trace, k) for k in keys)
def aggregate(traces, keys=KEYS):
"""Count every trace, keep none: one counter per cohort. Returns (cohort, traces, failed, rate)."""
total, failed = Counter(), Counter()
for t in traces:
c = cohort_of(t, keys)
total[c] += 1
if t.outcome != "ok":
failed[c] += 1
rows = [(c, total[c], failed[c], failed[c] / total[c]) for c in total]
return sorted(rows, key=lambda r: r[3], reverse=True)
def rest_rate(rows, worst):
"""The failure rate of every other cohort put together."""
others = [r for r in rows if r[0] != worst[0]]
return sum(r[2] for r in others) / sum(r[1] for r in others)
def examples(sample, cohort, limit=3):
"""A few failed traces from the sample, to read once the cohort is known."""
return [t for t in sample if cohort_of(t) == cohort and t.outcome != "ok"][:limit]
- A cohort is just a key: here, the tool plus the prompt version.
aggregateruns over every trace but keeps only two counters per cohort, so it's cheap at any volume.- Rows sort worst rate first. Once you know the cohort,
examplespulls a few of its failures from the tail sample to read.
Sample, then count
traces: 600 | failed 62 (10.3%)
head sample: kept 30 | failures seen 1 | bad_arguments seen 0
tail sample: kept 89 | failures seen 62 | bad_arguments seen 40
cohort traces failed rate
send_email + v2 100 42 42.0%
lookup_customer + v1 100 4 4.0%
search_help + v1 100 4 4.0%
send_email + v1 100 4 4.0%
lookup_customer + v2 100 4 4.0%
search_help + v2 100 4 4.0%
worst cohort: send_email + v2 | 42.0% against 4.0% for the rest
read these: tr-0011, tr-0023, tr-0041
rate from the tail sample alone: 69.7% (not a rate: every failure was kept)
Five cohorts sit at 4 percent: that's the normal background of rate limits and cut-off replies. Then one cohort, send_email with prompt version v2, fails 42 percent of the time. Now you know where to look, and you read three of its failures: tr-0011, tr-0023 and tr-0041. Open one and you see the arguments to send_email are wrong, which no amount of reading random traces would have told you was systemic.
The classic mistake: a rate from the tail sample
The last line of the output is a warning. Take the failure share of the tail sample alone and you get 69.7 percent. That's not a rate. The tail sampler kept every failure on purpose and only one ok trace in twenty, so any rate from it comes out far too high.
| Source | Job | Gives you |
|---|---|---|
| Tail sample | Reading | Why: open a failure, see the bad arguments |
| Cohort counters, over every trace | Measuring | How often: 42 percent against 4 percent |
For deciding how much telemetry to keep at all at high volume, see observability at scale.
Common mistakes
- Reading more traces by hand. A few dozen can't show a pattern across 600.
- Raising the head sample rate. It costs more and can still miss a rare cohort.
- Taking the failure rate from the tail sample. Every failure was kept, so it's inflated: 69.7 percent here against a true 10.3.
- Cohorting by the wrong key. By tool alone, send_email would blend v1 and v2; the prompt version is what makes the cohort stand out.
- Treating each failure as a one-off. Count first, then decide whether it's bad luck.
Quick recap
| Idea | What it does | In the run |
|---|---|---|
| Head sample | Keeps 1 in 20, decided first | 30 kept, 1 failure, 0 bad_arguments |
| Tail sample | Keeps every failure + 1 ok in 20 | 89 kept, 62 failures, 40 bad_arguments |
| Aggregation | Counts every trace, keeps none | 6 cohorts of 100 |
| Failure cohort | The pattern, with a real rate | send_email + v2: 42% vs 4% |
Try it yourself
In cohorts.py, set KEYS to just ("tool",) and run again. What rate does send_email get now, and does it still stand out from the other tools?
Related course on Udemy
Claude Certified Developer - Professional (CCDV-P) Exam Prep
Claude agents in production: reliability, observability, evals, cost, guardrails and fleets.View on Udemy ↗: Claude Certified Developer - Professional (CCDV-P) Exam Prep (paid course, opens in a new tab)Related posts
Fallback Models for an LLM App: A Chain That Checks Region and Fit, Flags Degraded Answers and Gets Rehearsed
When the main model goes down, the backup has to be allowed to take the data, able to read the whole request and tested before the bad day. A simulated four-model chain for an EU customer: a 6,000-token request lands on mid-eu, flagged degraded; a 10,000-token request is served by nobody, on purpose.
Back Pressure for LLM Traffic: Bound the Queue, Give Chat Its Own Lane, Count Tokens, Cancel When the Caller Leaves
A bulk upload buries the chat. In a small simulation, one shared queue grows to 199 waiting and the middle chat answer takes 80.2 s. A bound plus a chat lane brings it to 0.35 s while 178 bulk jobs are told to retry in 10 s, and cancelling abandoned calls saves 21,000 wasted tokens.
Load Balancing LLM Traffic: Count the Work, Weigh the Prefix Cache, Check Real Health, Drain Before Deploys
Six GPU replicas, two at 95% and four at 30%: round robin counts requests, and requests aren't equal. In a small simulation, least outstanding requests cuts p95 from 6.52 s to 3.05 s, prefix affinity lifts cache hits to 95% but pushes p95 to 11.45 s, a real health check takes errors from 48 to 2, and draining cuts 0 answers instead of 4.
A Message Queue for Document Ingestion: At Least Once, Leases, an Idempotent Consumer and a Dead Letter Queue
A queue doesn't lose a document, but it can hand one out twice. In a small simulation, a worker crash makes document two go from 3 to 6 chunks and the index holds 11 when the documents hold 8. An idempotent consumer brings it back to 8 with the same two deliveries, and a limit of three tries moves a poison file to a dead letter queue.
