Lost in the Middle: How Language Models Use Long Contexts
- Publication
- Transactions of the Association for Computational Linguistics 2024Journal
- First submitted to arXiv
- arXiv:2307.03172
Cite this paper · BibTeX
The citation identifies the linked source version. It cites the original paper and its authors.
@misc{modelevaluationlostinthemiddle2023,
title = {{Lost in the Middle: How Language Models Use Long Contexts}},
author = {Nelson F. Liu and Kevin Lin and John Hewitt and Ashwin Paranjape and Michele Bevilacqua and Fabio Petroni and Percy Liang},
year = {2023},
eprint = {2307.03172},
archivePrefix = {arXiv},
url = {https://arxiv.org/abs/2307.03172}
}
In 2023, language models' context lengths kept growing.
GPT-3.5-Turbo gained a 16K version, Claude gained a 100K version, and the open-source community was busy extending LLaMA's 2,048 tokens to 16K. The advertised numbers kept getting bigger, and a natural thought followed: if more documents fit, why not put them in?
But fitting something in and making use of it are two different things.
Does a model have the same chance of using a document placed first in a prompt as one placed tenth?
This paper from Stanford, UC Berkeley, and Samaya AI, later published in TACL, measures exactly that. The resulting curve is U-shaped: models perform best when the answer is at the beginning or end, and noticeably worse when it is in the middle.
Defining the Problem
The question the authors want to answer is:
- Can language models reliably use information anywhere in a long context?
If they can, moving relevant information from the first passage to the tenth should barely change performance.
This makes for a clean experimental design that keeps the question and answer unchanged and varies just two things:
- Where the relevant information appears.
- How long the context is.
Hold everything else fixed and watch the scores.
Solving the Problem
This is an evaluation paper. “Solving” the problem here means designing experiments that isolate and measure the position effect. The authors prepare two tasks, one close to a practical RAG scenario and another that removes as much meaning as possible, leaving only “find the identical string.”
Multi-Document Question Answering
The first task imitates generative search as it was commonly implemented at the time, giving the model a question and documents, of which only one contains the answer; the remaining are distractors.
The input looks roughly like this, excerpted from Figure 2 of the paper with omissions:
Write a high-quality answer for the given question using only the provided search
results (some of which might be irrelevant).
Document [1](Title: Asian Americans in science and technology) Prize in physics for
discovery of the subatomic particle J/ψ. Subrahmanyan Chandrasekhar shared...
Document [2](Title: List of Nobel laureates in Physics) The first Nobel Prize in
Physics was awarded in 1901 to Wilhelm Conrad Röntgen, of Germany, who received...
Document [3](Title: Scientist) and pursued through a unique method, was essentially
in place. Ramón y Cajal won the Nobel Prize in 1906 for his remarkable...
Question: who got the first nobel prize in physics
Answer:
The correct answer, Wilhelm Conrad Röntgen, is hidden in Document [2].
The questions come from NaturalQuestions-Open: real Google search queries paired with answers manually annotated in Wikipedia. The authors select 2,655 questions whose annotated answers are full paragraphs.
The distractors are chosen deliberately. The authors use Contriever to retrieve Wikipedia passages that are highly relevant to the question but do not contain the answer, then order them by relevance. These passages all look plausible; the model must actually read them to tell them apart.
Now the authors can vary the conditions:
- Position: Move the answer-containing document to the first, fifth, tenth… position. The content stays exactly the same; only the order changes.
- Length: Increase the distractors from nine to 19 and 29, giving 10, 20, and 30 documents in total—roughly 2K, 4K, and 6K tokens.
Scoring is lenient, counting an output as correct if it contains any accepted reference answer.
Key–Value Retrieval
The second task removes meaning and keeps only the most basic act of finding.
Give the model a JSON object containing key–value pairs, where every key and value is a randomly generated UUID. Then provide a key and ask for its value:
Extract the value corresponding to the specified key in the JSON object below.
JSON data:
{"2a8d601d-1d69-4e64-9f90-8ad825a74195": "bb3ba2a5-7de8-434b-a86e-a88bb9fa7289",
"a54e2eed-e625-4570-9f74-3624e77d6684": "d1ff29be-4e2a-4208-a182-0cea716be3d4",
"9f4a92b9-5f69-4725-ba1e-403f08dea695": "703a7ce5-f17f-4e6d-b895-5836ba5ec71c",
...}
Key: "9f4a92b9-5f69-4725-ba1e-403f08dea695"
Corresponding value:
The task requires finding an identical string in the context and copying the string after it. No semantic understanding is needed. The authors use 75, 140, and 300 pairs—roughly 4K, 8K, and 16K tokens—with 500 examples per setting.
If a model struggles even here, the problem is more basic than understanding a document: it cannot find the information.
Models Tested
The authors test six models, including two pairs of shorter and extended versions of the same model:
| Model | Maximum context | Notes |
|---|---|---|
| GPT-3.5-Turbo | 4K | OpenAI API |
| GPT-3.5-Turbo (16K) | 16K | Extended version of the same model |
| Claude-1.3 | 8K | Anthropic API |
| Claude-1.3 (100K) | 100K | Extended version of the same model |
| MPT-30B-Instruct | 8K | Open source; uses ALiBi positional encoding |
| LongChat-13B (16K) | 16K | Open source; extends LLaMA-13B from 2,048 to 16,384 |
The OpenAI models use the 0613 versions. All models use greedy decoding, choosing the highest-probability token at each step.
Discussion
The U-Shaped Curve

Start with the middle panel: 20 documents, about 4K tokens.
GPT-3.5-Turbo's line starts at the upper left. With the answer in the first document, accuracy is about 76%. Move it rightward and accuracy falls toward a trough near the tenth document, before recovering somewhat at the twentieth.
The appendix's raw numbers make this clearer:
| Answer document position | 1st | 5th | 10th | 15th | 20th |
|---|---|---|---|---|---|
| GPT-3.5-Turbo | 75.8% | 57.2% | 53.8% | 55.4% | 63.2% |
Compiled from Table 6, Appendix G.2, of the paper.
Compare these with the two reference lines from earlier, where GPT-3.5-Turbo scores 56.1% closed-book and 88.3% with the oracle document.
With the answer in the tenth document, 53.8% is below the score obtained with no documents at all.
Twenty documents are provided, one clearly contains the answer, and the model still performs worse than when given nothing.
That is the “lost in the middle” of the title. The authors call the advantage at the beginning primacy bias, and the advantage at the end recency bias.
Are Extended Models Better at Using Long Contexts?
GPT-3.5-Turbo comes in 4K and 16K versions. The 10- and 20-document settings fit into both.
Their curves almost overlap. With 20 documents and the answer first, they score 75.8% and 75.7%; with the answer tenth, 53.8% and 54.1%. Claude-1.3 and its 100K version show the same pattern.
The authors argue that a model's longer-context version is not necessarily better at using its context. It simply has room for more material, and it uses whatever fits in both windows much as the shorter version does.
Even Copying a String Can Go Wrong

The key–value results fall into two groups.
Claude-1.3 and Claude-1.3 (100K) stay near 100% in almost every setting, with flat curves along the top.
Other models struggle. In the 300-pair panel on the right, GPT-3.5-Turbo (16K) is nearly perfect at the beginning and end, but falls to 45.6% around the 200th pair. Even when all it needs to do is find an exact string, it loses its way in the middle.
LongChat-13B (16K) behaves differently with 140 pairs: the first half is the worst. On inspection, the authors find that when the target key appears near the beginning, the model tends to write code to “query” that key instead of copying its value directly.
Why Does This Happen?
The authors run several preliminary analyses.
-
Model architecture. The open-source models tested are decoder-only: each token sees only preceding content. The authors additionally test two encoder–decoder models, Flan-T5-XXL and Flan-UL2. Within Flan-UL2's training length of 2,048 tokens, the best and worst positions differ by just 1.9%; beyond that length, the U-shaped curve returns. The authors suggest that a bidirectional encoder lets each document “see” later documents, making it easier to judge which matters.
-
Putting the question first. A decoder-only model has not seen the question while reading the documents because the question comes last. What if it also appears before the documents? This makes key–value retrieval almost perfect, with GPT-3.5-Turbo (16K) reaching 100% on 300 pairs, versus a worst case of 45.6% before the change. Multi-document QA barely improves, however.
-
Instruction tuning. Instructions usually place the task description first. Could that teach a preference for the beginning? Comparing MPT-30B before and after tuning shows a U-shaped curve in both. Instruction tuning actually reduces the best-to-worst position gap from almost 10% to about 4%.
-
Model size. Appendix experiments on Llama-2 show only recency bias at 7B; the full U shape appears at 13B and 70B.
The authors also run several checks that rule out alternative explanations. Random Wikipedia distractors raise overall scores but preserve the U shape. Explicitly saying the search results are randomly ordered, and shuffling the distractors, produces the same trend. GPT-4 outperforms every other model on a 500-question subset, yet its curve is still U-shaped.
What About Adding More Documents?
Finally, the authors return to a practical RAG setup, using Contriever to retrieve Wikipedia documents and putting all of them into the prompt. This time, there may be no answer-containing document, one, or several.

The orange line shows retriever recall: the proportion of questions for which at least one of the top documents contains the answer. From five to 50 documents, it climbs toward 90.
The model accuracy curves below it, however, are almost flat after about 20 documents. Increasing from 20 to 50 adds only about 1.5% for GPT-3.5-Turbo and 1% for Claude-1.3, while more than doubling the input length and increasing latency and cost.
The retriever found it. The model did not use it.
Based on this, the authors suggest reranking relevant documents toward the beginning or, when appropriate, retrieving fewer documents.
What Happened Next?
Interest in long-context evaluation grew rapidly in the months after this paper appeared.
In November 2023, Greg Kamradt popularized a needle-in-a-haystack test that inserts an out-of-place sentence, such as “the best thing to do in San Francisco is eat a sandwich and sit in Dolores Park on a sunny day,” into a large collection of Paul Graham essays, then asks the model what is most fun to do in San Francisco.
Its spirit resembles this paper's key–value retrieval task. But Anthropic's December 2023 explanation notes that some of Claude 2.1's errors came from its training to avoid answering without sufficient document evidence. Faced with an incongruous sentence, it often said the documents contained no clear answer. Prefilling the answer with “Here is the most relevant sentence in the context:” raised the original evaluation's score from 27% to 98%.
Low scores can have causes beyond position. A model's response tendencies and the wording of a prompt can get mixed into the same number. The closed-book, oracle, random-distractor, and random-order settings in this paper are designed to separate position effects from those other factors.
Conclusion
By controlling information position and context length, this paper measures a weakness in how language models use long contexts:
- Performance drops sharply when relevant information is in the middle. With 20 documents, GPT-3.5-Turbo falls from 75.8% at the beginning to 53.8% in the middle, below its 56.1% with no documents.
- Longer-context versions use material that fits much like their shorter counterparts. The 4K and 16K GPT-3.5-Turbo curves almost overlap.
- Even simple string matching is affected. With 300 key–value pairs, GPT-3.5-Turbo (16K) falls to 45.6% in the middle.
- The benefit of adding documents saturates quickly. Increasing from 20 to 50 improves accuracy by only 1% to 1.5%.
These results come from models available in 2023. The authors turn them into an evaluation criterion: a model claimed to use long contexts reliably should show little sensitivity to where information appears, with a small gap between its best and worst positions.
If you build RAG systems, remember that where retrieved documents go and how many you include both affect the answer.
Whether today's models still get lost in the middle requires another measurement with today's models.