Blog

R

27/07/2026

Stop Losing Context: Practical Fixes for Problems in RAG 

You finally built the RAG pipeline correctly. The retriever found the right documents. The relevant information is sitting right there in the context window. The model still got the answer wrong. 

This is the “lost in the middle” problem, and three years after Liu et al. first documented it at Stanford and UC Berkeley, it remains one of the most consistent production failures in RAG systems. The original paper showed that LLMs achieve highest accuracy when relevant information appears at the beginning or end of the context window — and that performance degrades by more than 30 percentage points when critical information is in the middle. 

What’s changed since 2023? Models now support million-token context windows. The research community has proposed several mitigations. And the problem still hasn’t gone away. 

This piece covers what’s actually been solved, what hasn’t, and what the fixes look like in 2026. 

What the 2025–2026 Research Actually Shows 

The MIT 2025 follow-up research on causal masking — sometimes called “Found in the Middle” — finally explained the architectural mechanism behind the U-shape with precision. The core finding: position bias in transformer models isn’t just about distance decay in positional encodings. It’s structurally embedded in causal attention itself, where each token can only attend to tokens that came before it. Tokens in the middle have a fundamentally different attention profile than tokens at the edges. 

The U-shaped attention curve

A 2025 paper on layer-specific scaling of positional encodings showed that applying layer-specific scaling to enhance middle-context attention produced an average accuracy improvement of +20% on key-value retrieval tasks and +2.7% on multi-document question answering — without retraining. This is meaningful progress. It’s also not yet in any production model you’re using today. 

A broader 2025 survey on transformer context extension reviewed the full landscape of mitigation approaches — from efficient training schemes to inference-time context extension methods — and concluded that even models specifically trained for long-context use still exhibit the lost-in-the-middle problem. Extended context windows reduce the severity. They don’t eliminate the underlying bias. 

The bottom line as of mid-2026: no production model has fully eliminated position bias. The architectural mechanism is structural. The question isn’t whether your model is affected — it’s how much, and what you can do about it now. 

The Myths Worth Debunking in 2026 

“Long context windows solve this” 

This is the most persistent misconception, and it’s understandable — if the model can hold 200,000 tokens, surely it can pay attention to all of them? 

The research says otherwise. A 2025 analysis by Yen et al. tested frontier long-context models including Claude 3.5 and GPT-4o on needle-in-a-haystack tasks at 200-document contexts. The U-shape was still clearly measurable. Edge positions showed 90%+ recall. Middle positions showed a statistically significant dip — the exact magnitude varying by model and chunk size, but consistently present. 

The Atlan analysis published June 2026 summarizes the current state well: newer models have improved long-context capacity, but they still do not use every position equally. Research on context rot and effective context windows shows that performance can degrade before the advertised token limit. Larger windows still need selection, ordering, compression, and governance. 

Longer windows change where the problem lives and reduce its visible severity. They don’t make context position irrelevant. 

“New attention calibration techniques have solved this in production” 

Techniques like Multi-scale Positional Encoding (Ms-PoE) and attention calibration — covered in the “Found in the Middle” paper — do reduce the bias in controlled research settings. The +20% accuracy improvement from layer-specific scaling mentioned above is real and reproducible. 

But as of 2026, none of these techniques are deployed in the production LLMs most teams are using. They’re research results, not product features. The gap between “this works in a research paper” and “this is live in the OpenAI or Anthropic API you’re calling” is significant. Applying academic mitigations to your RAG pipeline means waiting for providers to adopt them or fine-tuning your own models — neither of which is a near-term option for most teams. 

“Agentic frameworks have made this irrelevant” 

This argument has some merit, and it’s worth taking seriously. A 2025 analysis in Towards AI makes the case that instead of a linear retrieve-rerank-generate flow, agentic AI operates in a reason-act-observe loop. An agent can recognize when its attentional focus is cluttered, decompose the question, retrieve different sub-contexts iteratively, and assemble an answer across multiple passes rather than trying to attend to everything in one long prompt. 

That’s accurate — and it’s genuinely one of the more promising architectural directions for long-context problems. But it applies to agentic systems specifically. Most RAG applications in production in 2026 are not agentic. They’re single-pass retrieve-then-generate pipelines. For those systems, position bias remains a live problem that no architectural shift has eliminated. 

“Randomizing document order fixes the bias” 

No. The bias is positional, not content-based. Randomizing order randomly assigns relevant documents to middle positions, which on average makes things worse. You want deliberate ordering based on relevance — the opposite of random. 

