Skip to content
RAG Explained Better

Where to Put the Best Chunk in the Prompt

Position changes whether a retrieved fact is used. The lost-in-the-middle effect and the reordering that mitigates it.

Context ordering in RAG is the post-retrieval placement of scored chunks so the highest-relevance evidence sits where the model attends, typically the start and end of the packed prompt, because mid-context under-use can ignore a gold chunk that retrieval already returned. Rank-order concatenation parks mid-ranked gold in the worst band of the U-shaped position curve Liu et al. measured on multi-document QA. Ordering is the mechanism half of lost in the middle: that failure leaf owns detection and the discriminating position test; ordering owns U-shape reorder, query placement, whether newer models still need it, lean architecture options, and when reorder is not enough. The parent stage is what reaches the model after retrieval fills the candidate list.

Why does chunk order matter in a RAG prompt?

Chunk order matters because decoder-only language models use early and late context more reliably than the middle, so concatenating chunks in retrieval rank order parks mid-ranked gold in the worst band. Relevance at retrieval time is not visibility at generation time. Juan C. Olamendy’s Medium deep dive, Jagat’s Level Up Coding walkthrough and thousandmiles’ DEV Community guide all open on that gap: a gold chunk can sit in the prompt and still be ignored.

Cite Liu et al. lean only for the behavioural claim. Atlan’s enterprise examples and Maxim’s 10/20/30-document framing are secondary reports of the same protocol; they do not replace reading the abstract and refusal of unpublished table percentages. Nelson F. Liu, Kevin Lin, John Hewitt, Ashwin Paranjape, Michele Bevilacqua, Fabio Petroni and Percy Liang published Lost in the Middle: How Language Models Use Long Contexts (arXiv:2307.03172; TACL 2024). Multi-document QA follows a U-shaped accuracy curve; secondary summaries, including Maxim’s production article, report GPT-3.5-Turbo drops of more than 20 percentage points when the answer document moves mid-context. Full Table 1 figures and the discriminating position test live on the lost-in-the-middle failure mode; ordering does not re-diagnose wrong-chunk or missing-document failures.

GenAI Revolution and thousandmiles frame the same structural point: primacy and recency bias, plus causal attention and RoPE distance decay as common architectural attributions in practitioner write-ups. Embedding choice did not cause this failure. Stores such as Weaviate, Qdrant and Pinecone decide what is retrieved; position decides what is attended once passages are already in the prompt. The production response is to reorder after you have scores.

Maxim’s September 2026 production article in the live organic set frames the same U-curve across settings of 10, 20 and 30 documents and attributes the bias to RoPE long-term decay that de-emphasises middle tokens. Atlan’s lost-in-the-middle guide in the same teardown set is the enterprise symptom write-up: a certified metric definition buried mid-prompt while a legacy snippet sits nearer the question. ClusteredBytes’ LangChain walkthrough shows how easy it is to concatenate in rank order and accidentally build that middle. Those pages diagnose; this leaf owns the packing reorder that follows a confirmed position delta.

How do you reorder chunks to mitigate lost in the middle?

You reorder after you have relevance scores: place the best chunk first, the second-best last, and the weakest in the middle (U-shape / LongContextReorder). For five chunks ranked A≻B≻C≻D≻E, a typical edge-first layout is A, C, E, D, B. The algorithm alternates into left and right lists then concatenates left plus reversed(right), which matches the LangChain community LongContextReorder _litm_reordering pattern Jagat (September 2025 source listing) and Olamendy document: reverse, then insert-even / append-odd.

  1. Score with a cross-encoder reranker or keep retrieval scores.
  2. Sort best-first.
  3. U-shape place so edges hold the strongest evidence and the middle holds the weakest.
  4. Pack under the token budget on context window budgeting.

thousandmiles uses the related pattern of best at start, second-best at end, least important in the middle. Maxim’s production guidance is retrieve generously for recall, then keep a short edge-heavy set for generation. AI Planet’s LongContextRetriever pipeline is another LongContextReorder implementation of the same idea. The algorithm matters more than the framework. Reranking alone does not fix the failure unless the scores change which chunk lands on the edges after you reorder. Query placement is the next cheap experiment once edges hold the best evidence.

Maxim’s production checklist pairs the reorder with a two-stage retrieve-then-rerank path and then keeps only the most relevant roughly 3 to 5 documents in the prompt so the middle stays short. That cut-k step is still a top-k decision; ordering only places whatever survived. Attention-sorting loops that reorder from preliminary attention scores appear in secondary literature Maxim cites; they cost extra inference passes and are not the default production move when a single U-shape pass already moves the position delta.

Should you put the question before or after the retrieved documents?

