AI engineering paper · 2024
Lost in the middle
Where a fact sits in a long prompt changes whether a language model uses it: strong at the start and end, weak in the middle.
You’ll learn to
- Explain the U-shaped accuracy curve and what the researchers changed to find it.
- Read a position-versus-accuracy table against the scores with no documents and with only the answer document.
- Describe the key-value test and what it shows about whether the problem is reading comprehension.
- Decide where to put the question and the best documents in your own prompts, and how to test it.
The idea in short
Models now accept prompts of tens of thousands of words, and systems that search your documents often paste many short extracts into the prompt. A 2023 study by Nelson Liu and colleagues asked a simple question about this setup. Does it matter where in the prompt the useful document sits? It mattered a great deal.
They moved the one extract that held the answer through a prompt of 20 documents and measured how often the model’s reply contained a correct answer. The model did best when the extract came first, less well when it came last and worst in the middle. In the worst spots, one model did worse than when it was given no documents at all. Models built for longer prompts showed the same curve.
The problem
A reads a , which is all the text you send it (also called its context), and writes a reply. It reads the prompt in small pieces called , which are words or parts of words. A typical English word is one or two tokens. The most text a model can read at once is its .
Context windows grew fast. Early models read a few thousand tokens. By 2023, models advertised 16,000 tokens and even 100,000. That raised a hope: instead of picking the best few documents from a collection of documents, just put everything in the prompt and let the model sort it out.
Systems that look things up already put a lot in the prompt. (RAG) means that before the model answers, a search step finds short extracts of your documents and pastes them into the prompt. Retrieval is that search step, and the code that does it is a retrieval system. This guide calls each pasted extract a document. The usual advice was to retrieve more documents, because a missing document means a wrong answer. But more documents means a longer prompt, and nobody had measured how well models use a long prompt.
“: How Language Models Use Long Contexts” is a 2023 study by Nelson Liu, Percy Liang and five co-authors, mostly at Stanford. It was published in 2024 in a journal of computational linguistics. The question it asks is plain: if the useful information is somewhere in a long prompt, does the model find it, and does the answer depend on where it is?
The question matters in everyday work. A RAG prompt usually lists documents in the order the search ranked them. A long chat history buries earlier messages under later ones. A pasted report puts its key table wherever the author put it. In each case, the position of what you need is a side effect, not a choice.
Two things make this a hard question to answer by looking at real use. A model that fails might have failed because the text was hard, not because it was long. And in real prompts, the position of the useful document is not under your control. The authors’ answer was to build tests where only one thing changes at a time.