Skip to content

RAG vs Long Context: Which to Use and When

Quick answer

Choose long context when the corpus fits and rarely changes; choose RAG when it is large or changing. See a decision table, a position test and a Python sketch.

RAG vs Long Context: Which to Use and When

For RAG vs long context, use long context when your whole corpus fits comfortably in the model's window, changes rarely and is queried often with caching. Use RAG when the corpus is large, changes daily or needs source-level control. Many teams combine both: retrieve first, then put a small set of relevant passages in the prompt.

Key takeaways

  • As of late November 2026, 1M-token windows are documented by Anthropic and Google, but Anthropic states that more context is not automatically better because accuracy and recall degrade as token count grows.
  • Research on long inputs found that performance drops when the relevant information sits in the middle.
  • Three inputs decide the choice: corpus size against the window, how often the data changes, and how many queries repeat the same context.
  • Test your own documents. A short position test tells you more than any general claim.

On this page:

SituationBetter fitWhy
Corpus fits well inside the window, rarely changesLong contextSimple to build; no index to maintain
Same large context reused across many queriesLong context with cachingGoogle calls caching the primary optimization for long-context work
Corpus far larger than the windowRAGIt cannot fit at all
Data changes dailyRAGUpdate the index, not every prompt
You must show which passage supported an answerRAGRetrieved chunks give a citation trail
Hard questions that need a few specific passagesRetrieve, then long contextSmall, relevant prompt beats a huge one

What do RAG and long context each mean?

Long context means putting the material directly in the prompt. RAG, retrieval-augmented generation, means searching an external store first and giving the model only the best matches.

The original RAG paper by Lewis and colleagues, submitted to arXiv on 22 May 2020 and revised on 12 April 2021, describes RAG models as combining pre-trained parametric and non-parametric memory for language generation. So RAG was defined as a way to give a model access to knowledge outside its weights, not only as a cost trick. If you need a refresher, read what RAG is, and for the wider choice between prompting, retrieval and tuning, prompt engineering vs RAG vs fine-tuning.

How do I decide?

Ask three questions, in this order.

  1. Does the corpus fit? Compare its token count with the window of your chosen model. Anthropic's context window documentation lists Claude Haiku 5.5, Sonnet 5.5 and Opus 5.5 among models with a 1M-token window and says other Claude models have a 200k-token window. Google's long-context guide says many Gemini models come with windows of 1 million or more tokens. We did not verify any page-count conversion, so count tokens with your tool.
  2. How often does it change? If daily, retrieval lets you update the index instead of rebuilding prompts.
  3. How often is the same context reused? If many queries share the same large block, caching can help.

If the answer to question 1 is "barely", stop and use retrieval. A prompt that nearly fills the window leaves little room for the answer. For why, see context windows explained.

Does it matter where the answer sits in a long prompt?

Yes. The research says models use the start and end of long inputs best.

The paper Lost in the Middle by Liu and colleagues, accepted to TACL, found that performance is often highest when relevant information is at the start or end of the input, and significantly degrades when models must access relevant information in the middle of long contexts, including for explicitly long-context models. It is a 2023 result on the models tested then, so rerun the idea on your own model. Anthropic says the same in general terms: more context is not automatically better, and as token count grows, accuracy and recall degrade, a phenomenon it calls context rot.

A quick position test you can run:

  1. Take 20 questions from a 300-page policy PDF.
  2. For each, place the paragraph holding the answer at the start, the middle and the end of the prompt.
  3. Count correct answers per position.

Illustrative results: 19, 13 and 18 correct for start, middle and end. A dip in the middle would match what the paper describes. If your own numbers are flat, long context may be fine for your case.

How does cost differ, and what does caching change?

Long context sends many tokens on every query. RAG sends fewer but adds a retrieval step. Caching can change the long-context side.

Google's guide calls context caching the primary optimization for long-context work, and says cached files are billed by storage time per hour. On Anthropic's page, models with a 1M window bill long requests at standard pricing, except that on Claude Haiku 5.5 prompts over 100,000 tokens cost more. We are not quoting prices, so use each vendor's pricing page.

