Context Packing for AI Systems: Ordering, Compression, and Evidence Loss
That gap — between what retrieval found and what the model used — is where context packing lives. It is the assembly stage between retrieval and…

Research updated Sep 10, 2026
Key topics
The retrieval log shows the right chunk at rank 2. The answer is still wrong.
That gap — between what retrieval found and what the model used — is where context packing lives. It is the assembly stage between retrieval and generation, and it is the stage most teams never instrument. They tune embeddings, swap rerankers, rewrite prompts, and keep losing the same way: the evidence was in the window, and the model answered as if it were not.
The weak mental model underneath that frustration is simple: if it's in the window, the model sees it. The stronger model is less comfortable. The context window is a budgeted, ordered, lossy channel. Packing decides what survives the trip.
This article assumes you already run retrieval and long-context calls, and that you have some way to measure whether retrieval found the right evidence. If that measurement is missing, fix it first — packing work is only meaningful once you can tell whether the failure happened upstream or here.
The Retrieval Was Right and the Answer Was Still Wrong

A correct retrieval result can be destroyed during assembly. Three loss channels do most of the damage.
Positional loss. Attention over long inputs is not uniform. Long-context evaluation work has repeatedly found that models use evidence near the beginning and end of a long input more reliably than evidence in the middle — the lost-in-the-middle pattern. The same chunk that answers the question cleanly at position 1 can fail at position 40. Treat this as a measured tendency, not a fixed law: how much it holds depends on the model, the task, and the prompt format, and it is worth confirming on your own traffic rather than assuming.
Compression loss. Summarization discards information, and it does not discard randomly. It drops whatever the compressor judged non-essential — which is exactly where decisive details live: numbers, negations, qualifiers, units, entity names.
Dilution loss. Near-duplicate chunks from overlapping splits consume budget and can create a false-consensus effect: the model sees the same claim three times and treats repetition as corroboration. That is a hypothesis about model behavior, not a documented certainty, but it is cheap to test — deduplicate and see whether confidence and accuracy move. Meanwhile the one chunk that contradicts the repeated claim sits at the edge, unread.
None of these are retrieval-quality problems. That measurement belongs upstream, and conflating the two sends teams to fix the wrong stage — usually by re-embedding a corpus that was never the bottleneck.
My working rule: before changing embeddings or rerankers, log what the model actually received, in order, with token counts. Most teams discover the decisive chunk was present, compressed, and buried. That is a packing bug, not a search bug.
What Packing Actually Controls
Packing is a scheduling problem under a hard capacity constraint. It is not a formatting problem, and it is not prompt writing with better manners.
The boundary matters, because selection and ranking are easy to blur. Retrieval produces a candidate set with relevance scores. Packing works inside that set: it decides admission, deduplication, role-based caps, representation, and placement. It does not redesign retrieval or rerank the candidates — that is upstream work, and if the right evidence never reached the candidate set, no packing policy will recover it.
Within that boundary, four decisions are in play, and they are separable:
- Admission — which candidates from the retrieved set enter the window at all.
- Ordering — where each admitted item lands.
- Representation — verbatim, compressed, or structured into fields.
- Budget — how many tokens each item may consume.
Teams routinely tune one and assume they have tuned all four. They reorder without deduplicating, or compress without reserving space for the instruction, and then attribute the result to the model.
The artifact worth producing is an ordered manifest: item IDs, token counts, provenance, and the compression ratio applied to each. When an answer fails, the manifest is what lets you say which stage lost the evidence instead of guessing. Build the logger before you build the policy — you cannot debug a channel you cannot see.
Ordering: Position Is a Resource
If attention were uniform, ordering would be cosmetic. It is not. Position behaves like a finite resource that gets allocated, and the allocation is worth testing rather than assuming.
Three ordering policies are worth testing against your own traffic:
Highest-relevance-first. Simple, matches most reranker output, and works when the top item is genuinely decisive. It fails when the decisive item is rank 4 and the top three are plausible distractors.
Query-adjacent placement. Put the strongest evidence immediately before the instruction or question. This keeps the decisive material in a high-attention region and close to the moment of generation.
Edge-anchored placement. Reserve the first and last slots for the single most decisive item and the output contract. Everything supporting goes in the middle, where it can be skimmed without displacing the anchor.
Ordering also interacts with instruction position. If the task instruction sits at the top and evidence trails behind it, the model may commit to an interpretation before it has read the evidence — a plausible mechanism, not a settled finding. Moving the instruction after the evidence, or restating the output contract at the end, is a candidate policy worth testing rather than a default to adopt.
The test is cheap: hold the item set constant, permute the order, measure answer accuracy. If accuracy swings, ordering is a live variable in your system. If it does not swing, you have learned something too — stop spending engineering time on it.
Compression: What Summarization Quietly Deletes
Compression buys budget by discarding information. The bill arrives later, in a specific shape.
The failure signature is distinctive: the answer is fluent, plausible, and wrong in exactly one detail. The date is off by a year. A "must" became a "should." A threshold of 500 became "around 500." That pattern points at compression, not retrieval — the model had material, just not the material that mattered.
Compression strategies differ in how much meaning they are willing to rewrite:
- Deduplication and boilerplate stripping. Highest return, lowest risk. Near-duplicate chunks and repeated headers are pure budget waste. Do this before anything else.
- Extractive sentence selection. Cheap, preserves original wording, keeps noise. Safe when the decisive sentence is identifiable; useless when the answer requires combining two sentences.
- Structured extraction into fields. Lossy but auditable. You can see exactly which field was dropped, which makes failures attributable.
- Abstractive summarization. Aggressive, rewrites, and can invert meaning. A negation dropped in paraphrase flips the answer. This is the highest-risk move on the list.
The decision boundary is the task, not the token count. Compress when the task tolerates paraphrase — background, narrative, general description. Keep verbatim when the task depends on exact strings, numbers, identifiers, or legal and technical wording. If a human reviewer would object to the paraphrase in a contract, do not let a summarizer produce it.
Duplication, Distraction, and the Cost of Extra Tokens
A full context window is not a free win. It has three costs that rarely show up in retrieval metrics.
False consensus. Overlapping chunk splits mean the same claim appears three times. If the model reads repetition as corroboration, it gains confidence it has not earned. Deduplication is not just a budget optimization; it is a correctness fix worth testing directly.
Distraction. Plausible but irrelevant passages pull generation toward their vocabulary and framing even when the correct evidence is present. The model answers in the register of the loudest document, not the right one.
Serving cost. Longer contexts raise prefill work and KV-cache pressure. KV cache is the stored attention state a model keeps for tokens it has already processed; its size grows with context length, and it competes for the same memory that serves concurrent requests. The magnitude of the effect depends on model architecture, serving implementation, batching, and cache reuse — so measure time-to-first-token, memory, and throughput on your actual stack rather than assuming a uniform cost curve.
The rule I use: add context only when you can name the failure it prevents. If you cannot finish the sentence "this chunk prevents the model from ___," you are paying latency and attention for noise.
A Packing Policy You Can Implement and Test
Here is the procedure, in order. The sequence matters because each step changes what the next one sees.
- Deduplicate. Collapse near-identical chunks and strip repeated boilerplate. Do this first; it is the cheapest budget you will ever recover.
- Admit from the retrieved set. Use your existing relevance signal to decide which candidates enter the window. Do not re-rank here — that is upstream work.
- Assign an evidence role. Tag each admitted item as decisive, supporting, or background at retrieval time. Role determines both budget and placement, which makes the policy explainable to a reviewer who was not in the design meeting.
- Allocate budget by role. Decisive items get verbatim space. Supporting items get a smaller cap. Background gets whatever remains, and often deserves nothing.
- Reserve a fixed slice for the instruction and output contract. Packing must not be able to starve them. If the budget is tight, cut evidence, not the contract.
- Order. Anchor the decisive item at an edge, place supporting material in the middle, and keep the instruction adjacent to the evidence it governs.
- Compress only what exceeds budget. Compression is the last resort, not the first move. Apply it to the items that overflow, and log the ratio.
Instrument all of it: the manifest, per-item token counts, compression ratios, and final ordering. When an answer fails, you should be able to point at the stage that lost the evidence.
When this is overkill: single-document, short-context tasks with one obvious source. Packing policy earns its complexity when multiple sources compete for a fixed window. Below that threshold, it is ceremony.
How to Tell Which Stage Broke
Attribution beats intuition. Run the same query through four variants, changing one packing decision at a time:
- (a) Full packed context — your production configuration.
- (b) Top-ranked item only — strips dilution and distraction.
- (c) Reversed order — isolates positional effects.
- (d) Uncompressed originals — isolates compression loss.
Read the results as a ladder, and convert each result into a conditional action rather than a one-off observation:
- If (b) beats (a), your packing is adding harm. The evidence is not missing; it is being crowded out. Introduce a marginal-evidence cap or a distractor filter, and re-run the harness to confirm the cap helps rather than hurts.
- If (d) beats (a), compression is the loss channel. Look at what the summarizer dropped, and route high-risk evidence — numbers, negations, identifiers — verbatim while compressing only the low-risk material.
- If order permutation changes the answer, positional placement is the loss channel. Maintain a model- and task-specific placement policy and add it to your regression suite so a model upgrade does not silently invalidate it.
- If all four fail, the problem is upstream or downstream of packing. Stop tuning packing and go back to retrieval evaluation or the generation contract.
Keep a small, fixed set of representative queries with known correct evidence. Ten queries you understand beat a thousand you do not. The harness is cheap to rerun after every pipeline change, and it turns packing from opinion into measurement.
What to Learn Next and What to Watch
The skill sequence is not arbitrary. Retrieval evaluation comes first, because packing work is only meaningful once you can measure what reaches the model. Assembly instrumentation comes second — the manifest logger and the ablation harness. Compression and ordering policies come last, because they are the decisions you can only make well after you can see their effects.
Build the smallest useful artifact: a packing manifest logger plus a four-variant ablation harness. That combination is the difference between tuning by feel and tuning by evidence.
Some open questions are worth holding honestly. How positional effects shift as models and attention implementations change is not settled. Whether structured packing during training changes the ordering rules that apply at inference is an active research question — work on structured packing for long-context training suggests that how documents are collated during fine-tuning affects long-context utilization downstream, but that is a research signal, not an operating parameter for inference-time packing. And much of the published long-context behavior comes from controlled benchmarks, not production retrieval traffic. Treat those results as directional, not as settled numbers you can inherit.
The watchpoint is on the cost side. As serving stacks optimize KV-cache handling and prefill batching, the economics of a longer context will move. Re-measure rather than inheriting last year's budget.
Pack for the failure you can name. Log the manifest so failures are attributable. Re-measure ordering and compression whenever the model or the serving stack changes. That is the whole discipline — and it is a discipline, not a prompt-writing preference.