The Fixes: What Actually Works in 2026 

Fix 1: Strategic document reordering — still the highest-leverage, lowest-effort fix 

Nothing has replaced this. The research consensus from Liu et al.’s original paper through the 2025 follow-on work remains consistent: placing the most relevant document first and the second most relevant document last, with supporting context filling the middle, recovers a significant portion of accuracy lost to position bias. 

LangChain implements this as LongContextReorder. The underlying logic: the model attends most strongly to the beginning and end of the context window. Give it what it needs most at both edges. 

Implementation cost: a few lines of sorting logic in your context assembly step. No architecture changes, no re-indexing, no additional latency. This is still the first fix to implement because the cost is near zero and the impact is measurable immediately. 

What’s new in 2026: the Position Engineering paper (He et al., 2024) showed that systematic positional information manipulation — not just simple first/last ordering but deliberate placement of different information types at different positions — can further boost performance. For teams that want to go beyond simple relevance-based ordering, this is worth reading. 

Fix 2: Reduce retrieved context volume — the case has gotten stronger 

The argument for retrieving fewer documents has only gotten stronger as context windows have gotten longer. The instinct to “use the full context window” leads teams to pass 50 or 100 chunks when 3 to 5 highly relevant ones would produce better results. 

The Towards AI analysis frames this well: the best enterprise fix is governed context delivery — fewer, higher-signal context objects, ranked by relevance, placed intentionally. Prompt tactics help, but durable improvement comes from controlling what goes into the context before the model starts reasoning. 

Shi et al.’s work on distracting context showed that irrelevant-but-plausible passages in the context window significantly degrade model performance even when the correct answer is also present. Every additional chunk you pass is a potential source of distraction — and a potential source of middle-position penalty if it pushes relevant content away from the edges. 

Practical starting point: if you’re currently passing 15–20 chunks, cut to 5 and measure accuracy. Most teams see improvement, not degradation. 

Fix 3: Two-stage retrieval with cross-encoder reranking — now table stakes for production 

What was an advanced technique in 2023 is now a standard production pattern. Two-stage retrieval works as follows: use your dense retriever to pull a broad candidate set of 20–30 documents, then run a cross-encoder reranker to re-score and rerank based on precise relevance to the query. 

Cross-encoders attend to both the query and the document simultaneously, which produces significantly more accurate rankings than first-stage semantic similarity. Cohere’s Rerank API and the ms-marco-MiniLM models on Hugging Face are the most widely used options. 

What’s new in 2026: RankRAG (Yu et al., 2024) proposed unifying context ranking with RAG generation, training the LLM to jointly rank and generate rather than treating them as separate steps. This showed state-of-the-art results on several benchmarks. It’s not widely deployed in production yet, but it’s the direction the field is moving — from separate retrieval and generation to unified ranking-generation. 

Then apply Fix 1 to the reranked results — place the top result first, second-most-relevant last, fill the middle with the rest. The combination of reranking and strategic ordering is the most reliable pattern for production systems in 2026. 

Fix 4: Contextual retrieval and smarter chunking — the re-indexing investment worth making 

Anthropic’s contextual retrieval approach (released late 2024) adds a short contextual summary to each chunk before embedding. The summary tells the embedding model where in the document the chunk appears and what broader topic it relates to, which significantly improves retrieval precision. 

The AutoRAG paper (Choi et al., 2024) benchmarked chunking strategy variations and found recall differences of up to 9% between fixed-size and semantic chunking on the same corpus. 

What’s new in 2026: LIFT (Long Input Fine-Tuning, Mao et al., 2025) proposes improving long-context understanding through fine-tuning on long-input examples rather than improving chunking. This is a model-level fix rather than a pipeline-level fix — relevant for teams fine-tuning their own models, less relevant for teams using commercial API endpoints. 

For most teams using commercial models: invest in chunking improvements before model-level fixes. The pipeline-level gains from contextual retrieval are more immediately actionable. 

Fix 5: Multi-query retrieval for complex questions — increasingly automated 

Multi-query decomposition is more accessible in 2026 than it was two years ago. LangChain’s MultiQueryRetriever generates sub-queries from the original query automatically. What used to require custom implementation is now a one-line setup. 

The underlying value hasn’t changed: for queries that require information from multiple sections of a document or multiple documents, single-query retrieval systematically misses relevant content. Decomposing into sub-queries retrieves more complete context. 

