AI State of the Art
SOTA/Retrieval Augmented Generation (RAG)/26 min
Section 143

Long-context vs RAG: When to Use What?

Listen as a podcast →

The Turn: Models Now Read Thousands of Pages

A model's context window is how much text it can take as input at once. It is measured in tokens, the units text is chopped into (Tokenization), reckon roughly three quarters of a word per token.

The order of magnitude has changed dramatically in a few years. Early GPT-family models accepted a few thousand tokens, a handful of pages: RAG was then the only way to work over a corpus. Leading models today accept hundreds of thousands of tokens, and in some cases on the order of a million, several hundred pages in one go.

About Context-Window Figures

This page deliberately quotes no per-model numbers. They change with every model release, go stale within weeks, and are in any case announced by the vendors themselves. What stays true, and is all the argument needs: windows are now counted in hundreds of thousands to a few million tokens, not thousands. Check today's value in the documentation of the model you are using.

Also beware a common confusion: the advertised size is what the model accepts, not the length over which it stays equally reliable. The rest of this page is precisely about that gap.

Hence the serious question: why keep building a RAG chain, with its chunking, its embeddings, its vector database, its reranking and all the plumbing that comes with it, if you can simply put the documents in the prompt?

The Real Question

Long context versus RAG isn't a debate of principle, it's a concrete tradeoff between cost, response time, quality and maintenance burden. The right choice depends on your own numbers.

Advantages of Long Context

Radical simplicity: no chain to build. No chunking to calibrate, no embeddings to choose, no vector database to maintain, no reranking to tune. You put the documents in the prompt, the model does the rest.

Complete and simultaneous access: the model receives the whole corpus at once. So it can cross-reference information sitting in different sections, spot contradictions, produce an overview. This is the right tool for questions about the whole rather than about a point, "summarize the trends in this 200-page report", "are there contradictions between chapters 3 and 7?" A RAG system is structurally bad at these: it returns five passages, and a contradiction between two chapters is not a passage.

No retrieval loss: a RAG system can miss the useful passage. If question and passage don't resemble each other enough from the embedding model's point of view, it doesn't come back, and recall, the share of useful passages actually retrieved, never reaches 100% (RAG evaluation). With long context the retrieval question doesn't arise: everything is there.

No tuning: no embedding model to pick, no chunk size or overlap to calibrate, no result count to set. All those decisions disappear, and with them, every flaw they can introduce.

Advantages of RAG

Volume: the context window has a ceiling, today a few million tokens. An index has no limit of that kind: you can index billions of tokens and read only five passages per question. A corporate document base, a corpus of millions of articles: long context is simply not in the running.

Cost: often the decisive argument, and it depends on no particular price list. Providers bill per input token processed. The cost ratio is therefore the volume ratio, and that can be computed without consulting any pricing page:

  • a RAG system sending 5 passages, say 2,000 tokens: baseline cost;
  • the same corpus sent whole, 1,000,000 tokens: 1,000,000 ÷ 2,000 = 500 times more expensive, per query.

So it doesn't matter whether a million tokens costs a euro or a cent: long context here costs five hundred times what RAG costs. At ten queries a day, the gap is a rounding error. At ten thousand queries a day, it's the difference between a viable service and an unviable one. Which is why you should run the calculation with your query volume and today's rate, rather than memorising a number.

Two honest caveats. Prompt caching, offered by most providers, sharply reduces the cost of tokens repeated from one request to the next, which is exactly the case of a fixed corpus, and shifts the tradeoff toward long context. Conversely, RAG carries a cost this calculation ignores: the initial indexing and the upkeep of the database.

Response time: before writing the first word of its answer, the model has to read all of its input. That reading phase is called prefill (see KV cache), and its duration grows with input length, at least proportionally.

The practical consequence is best stated in orders of magnitude, exact values depending on model and hardware:

  • with RAG, the input is a few thousand tokens: reading is imperceptible, and the time is dominated by the search itself, on the order of a fraction of a second;
  • with a few hundred thousand tokens of input, reading takes on the order of several seconds;
  • at a million tokens, you are in the tens of seconds, before the first word of the answer.

For interactive use, that wait is the blocker more often than the price is.

Concentrated attention: a RAG system hands the model only the selected passages, and nothing else. With 500 pages of input, the model must find the needle in the haystack itself, and that is where its best-documented weakness lies, described just below.

Accuracy curves as a function of the number of documents placed in the input context (5, 10, 20, 30) for six models. Every curve slopes downward as the document count grows.
The more documents you put in the context, the less often the model answers correctly, across every model tested. The right information is still in there: it is dilution alone that costs accuracy, which is the core argument for a retriever that returns only the useful passages.Source : Lost in the Middle paper by Liu et al., via Pinecone
Lost in the Middle

This is the central result of the paper by Liu and colleagues, and it is worth stating exactly. The authors take a task where the answer sits in one specific document among several placed in the context, and move that document from one position to another while changing nothing else. They find that answer accuracy is highest when the useful information is at the beginning or the end of the input, and degrades markedly when the model must reach information in the middle, including for models explicitly built for long contexts.

Two lessons follow. First, the advertised window is no guarantee of uniform use: accepting a million tokens does not mean exploiting them equally well everywhere. Second, RAG sidesteps the problem for free, since it supplies only a few passages: there is then almost no middle.

