Trace routing, filtering, candidate generation, reranking and context packing so a failed answer can be explained and repaired.
Retrieval traces: locate the stage that lost the answer
Record stage boundaries
A single total latency and final answer are not enough. Give each query a trace ID and record route, index generation, encoder revision, allowed corpus scope, candidate counts, reranker cutoff, context IDs and answer citations. Keep the original query in a protected audit store when policy permits; avoid putting private text, user IDs or document contents into metric labels. Routing and packing need separate spans.
Name the first failing stage
If the answer-bearing passage is absent from candidate retrieval, changing the prompt will not fix it. If it appears at rank 12 but the reranker receives only eight, the candidate window is the failure. If it reaches the context packet but the model cites another passage, investigate answer synthesis and support checking. Diagnose against a judged passage ID, not a vague statement that search felt poor.
Keep traces safe and bounded
Trace metadata can itself expose sensitive document names or tenant relationships. Hash or redact identifiers in broad telemetry, reserve full IDs for access-controlled debugging, and apply retention limits. Sample routine traffic, but retain all permission denials, deletion races and release-gate failures under an approved policy. A trace with every passage text can cost more to store than the retrieved context and may violate the source's access boundary.
Use paired comparisons
Replay a fixed query against current and candidate releases with the same corpus snapshot and permission scope. Diff each stage: route, top-k identities, filters, rerank order, context selection and citations. This isolates a changed encoder from a changed index or prompt. Release gates should attach these traces to failures so a rollback decision has evidence.
Worked contract
from dataclasses import dataclass
@dataclass(frozen=True)
class RetrievalTrace:
expected_passage: str
candidates: tuple[str, ...]
reranked: tuple[str, ...]
context: tuple[str, ...]
cited: tuple[str, ...]
def first_loss(trace: RetrievalTrace) -> str:
stages = (("candidate retrieval", trace.candidates),
("reranking", trace.reranked),
("context packing", trace.context),
("answer citation", trace.cited))
for stage_name, passage_ids in stages:
if trace.expected_passage not in passage_ids:
return stage_name
return "supported path"
incident = RetrievalTrace("policy-47-r3", ("policy-47-r3", "memo-29"),
("memo-29",), ("memo-29",), ("memo-29",))
assert first_loss(incident) == "reranking"
assert first_loss(RetrievalTrace("missing", (), (), (), ())) == "candidate retrieval"Performance and operating cost
Finding the first loss scans at most the four recorded stage lists, O(K) time for K total IDs and O(1) extra space. A full trace pipeline adds serialization, storage and review cost proportional to captured candidates. Sample successful queries and store compact IDs; preserve richer traces for judged failures under access control.
Common Mistakes
- Blaming generation when the answer passage never entered candidate retrieval.
- Logging private passage text or principal IDs in broad metric labels.
- Comparing releases against different corpus snapshots.
- Recording only final scores and losing stage identities.
Validation drill
Create 41 judged traces with deliberate failures at each stage. Produce a count by first-loss stage and inspect one trace from each category before changing any retrieval parameter.
Read next
- Retrieval release gates: evaluate slices before changing an index
- Reranking: spend model budget on a bounded candidate window
- Context packing: retain independent evidence under a token cap
- Claim-level citations: verify each answer against its passage