The trade-off remains: at least 2× the retrieval latency. Use this for query categories that consistently fail single-query retrieval, not universally. 

Fix 6: Agentic context management — the emerging fix for complex multi-document tasks 

This is the most significant change from the 2023 version of this problem. For complex, multi-hop tasks — where the answer requires reasoning across multiple documents or sources — agentic approaches that iteratively retrieve and reason have shown substantially better results than single-pass RAG. 

The PAM QA approach (Never Lost in the Middle, 2024) trained models to use position-agnostic decomposition: rather than attending to one long context, models learn to decompose queries, retrieve sub-contexts, and assemble answers across multiple passes. The results on long-context QA benchmarks were significantly better than single-pass approaches. 

For production systems: this doesn’t mean you need to rebuild your RAG pipeline as an agent overnight. It means that for the subset of queries in your system where single-pass retrieval consistently fails — usually complex multi-hop questions — a targeted agentic retrieval loop is now a practical option rather than a research experiment. 

How to Detect and Measure the Problem in Your System 

The detection methodology is straightforward and worth running before implementing any fixes. Place a known relevant document at position 1 in your context window and measure accuracy. Repeat with the same document at positions 5, 10, 15, and 20. Plot the accuracy curve. 

If you see the U-shape — high accuracy at positions 1 and 20, degraded accuracy at positions 5–15 — you have quantified the problem. The depth of the dip tells you how much accuracy you’re losing from position bias and how much the fixes above are likely to recover. 

The Yen et al. (2025) long-context analysis provides a replication framework you can adapt to your specific retrieval configuration and query distribution. 

What to add to your evaluation pipeline: Track context recall by chunk position over time. Did implementing strategic reordering flatten the U-shape? Did the cross-encoder reranker reduce the number of relevant chunks landing in middle positions? These are the metrics that tell you the fixes are working — not just aggregate faithfulness or groundedness scores, which can look fine even when position bias is degrading specific query categories. 

This is where qAPI’s RAG evaluation module provides direct value. The chunking strategy A/B comparison lets you measure the impact of a retrieval configuration change without re-indexing production — run both configurations against your eval set and compare recall at each position.  

Index freshness monitoring catches the related failure mode where stale retrieval produces confident answers about outdated information. And because qAPI sits alongside your API testing, when a retrieval endpoint changes its response structure and causes a quality drop, that surfaces in the same report as your RAG metrics rather than requiring a separate investigation. 

The Implementation Sequence for 2026 

The right order hasn’t changed, but the context around each step has: 

week

Week 1: Run the position-detection test. Quantify the U-shape on your actual queries. If the accuracy drop at middle positions is under 5%, other optimizations are higher priority. If it’s over 10%, Fix 1 and Fix 2 are urgent. 

Week 2: Implement strategic document reordering (Fix 1). A few lines of sorting logic. Measure the U-shape again. For most teams, this alone recovers 50–70% of the accuracy lost to position bias. 

Week 3: Reduce retrieved context to 3–5 highly relevant chunks (Fix 2). Measure. Most teams see further improvement. 

Week 4+: Add cross-encoder reranking (Fix 3) if the residual gap justifies the latency cost. Pairwise comparison with and without reranking on your eval set should make this decision straightforward. 

Ongoing: Invest in contextual retrieval and chunking improvements (Fix 4) when you have operational bandwidth to re-index. Evaluate agentic approaches (Fix 6) for specific query categories where single-pass retrieval consistently fails. 

At each step, re-run the position-detection test. You want to see the U-shape flattening. 

The Takeaway 

The lost-in-the-middle problem looks different in 2026 than it did in 2023. The research community understands the architectural cause with much more precision. Mitigation techniques like attention calibration and layer-specific positional scaling show real promise. Agentic frameworks offer a structural alternative for complex multi-hop tasks. 

What hasn’t changed: no production model you’re deploying against has fully eliminated position bias. The U-shape is still there. The accuracy drop at middle positions is still measurable and still consequential. 

The teams that know about this problem and measure it have a clear, actionable path to improving answer accuracy without touching the model. The teams that assume million-token context windows solved it are flying blind on an accuracy deficit they don’t know exists. 

Run the detection test. Quantify the dip. Fix it layer by layer. It will work with qAPI. 

Author

Author Avatar

R

    Debunking the myths around API testing

    Watch our live session where we debunked common myths around API testing — and shared how teams can simplify it with qAPI

    Watch Now!