One caveat: the paper establishes the phenomenon, it does not explain it. The causes usually proposed (positional habits acquired during training, effects of positional encoding) are discussed hypotheses, not demonstrated results.

Lost in the Middle: How Language Models Use Long Contexts (Liu et al., 2023)
Three plots of accuracy against the position of the document holding the answer, for 10, 20 and 30 documents in the context. Each curve forms a U shape: high at the start, low in the middle, rising again at the end.
The U-shaped curve that gave the phenomenon its name. The document holding the answer is moved from one position to another while everything else stays identical: accuracy peaks when it sits first, collapses in the middle, and partly recovers at the end. The more documents in the context, the deeper the central dip.Source : Lost in the Middle paper by Liu et al., via The Decoder

Updating: adding a document to an index takes as long as computing its embeddings, and no longer. With long context there is nothing to update, since nothing is stored, but the whole corpus must be retransmitted on every query. One pays once on writing, the other pays on every read.

What the Choice Should Rest On

Long context suits you if:

  • the corpus is small. Let's derive an order of magnitude: 50,000 tokens is about 37,000 words, so a few dozen pages depending on layout density. That is a case file, not a document base
  • the questions are about the whole: synthesis, comparison across sections, hunting for contradictions
  • the number of queries per day is low, which makes the cost factor inconsequential
  • you want a working mock-up today

RAG suits you if:

  • the corpus far exceeds the window, or has no fixed size
  • the questions are targeted and expect one specific passage
  • query volume is high: this is where the cost factor computed above becomes decisive
  • response time must stay under a second
  • you need to cite the source of every claim. A RAG system knows by construction which document its answer came from; a corpus dumped wholesale into the prompt forces you to trust the model's own attributions

A mixed approach suits you if:

  • your questions come in two kinds, some pinpoint and some global
  • you want RAG's traceability together with whole-document reading
The Mixed Approach: Search Narrow, Read Wide

Often the best: retrieval is used to find the right documents, but what gets sent to the model is the whole documents rather than chunks of a few hundred tokens. You keep the selection retrieval performed, and remove chunking's defect, the useful passage is no longer amputated from its context.

This is exactly the parent-child principle described in Chunking strategies, pushed all the way to the whole document: search narrow, read wide.

Two stacked plots against the number of retrieved passages (top-k): the upper one shows the performance of three approaches (long context, RAG, Self-Route), the lower one the percentage of tokens each consumes.
What the mixed approach actually buys you. On top, answer quality: plain RAG (red) trails long context (blue dashed) as long as it retrieves only a handful of passages, whereas the Self-Route hybrid (yellow) catches up by around ten. Below, the price: at that same point the hybrid burns substantially fewer tokens than long context.Source : Retrieval Augmented Generation or Long-Context LLMs paper by Li et al. (Google DeepMind), via alphaXiv
What the Google DeepMind Study Measures

The paper this chart comes from compares the two approaches across several public datasets, and its conclusion cuts both ways. Given sufficient resources, long context outperforms RAG on average; RAG's advantage is its markedly lower cost. From that the authors derive a method, Self-Route, which lets the model decide for itself, question by question, whether it can answer from the retrieved passages alone or needs the whole corpus. They report a large drop in compute cost at performance comparable to long context.

That is the honest formulation of the tradeoff: long context is not a commodity that replaces RAG, nor the reverse. Each buys you something, and one of the two can be decided case by case.

Retrieval Augmented Generation or Long-Context LLMs? A Comprehensive Study and Hybrid Approach (Li et al., 2024)

What Will Probably Bring Them Closer

Three things are in motion, and they are better stated as trends than as quantified predictions.

The cost per token processed falls year on year at a given model quality. No figure deserves to be put forward here, the trajectory matters, not the value. But note that this decline does not remove the tradeoff: it only moves the volume threshold at which the factor of 500 computed above becomes unbearable.

Long-context use is improving: it is an explicit focus of recent work, and lost-in-the-middle is a known defect people are trying to fix (Context window extension).

RAG is getting more elaborate, in the good sense: traversal of knowledge graphs (Graph RAG), chained retrievals decided by an agent (Agentic RAG).

The likely destination looks like Self-Route: not a winner, but systems that decide case by case whether to fetch a passage or read a whole file.

No Universal Solution

The five numbers to establish before choosing: corpus size, queries per day, acceptable response time, the nature of the questions (pinpoint or global), and how often the documents change. Four of the five are known from the start; the fifth comes from looking at the questions users actually ask.

And a remark that applies to both camps: long context does not excuse you from evaluating. It removes the retrieval half of the problem, not the generation half, the model can still misuse what is in front of it, and that is in fact the most likely failure when you give it a great deal (RAG evaluation).

The real lesson: long context and RAG aren't enemies. They're two tools with different strengths, and the choice is a calculation, not an opinion.

Check your understanding0 / 5

1. A model advertises a context window of one million tokens. What exactly does that figure guarantee?

2. In the lost-in-the-middle experiment by Liu and colleagues, what is demonstrated, and how?

3. Why can you say long context costs 500 times more than RAG in this example, without consulting any price list?

4. Why does a question like are there contradictions between chapters 3 and 7 put a RAG system in structural difficulty?

5. What does the mixed approach recommended by the page (search fine, read wide) consist of?