How it works · 12 of 37 Advanced
Long context or RAG: which should an AI app use?
Pour it all in, or look it up?
Long context vs RAG: Huge context windows let you paste everything in. It's a trade-off, not a replacement for retrieval.
Loading the animation…
Say it like this A long context lets you paste everything in, but every question then pays to read all of it and facts in the middle get missed; for large or changing data, retrieve the few pages that matter.
Under the hood — for builders
- Lost in the middle
- A worked example from Liu et al.: a model got a question and 20 Wikipedia passages, exactly one holding the answer, and moved that passage from first to last. Closed-book = the same question with no passages at all:
- What goes in the prompt
- Long context: every page, for every question (the context window: the most text a model can read at once). RAG: a search picks a few short pieces first. Li et al.'s RAG setting (drawn as boxes, not to scale):
- Head to head
- Li et al. ran both on long-document benchmarks. Given enough window for the full text, long context scored higher on average. Their hybrid, Self-Route, came close for far fewer tokens:
- Best of both
- Self-Route tries RAG first and lets the model refuse: “Write unanswerable if the query can not be answered based on the provided text.” Only refused questions are re-asked with the full text (Li et al., §4.2). The same check as Agentic RAG's “enough?”.
- Typical cost
- Billing is per token, so a long-context question costs in proportion to everything sent. Li et al.'s Self-Route needed 38% (Gemini-1.5-Pro) and 61% (GPT-4o) of long context's tokens. Caching a repeated prefix also cuts it (D4).
- Measure it
- Liu et al.'s test on your own data: put the answer page at the start, middle and end of the prompt, and compare.
- Watch out
- Changing data means re-sending everything, every time. Past the window's limit, the rest simply does not fit (Tokens & context window, A4).
- Sources
- Liu, Lin, Hewitt, Paranjape, Bevilacqua, Petroni & Liang, Lost in the Middle: How Language Models Use Long Contexts (TACL, arXiv 2307.03172v3) · Li, Li, Zhang, Mei & Bendersky, Retrieval Augmented Generation or Long-Context LLMs? A Comprehensive Study and Hybrid Approach (EMNLP 2024 industry track, arXiv 2407.16833v2).
What happens in the long context vs RAG animation, step by step?
- Long context: skip the search and pour every page into the model's prompt. It reads everything, so with 10 pages it gets the refund rule right every time.
- Your turn: drag the slider to pour more pages in. Watch the receipt, and the five tries.
- At 2,000 pages, every question pays to read all of them. And the refund page, buried in the middle, is missed more often: 3 right in 5.
- RAG: search first, and lift out only the pages that matter. Same answer every time, on a 3-page bill.
- Small, stable documents: paste them in. Large or changing data: retrieve.
What everyday example explains long context vs RAG?
Reading the whole encyclopedia for every question vs using the index.
Why should a team care about long context vs RAG?
The "do we even need RAG?" question every team asks.