Lost in the Middle: How Language Models Use Long Contexts

Nelson F. Liu, Kevin Lin, John Hewitt, Ashwin Paranjape, Michele Bevilacqua, Fabio Petroni, Percy Liang

As language models (LMs) boast ever-expanding context windows—growing from 2,048 tokens to 100,000 and beyond—the AI community has largely assumed that "more is better." We imagine these models as digital librarians capable of perfectly indexing every word in a massive document. However, a recent paper from researchers at Stanford, UC Berkeley, and Samaya AI titled "Lost in the Middle" suggests that our digital librarians might actually be skimming the beginning and end while dozing off in the middle.

TL;DR

Language models struggle to effectively use information buried in the middle of long input contexts, exhibiting a "U-shaped" performance curve. While they excel at retrieving information from the very beginning (primacy bias) or the very end (recency bias) of a prompt, their accuracy plummets when the relevant data is located in the center, even for models specifically designed for long contexts.

What Problem Does This Paper Address?

The rise of Retrieval-Augmented Generation (RAG) has made long-context performance critical. In RAG, a system retrieves dozens of documents to help an LM answer a query. The standard assumption is that providing more retrieved documents increases the chance of including the answer, thereby improving performance.

However, the authors identify a gap in our understanding: we know LMs can take long inputs, but we don't know how well they reason over them. If a model's ability to use information depends on where that information is placed, then simply expanding context windows or retrieving more documents might actually be counterproductive.

Methods

The researchers conducted controlled experiments across several state-of-the-art models, including GPT-3.5-Turbo, Claude-1.3, and open-source models like MPT-30B-Instruct and LongChat-13B.

They used two primary tasks to stress-test context usage:

  1. Multi-Document Question Answering: The model is given $k$ documents, only one of which contains the answer to a specific question. The researchers systematically varied the position of the "gold" document within the list.
  2. Key-Value Retrieval: A synthetic task where the model is given a JSON-formatted list of UUID pairs (keys and values) and asked to retrieve the value associated with a specific key. This removes linguistic "shortcuts" and tests raw retrieval capability.

By varying the total number of documents/pairs and the position of the relevant information, they could map out exactly where the models were most and least effective.

Datasets

  • NaturalQuestions-Open: Used for the multi-document QA task, providing real-world queries and Wikipedia-based answers.
  • Synthetic Key-Value Dataset: A newly introduced benchmark consisting of unique, randomly generated 128-bit UUIDs to test retrieval without the influence of language patterns or prior knowledge.
  • Wikipedia: Used to provide "distractor" documents for the QA task.

Key Insights and Ideas

  • The U-Shaped Curve: Across almost all models and tasks, performance follows a U-shape. Accuracy is highest when the relevant information is at the very beginning or very end of the prompt.
  • Primacy and Recency Bias: The "recency bias" (remembering the end) is common in decoder-only models, which are trained to predict the next token. The "primacy bias" (remembering the start) likely stems from instruction fine-tuning, where the task description is usually placed at the beginning.
  • "Long-Context" Models Aren't Immune: Surprisingly, models with 16K or 100K context windows performed almost identically to their 4K counterparts when tested on the same 4K context. Simply being able to see more doesn't mean the model is better at finding what it needs.
  • Architecture Matters: Encoder-decoder models (like Flan-T5) showed more robustness within their training sequence length because their bidirectional encoders allow every part of the context to "see" every other part. However, they too fell into the U-shaped trap once the context exceeded their training length.
Figure 1: The U-shaped performance curve of GPT-3.5-Turbo. Accuracy is high at the start and end but dips significantly in the middle.

Key Results

  • Performance Degradation: In the 20-document QA task, GPT-3.5-Turbo’s accuracy dropped from over 75% (when the answer was at the start) to below 55% (when the answer was in the middle).
  • Worse than "Closed-Book": In some cases, providing the model with 20 documents—one of which contained the answer—resulted in lower performance than asking the model the question with no documents at all (relying purely on its internal weights).
  • Saturation Point: For open-domain QA, the researchers found that model performance saturates long before retriever recall. Adding more than 20 documents provided only marginal gains (~1.5%) while significantly increasing latency and cost.
Figure 5: Multi-document QA performance across different context lengths (10, 20, and 30 documents). The U-shape persists and deepens as context grows.

Limitations and Future Work

The paper highlights a fundamental limitation in current Transformer-based LMs: they do not treat all parts of their context equally.

Future directions include: * Rethinking RAG: Instead of just retrieving more documents, systems should focus on reranking to ensure the most relevant information is at the beginning or end of the prompt. * Training Objectives: Developing new pre-training or fine-tuning objectives that force models to attend more uniformly to their entire context. * Evaluation: Moving beyond simple "needle-in-a-haystack" tests (which often only test the very end of the context) to more rigorous evaluations that probe the "middle" of the model's memory.

For engineers and researchers, the message is clear: if you want your model to remember something important, don't bury it in the middle.

Summary updated

BibTeX
@article{liu2024lost,
  author = {Nelson F. Liu and Kevin Lin and John Hewitt and Ashwin Paranjape and Michele Bevilacqua and Fabio Petroni and Percy Liang},
  title = {{Lost in the Middle: How Language Models Use Long Contexts}},
  journal = {Trans. Assoc. Comput. Linguistics},
  year = {2024},
  doi = {10.1162/tacl\_a\_00638},
}
Figure