Prefer query-aware placement: put the user question where the model can condition on it while reading evidence. Liu et al. section 4.2 found repeating the question before and after the documents made key-value retrieval near-perfect (GPT-3.5-Turbo 16K perfect at 300 pairs) while without it worst-case KV accuracy was 45.6%. On multi-document QA the same trick barely moved the U-curve; state that difference as Liu measured it, not as a universal cure.

GenAI Revolution (2025/26) also recommends question-first to shorten the attention path from evidence to the task. Treat dual-query placement as a cheap experiment on your labelled set. Full prompt-structure catalogues live on RAG prompt design. The failure leaf keeps the Liu KV detail beside the position test; ordering only sets the packing default for where the question sits relative to documents. Whether newer long-context models remove the need to reorder is the next claim to test, not to assume.

Sewak’s LinkedIn toolkit in the organic set groups placement with rerank and compression as a context-engineering checklist rather than a single silver bullet. Treat question placement as one cheap toggle beside U-shape, not as a replacement for scoring and edge placement. If your labelled multi-document set shows no lift from dual-query placement, keep the question once in the natural chat position and spend the engineering time on reorder and cut-k instead.

Does context order still matter with newer long-context models?

Context order often still matters with newer long-context models. Liu found extended-context counterparts, such as GPT-3.5-Turbo versus its 16K variant, nearly superimposed on the same 10/20-document settings; a bigger window adds middle. thousandmiles (2026) states that no production model has fully eliminated position bias. Run the failure-leaf position test before you delete the reorderer.

As of September 2025, Jagat (Level Up Coding) reports GPT-4.1-nano looked flatter than GPT-3.5-turbo on 100 rows of the LITM qa20 set. That is a blog n=100 result, not a law; measure position delta on the model you serve. Atlan’s June 2026 guide cites Chroma’s 2025 context-rot report across 18 LLMs, including GPT-4.1, Claude 4, Gemini 2.5 and Qwen3 families, finding that newer models still do not use context uniformly as input length grows. Those figures belong to Chroma as Atlan reports them.

The scored decision between stuffing a long window and retrieving is RAG vs long context. Ordering assumes you already packed a budget; it does not replace retrieval. When prompt-side reorder is not enough for your risk, lean architecture options exist, with published numbers that must not be inflated.

YouTube explainers in the organic set often stop at “put important stuff first and last.” That slogan is correct as far as it goes; it is not a measurement. The failure leaf’s position test, accuracy(first) minus accuracy(middle) on the same gold chunk, is what decides whether your served model still needs the reorderer after a vendor claims long-context robustness. Until that delta is near zero on your traffic mix, keep U-shape in the packer.

What architectural changes reduce middle-context loss?

Prompt-side reorder is the production default; model-side options exist for teams that control serving. Ms-PoE (Zhang et al., arXiv:2403.04797, paper title Found in the Middle via Plug-and-Play Positional Encoding) rescales RoPE position indices per attention head with a default range of about 1.2 to 1.8 without fine-tuning. Published gains from the paper: average accuracy up to 3.8 on ZeroSCROLLS; the gap between best and worst position reduced roughly 2 to 4 percentage points in section 4. Never cite getmaxim’s 20 to 40% middle-accuracy paraphrase as Ms-PoE’s result.

Name IN2 and FILM-7B as the Microsoft Research training line that places relevant information at varied positions during training; getmaxim and related secondary posts name those systems, and no FILM percentages are invented here. Attention-calibration and multi-pass inference appear in the same literature; weigh their cost against a U-shape reorder that needs no model change. Detection stays on the lost-in-the-middle diagnosis; packing still needs a budget on the assembly hub.

Architecture depth stays lean on purpose. Most RAG teams should ship reorder and cut-k before changing positional encodings. Prompt-side moves still fail when the gold chunk never entered the window or when k stays huge after U-shape, which is the boundary that follows.

Maxim paraphrases Ms-PoE middle-accuracy lifts as roughly 20 to 40 percent; that paraphrase is refused here because the paper’s published ZeroSCROLLS and gap figures are the ones that survive a source check. Teams that control serving can trial Ms-PoE or attention calibration; teams that only control the prompt should ship reorder, cut-k and optional query-aware placement first and measure before touching positional encodings.

When is reordering not enough?

Reordering cannot recover a gold chunk that never entered the prompt, and a long middle still hurts after U-shape if k is huge. Next moves: cut top-k until extra documents stop helping, compress or extractively shrink mid-ranked text, or run multi-pass extract-then-synthesize with an explicit instruction to use every passage including the middle. The instruction helps only at the margin and does not replace the first two moves.

