Select passages by answer coverage, document diversity and provenance instead of filling the prompt with near-duplicates.
Context packing: retain independent evidence under a token cap
Reserve the evidence budget
Allocate tokens for instructions, question, response and citations before retrieved text. Each passage needs title, revision, section and citation identity. A long high-ranked section can crowd out a short exception that changes the answer. Pack by paragraphs or table rows, not a cut through a condition. Chunk boundaries determine what can be selected intact.
Prevent one revision from monopolizing context
Near-duplicate chunks often rank together. Limit repeated passages from one revision unless the question needs several clauses. Prefer a second independent source when it adds a missing fact, but do not force diversity at the cost of the sole answer-bearing span. Deduplicate the same passage identity across lexical and vector legs; copied text in separate revisions retains separate provenance.
Preserve contradictions
When permitted sources disagree, packing must not hide the newer statement just because it ranked lower. Include effective dates and revision labels so the answer can explain the conflict or abstain. Test exceptions, date boundaries and footnotes. Packet quality is whether it contains every fact needed, not its average similarity score.
Inspect the packet actually sent
Store candidate IDs, included IDs, exclusions and token counts. Compare generated claims with this exact packet, not the entire index. If support is absent, a fluent answer still fails. Claim checks decide whether to cite, revise or abstain.
Worked contract
def pack_passages(candidates: list[dict], token_budget: int,
per_revision_limit: int = 2) -> list[dict]:
if token_budget < 1 or per_revision_limit < 1:
raise ValueError("positive limits required")
selected, spent, per_revision = [], 0, {}
for passage in candidates:
revision = (passage["document_id"], passage["revision_id"])
if per_revision.get(revision, 0) >= per_revision_limit:
continue
if spent + passage["tokens"] > token_budget:
continue
selected.append(passage)
spent += passage["tokens"]
per_revision[revision] = per_revision.get(revision, 0) + 1
return selected
ranked = [{"document_id": "policy", "revision_id": "r8", "tokens": 33},
{"document_id": "policy", "revision_id": "r8", "tokens": 41},
{"document_id": "faq", "revision_id": "r3", "tokens": 29}]
packet = pack_passages(ranked, 67, 1)
assert len(packet) == 2 and sum(p["tokens"] for p in packet) == 62Performance and operating cost
This bounded pass costs O(K) time and O(D) counters for K candidates and D revisions. Tokenization and model context cost dominate. Greedy packing can miss a better combination; add measured coverage-aware selection only if judged queries expose that ceiling.
Common Mistakes
- Spending the whole context window on passages and leaving no response budget.
- Suppressing a newer contradiction because its score is lower.
- Confusing duplicate text with duplicate source identity.
- Citing a passage that was retrieved but not included in the final packet.
Validation drill
For 37 questions, label required facts and revisions. Pack under a 540-token evidence budget and measure how often all facts survive, especially exceptions and conflicts.
Read next
- Reranking: spend model budget on a bounded candidate window
- Claim-level citations: verify each answer against its passage
- Chunking documents: preserve section identity and answer boundaries
- Retrieval prompts: pack evidence and map claims
Continue this retrieval path
- Hierarchical retrieval: find a section before selecting its evidence
- Near-duplicate retrieval: collapse copies without hiding independent evidence
