Module 3 · Long-context ingestion
"Lost in the middle" and information placement
4 min read
In 2023, a study with a now-famous title, Lost in the Middle, showed that language models retrieved information placed at the start or end of a long context well, and clearly less well in the middle. Recent large models have made much progress on this, but the placement of the information and of the question remains a simple, free lever. This lesson measures both on the local model.
Two placement questions
| Question | What we vary |
|---|---|
| Where is the information in the document? | The needle position: 0% (start) to 100% (end) |
| Where is the question relative to the document? | Before the document, or after |
You do not always choose where the information is (it is where it is in the contract), but you always choose where the question goes and the order of the documents.
def ask(document: str, needle: Needle, question_first: bool = False) -> list[dict]:
doc = f"<document>\n{document}\n</document>"
content = f"{needle.question}\n\n{doc}" if question_first else f"{doc}\n\n{needle.question}"
return [{"role": "user", "content": content}]
| Question placed… | Start | 25% | Middle | 75% | End |
|---|---|---|---|---|---|
| after the document (8,000 tokens) | 1.00 | 1.00 | 1.00 | 1.00 | 1.00 |
| before the document (8,000 tokens) | 1.00 | 1.00 | 1.00 | 1.00 | 1.00 |
On Qwen2.5-1.5B, up to 8,000 tokens, neither the position of the information nor that of the question changes anything: the test is passed at 100% everywhere (lesson 3.1). The "lost in the middle" effect does not appear on such a simple task, with a recent model and documents of this size.
Should the placement rules be forgotten, then? No, for three reasons: they cost nothing; the effect reappears on harder tasks (combining several passages) and much longer contexts; and placing documents first is required by prompt caching anyway (lesson 3.4). But this measurement is a reminder that a rule must be checked on your model and your documents: it may have no visible effect.
Why position matters
Two mechanisms explain the historically observed U-shaped curve:
- recency: the tokens closest to the generation point weigh more, and a question placed at the end is "fresh" when answering;
- primacy: the start of the context gets a lot of attention, partly because models learn to find important elements there (instructions, titles).
The middle benefits from neither. Models trained specifically on long contexts strongly reduce the effect, without always removing it, especially when the task requires combining several distant passages.
Placement rules
| Rule | Why |
|---|---|
| Long documents first, question last | The question is read last, right before answering; it is also the order that enables caching (lesson 3.4) |
| Essential instructions repeated after the documents | A long document between the instructions and the answer pushes them away; a short reminder brings them back |
| The most relevant documents at the end of the list | Closer to the question (useful when a RAG system ranks the documents) |
| Quote first, answer next | The model goes and finds the passages, wherever they are (lesson 3.3) |
| Split if necessary | One question per passage, then a synthesis (lesson 2.3) |
The typical structure of a prompt over a long document:
[Prompt système : rôle, règles, format]
<documents>
... document 1 ...
... document 2 (le plus pertinent en dernier) ...
</documents>
Rappel du format attendu.
Question : ...
Key takeaways
- "Lost in the middle": information in the middle of a long context has historically been retrieved less well.
- You do not choose where the information is, but you choose where the question goes: at the end, after the documents.
- Repeat essential instructions after the documents; place the most relevant ones near the question.
- Measure on your model and your documents: here, up to 8,000 tokens, position had no effect (100% everywhere).
Practice
- Make the test discriminating: replace the needle with one purchase-order amount among hundreds of others, and rerun
examples/04_long_context.py: does position matter then? - Add a variant where the question appears both before and after the document: is it better than the question alone at the end?
- Place two needles (start and middle) and ask for both codes: which one is forgotten more often?