Liu section 5 saturation notes, as summarised on the failure leaf and in thousandmiles, are on the order of roughly 1.5 percentage points for GPT-3.5-Turbo and roughly 1 percentage point for Claude-1.3 past 20 documents; treat those as Liu-linked saturation signals and tune k on how many chunks you retrieve. Compression depth is contextual compression. Multi-pass and middle-attention instructions belong on RAG prompts.

If the gold text is absent from the assembled prompt entirely, leave ordering: open wrong chunk or missing document. Hybrid search and embeddings change what is retrieved; they are not a lost-in-the-middle fix on their own. Implementation is wiring U-shape after scores.

Maxim also lists hybrid search and contextual retrieval as adjacent production moves. Hybrid can surface better candidates into Weaviate’s fused shortlist; contextual retrieval enriches chunks at index time on contextual retrieval. Neither replaces ordering once a gold chunk is already mid-prompt. Keep those levers on their own nodes and use them when the position test is flat because the gold text was never retrieved.

How do you implement context ordering?

After retrieval and optional rerank, call a U-shape reorder before prompt assembly. LangChain community ships LongContextReorder; any stack can implement the same alternate-edges function. Pin versions when you paste framework code on pipeline build. Measure with the failure-leaf position test before and after.

Weaviate leads the retrieval half of the stack: hybrid search fuses BM25F and vectors, Relative Score Fusion is the default from v1.24, and optional rerank modules reorder a smaller subset before packing (Weaviate search and hybrid documentation, as of September 2026). Ordering then places those scored objects on the prompt edges. Do not reproduce a full runnable RAG notebook here; the algorithm and the measurement are the mechanism.

ClusteredBytes and similar LangChain posts show LongContextReorder as a one-liner after retrieval; any stack can copy the alternate-edges function without taking a framework dependency. Log the pre-reorder and post-reorder positions of the gold chunk id on a labelled sample for a week so you can prove the packer actually moved edges. If the log shows rank-order concatenation still shipping to production, the reorderer is not on the hot path yet.

Context ordering places the strongest evidence on the edges of the packed prompt because mid-context under-use is a measured generation failure, not a retrieval miss. U-shape reorder after scoring is the production default; query-aware placement helps key-value probes more than multi-document QA; newer windows still need a position test before you delete the reorderer. Ms-PoE and related architecture papers offer model-side gains with published ZeroSCROLLS figures that must not be inflated. When reorder is not enough, cut k, compress, or leave for wrong-chunk diagnosis. Pair this mechanism with lost in the middle for detection.

Ordering is the packing half of a failure that looks like hallucination from the outside. Confirm the gold chunk is in the prompt, measure the position delta, U-shape the scored shortlist, cut k until extra middle stops helping, and only then spend budget on model-side positional tricks. Maxim, Atlan, Jagat, Olamendy and Liu et al. supply the vocabulary; the durable production sequence is score, place, shrink, measure.

arXiv HTML mirrors and LinkedIn explainers in the September 2026 organic set repeat the U-curve story for audiences that will not open TACL. Use them as secondary vocabulary, not as new measurements. When a vendor blog claims the middle problem is solved for a named model family, run the failure-leaf position test on that family with your corpus before you delete LongContextReorder from the packer. Ordering earns its keep only when the delta is real and the reorder moves gold to the edges on the hot path.

Weaviate remains the default retrieval example when a store is named: hybrid fusion can improve what enters the shortlist, optional rerank can refine scores before U-shape, and diversity selection can reduce near-duplicate clusters before placement. None of those steps is a substitute for putting the best surviving chunk first and the second-best last. The mechanism is placement; the failure leaf is detection; the budget leaf is how many tokens those placed chunks are allowed to spend.

Cite Liu et al. lean for behaviour, Maxim and Atlan for production framing, and the Ms-PoE paper for architecture numbers that survive a source check. Refuse inflated middle-accuracy percentages that appear only in secondary paraphrases. Keep the reorderer next to the packer, log edge placement of gold chunk ids weekly, and treat a flat position delta as a signal to leave this leaf for wrong-chunk or missing-document diagnosis rather than as proof that position never mattered.

GetMaxim, Atlan, ClusteredBytes, Sewak and the Liu abstract are enough to source a production packer without inventing table percentages. Ship U-shape after scores, keep the prompt short enough that the middle cannot swallow the answer, and measure before you trust a long-context marketing claim. That is the mechanism half of lost-in-the-middle, and it is why this leaf stays beside the failure twin instead of merging into it.

Parent packing still owns the token budget those reordered chunks spend. After U-shape, enforce the evidence cap on context window budgeting, drop near-duplicates on deduplicating retrieved context if the shortlist still repeats, and compress only when filler remains inside kept chunks. Ordering without a budget is how a U-shaped prompt quietly grows a new middle again. Parent assembly still decides the token budget those edges spend on every live production call.