Chapter 4 of 10
Construct a bounded evidence packet
Context engineering
About 4 minutes · includes examples, an exercise, and references
Chapter at a glance
- •An evidence packet is a versioned, budgeted data structure with provenance.
- •Selection balances relevance, coverage, authority, diversity, and coherence under hard policy.
- •Compression creates derived content that must retain spans and be verified.
Retrieval returns candidates; context construction chooses what the generator can inspect. This boundary is sometimes called context engineering because selection, ordering, formatting, compression, and provenance directly shape generation. More tokens are not automatically better. Irrelevant passages distract, duplicates crowd out coverage, conflicting revisions create ambiguity, and untrusted instructions can influence the model.
Define an evidence packet schema. Each item carries an opaque handle, stable unit and revision IDs, source title, heading path, locator, authority class, effective interval, retrieval evidence, text, token estimate, and policy labels. The packet also records question scope, corpus generation, construction policy, omitted-candidate reasons, and total budget. The generator sees only fields authorized for processing.
Selection is a constrained optimization. Desired properties include relevance, facet coverage, source authority, freshness, diversity, and coherence under a token budget. Hard constraints include access, jurisdiction, effective time, maximum item count, and forbidden source classes. A greedy policy can begin with the highest reranked candidate, then choose items with high marginal coverage and low redundancy. For multi-part questions, reserve evidence slots per subquestion so one easy facet does not consume the packet.
Adjacent expansion restores context around precise passages. If a selected unit begins mid-procedure or references a definition, add parent heading or neighboring units within a cap. Expansion must preserve identities and distinguish retrieved hit from supporting neighbor. Avoid automatically appending entire documents; that defeats passage retrieval and increases injection surface.
Deduplicate with provenance. Exact duplicates can share text while representing different versions or authorities. Group by content digest and source family, then select a representative consistent with query scope. Preserve the group so citations and conflict analysis remain possible. Near-duplicate templates may differ in one decisive number; similarity alone should not collapse them.
Ordering affects model attention. A stable policy may group by subquestion, place decisive authoritative evidence first, and explicitly separate conflicting sources. Do not rely on the generator to infer that a later revision supersedes an earlier one; encode dates and relations. Randomize order in evaluation to test sensitivity. If answer quality changes sharply, the system is brittle even when one chosen order scores well.
Compression can extract relevant sentences or create summaries, but a generated summary introduces another unsupported transformation. Prefer deterministic extraction when possible, retain source spans, and label derived text. If a model compresses evidence, verify its statements against the original unit and never cite the summary as if it were source. Measure compression recall: did it preserve every fact required by judged claims?
Fusion-in-Decoder provides an architectural alternative in which separately encoded passages are combined in decoder attention. It was designed to exploit many retrieved passages without simply concatenating them into one encoder input. The broader lesson is to evaluate how a generator consumes multiple evidence units, not assume concatenation is neutral. Current systems may use long contexts, but selection and provenance remain necessary for cost, security, and audit.
Treat retrieved text as data, not instructions. Use clear delimiters and evidence handles; instruct the generator that commands inside evidence are untrusted content. This is not a complete security boundary, because language models do not reliably enforce privilege separation. Minimize included text, remove active content where appropriate, run generation without broad tools, and verify output. Security is expanded in a later chapter.
The runnable packer demonstrates authority and time filtering, per-parent diversity, and a word budget. A production policy would use token counts and learned relevance, but deterministic constraints remain. Record why each candidate was omitted: denied, invalid time, duplicate, lower marginal value, or budget. That ledger is essential when a missing answer fact was retrieved but not passed to generation.
Evaluate context independently from the final answer. Context precision measures how much selected evidence is useful; context recall or coverage measures whether evidence needed for reference claims is present. Also report duplication, authority violations, conflict representation, token use, and selection latency. Inspect the smallest packet that supports a correct answer. Context construction succeeds when it supplies sufficient, trustworthy, legible evidence—not when it fills the window.
Key points
- An evidence packet is a versioned, budgeted data structure with provenance.
- Selection balances relevance, coverage, authority, diversity, and coherence under hard policy.
- Compression creates derived content that must retain spans and be verified.
- Evaluate whether evidence was retrieved but lost during packet construction.
Pack authorized diverse evidence under a budget
Read the expected behavior in the surrounding walkthrough, then copy and run this reference implementation.
def pack(candidates, allowed_sources, as_of, word_budget=80):
chosen, omitted, used, parents = [], [], 0, set()
for item in sorted(candidates, key=lambda x: (-x["score"], x["id"])):
words = len(item["text"].split())
if item["source"] not in allowed_sources:
omitted.append((item["id"], "source_policy")); continue
if not item["valid_from"] <= as_of < item["valid_to"]:
omitted.append((item["id"], "effective_time")); continue
if item["parent"] in parents:
omitted.append((item["id"], "duplicate_parent")); continue
if used + words > word_budget:
omitted.append((item["id"], "budget")); continue
chosen.append({**item, "handle": "E" + str(len(chosen) + 1)})
parents.add(item["parent"]); used += words
return {"evidence": chosen, "omitted": omitted, "words": used}
items = [{"id":"p1", "parent":"d1", "source":"manual", "score":.9,
"valid_from":1, "valid_to":99, "text":"Retry twice before rollback."}]
print(pack(items, {"manual"}, as_of=10))Exercise
Compare evidence-packet policies
Construct context for a question requiring three facets and two source types.
- Define schema, hard constraints, selection objective, adjacency, ordering, and omission reasons.
- Compare top-score-only, diversified, and facet-budgeted packets.
- Measure support coverage, precision, duplication, token use, and order sensitivity.
Success criteria
- All required facets fit without unauthorized or obsolete evidence.
- Every omission is explainable from recorded policy.
- Citations resolve through immutable handles, not model-generated URLs.
Reflect: Which retrieved passage is relevant but harmful to include?
References and further reading
- Leveraging Passage Retrieval with Generative Models for Open Domain Question AnsweringThe primary Fusion-in-Decoder paper on aggregating evidence from multiple passages.
- Retrieval-Augmented Generation for Knowledge-Intensive NLP TasksThe original RAG formulation combining parametric and non-parametric memory.
- Not What You've Signed Up For: Indirect Prompt InjectionThe primary security paper demonstrating indirect prompt injection through retrieved data.