AI engineering paper · 2024

Lost in the middle

Liu, Lin, Hewitt, Paranjape, Bevilacqua, Petroni and Liang · TACL 2024

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.

The same model, the same 20 documents, only the answer’s position changes: 75.8% right at the start, 53.8% in the middle, 63.2% at the end. The middle score is below the 56.1% it scored closed-book, with no documents at all.

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.

The design choices

The core idea is a controlled experiment. Keep the question, the documents and the model fixed. Move only the useful document. If the score changes, position is the cause.

Our biggest sale ever · Black Friday

Flat 50% Off

Pro

Continue reading

Unlock every course, now and later

  • System Design, live
  • AI Engineering, live
  • Data Engineering, live
  • Machine Learning Engineering, live
  • Inference Engineering, coming soon
  • AI Research, coming soon
  • Performance Engineering, coming soon
  • Agent & Harness Engineering, coming soon
  • LLM Fine-tuning, coming soon
  • LLM Pre-training & Post-training, coming soon
  • Reinforcement Learning, coming soon
  • AI Infrastructure & Platform Engineering, coming soon
  • AI Evaluation & Safety, coming soon
  • Forward Deployed Engineering, coming soon
  • Cloud Computing, coming soon
  • DevOps, coming soon

50% off · Annual and Lifetime

BLACKFRIDAY50Applied at checkout

Choose a plan

About the paper

Lost in the Middle: How Language Models Use Long Contexts by Liu, Lin, Hewitt, Paranjape, Bevilacqua, Petroni and Liang, TACL 2024.

Read the original paper

More AI engineering papers

All papers