Here is a sketch with assumed numbers. Option A sends 50,000 tokens of policy text on every query. Option B retrieves and sends 3,000 tokens. Over 10,000 queries, A sends 500 million tokens and B sends 30 million. Multiply each by your current price per million tokens to compare. If caching makes A's repeated block cheaper, the gap narrows, so include it in the calculation. B has its own costs for the index and the embedding step.

What would this look like in two Indian examples?

A Hindi-English HR policy bot for a 400-person company has a small corpus, perhaps under 1 lakh words (our assumption). Long context may be enough, especially with caching. A legal archive of 10,000 documents cannot fit in any single prompt, so it needs retrieval. These are the writer's illustrative cases, not measured data. Check retrieval quality in Hindi and Hinglish yourself.

What does a minimal retrieval sketch look like?

This toy example splits text into overlapping chunks and ranks them with a placeholder embedding based on word counts. It uses words, not real tokens, and a real system would use a proper embedding model.

import math
from collections import Counter

def chunk(text, size=50, overlap=10):
    words = text.split()
    step = size - overlap
    return [" ".join(words[i:i + size]) for i in range(0, max(len(words) - overlap, 1), step)]

def embed(text):
    # Placeholder: word-count vector. Replace with a real embedding model.
    return Counter(text.lower().split())

def cosine(a, b):
    dot = sum(a[w] * b[w] for w in a if w in b)
    na = math.sqrt(sum(v * v for v in a.values()))
    nb = math.sqrt(sum(v * v for v in b.values()))
    return dot / (na * nb) if na and nb else 0.0

def top_k(question, chunks, k=3):
    q = embed(question)
    return sorted(chunks, key=lambda c: cosine(q, embed(c)), reverse=True)[:k]

policy = "Employees may carry forward ten days of leave. " * 20 + "Refunds are processed within seven working days. " * 20
chunks = chunk(policy, size=30, overlap=5)
best = top_k("How long do refunds take?", chunks, k=1)
print(best[0][:80])

The printed chunk should be about refunds. Our guide to vector databases for RAG covers where real embeddings are stored.

How do I check that retrieval works?

Test retrieval separately from the answer. First check whether the right chunk appears in the top results. Then check whether the model used it.

Anthropic reported in its contextual retrieval post, dated 19 September 2024, that contextual embeddings reduced the top-20-chunk retrieval failure rate by 35% (from 5.7% to 3.7%) and, combined with contextual BM25, by 49% (to 2.9%). That is the vendor's own test, not an independent benchmark, and results depend on your data. The practical lesson is to measure retrieval failures, not to assume them away. For a method, see LLM evaluation explained.

When should I combine both?

Most real systems do. Retrieve candidates, then give the model a focused set of passages, perhaps with a few full documents. You get the audit trail of retrieval and the model's ability to read passages together. The answer shifts as windows and prices change, so as of late November 2026 treat today's limits as a snapshot and check the vendor pages again before you commit.

Next step

Run the position test on one real document of yours, then run the retrieval sketch on the same file. Compare the two on accuracy and tokens sent. For related choices, read RAG vs fine-tuning vs AI agents next.

Frequently asked questions

Can I just paste my whole PDF or codebase into the prompt now?

If it fits comfortably in the model's window, yes. As of late November 2026, Anthropic and Google document 1M-token windows. But accuracy can fall as tokens grow, so test your documents.

Is RAG dead because of 1 million token windows?

No. Large windows cannot hold very large or fast-changing corpora, and retrieval gives a source trail. Anthropic notes more context is not automatically better. Many teams combine both.

What is the cost difference, and how does caching change it?

Long context sends more tokens per query. Google calls caching the primary optimization for long-context work and bills cached files by storage time. Check each vendor's pricing for your numbers.

When do I still need a vector database?

When the corpus is too big for the window, changes often, or you need to cite sources. For small, stable corpora a long prompt may do without one.

How do I check whether retrieval is working?

Measure whether the right chunk appears in the top results, separately from answer quality. Anthropic reported lower retrieval failure rates with contextual methods in its own tests; measure on your data.

Share LinkedIn X WhatsApp
×

Our Courses

Practice-Based Learning Tracks, Supercharged By A.I.