Long context vs RAG

In one sentence

Stuff the whole corpus in the window when it's small, static and shared; use RAG when it's large, changing, permissioned or high-volume. Often you do both: retrieve whole documents, then reason in long context.

Analogy: An open-book exam: if the book is a 20-page handout, bring all of it. If it's the national library, you need the catalog to fetch the right three books first.

Fit, freshness and permissions pick the path; evals and the bill confirm it. At volume, even cached stuffing costs ~3× RAG per query.

How it works

  • Long context fits corpora under ~200k tokens (~500 pages) and cross-document reasoning that chunks would fragment.
  • Stuffing means paying for and prefilling every token on every uncached call, so cost and latency grow with volume.
  • RAG can filter by permission and cite passages; a stuffed prompt exposes everything to every user.
  • A bigger window isn't free quality: models use facts in the middle of long inputs less reliably.
  • Caching narrows the gap: in the example, RAG was ~30× cheaper than naive stuffing, ~3× than cached.

Check yourself

Answer to mark it learned; your answers aren't scored.

A B2B app's knowledge base is ~120k tokens, but each customer may only see its own contracts plus shared docs. A PM says: "It fits in the window, so let's skip RAG and stuff it with prompt caching." What do you recommend?

Go deeper

Full explanation

Context windows of 200k–1M+ tokens mean you can often skip retrieval and put the whole corpus in the prompt (context stuffing). Whether you should is a cost, latency and quality trade-off.

Long context wins when:

  • The corpus fits comfortably: Anthropic's 2024 rule of thumb is under ~200k tokens (~500 pages).
  • The task needs cross-document reasoning (compare every contract, summarize a codebase) that top-k chunks would fragment.
  • You're prototyping and don't want to build ingestion, chunking and retrieval evals yet.

RAG wins when:

  • The corpus is large or changes daily: no window holds 10M tokens, and re-sending a static one on every call is waste.
  • Cost and latency matter at volume: you pay for and prefill every token on every uncached call.
  • Access control differs per user: retrieval can filter by permission; a stuffed prompt exposes everything.
  • You need citations to specific passages.

More context isn't free quality either: models use facts in the middle of long inputs less reliably (lost in the middle, Lost in the middle & context rot).

Hybrid is the common production answer: retrieve the top 5–20 documents (not chunks), then let a long-context model read them whole. Prompt caching narrows the cost gap for static corpora, and routing (answer with RAG, escalate to long context when the model says the retrieved text is insufficient) gets most of both.

Code Cost per query: stuffing vs caching vs RAG
pythonCost per query: stuffing vs caching vs RAG
# Input cost per query: stuff the corpus vs retrieve top-k.
# Output tokens are the same either way, so they cancel out.
PRICE_IN = 3.00 / 1_000_000  # $ per input token, example mid-tier model
CACHE_READ = 0.10            # cached input billed at ~10% (Anthropic-style)
QUERIES_PER_DAY = 100_000

corpus = 150_000             # tokens, ~375 pages
instructions = 1_000         # system prompt + question
rag_context = 8 * 500        # top-8 chunks of ~500 tokens

def cost(uncached: int, cached: int = 0) -> float:
    # Cache writes ignored: amortized at ~70 queries/minute
    return uncached * PRICE_IN + cached * PRICE_IN * CACHE_READ

options = {
    "stuff everything": cost(corpus + instructions),
    "stuff + prompt cache": cost(instructions, cached=corpus),
    "RAG top-8 chunks": cost(instructions + rag_context),
}
for name, usd in options.items():
    print(f"{name:21s} ${usd:.3f}/query  ${usd * QUERIES_PER_DAY:>7,.0f}/day")

# stuff everything      $0.453/query  $ 45,300/day
# stuff + prompt cache  $0.048/query  $  4,800/day
# RAG top-8 chunks      $0.015/query  $  1,500/day
Real example

Internal policy Q&A: a 150k-token corpus (~375 pages), 100k queries a day, a model at $3 per million input tokens (output cost is the same either way).

  • Stuff everything: ~151k input tokens → ~$0.45/query → ~$45k/day, plus seconds of prefill on every uncached call.
  • Stuff + prompt caching: corpus billed at ~10% → ~$0.048/query → ~$4.8k/day.
  • RAG, top-8 × 500-token chunks: ~5k tokens → ~$0.015/query → ~$1.5k/day, plus retrieval infrastructure.

RAG is ~30× cheaper than naive stuffing but only ~3× cheaper than cached stuffing, so here quality evals, freshness and access control decide, not cost alone.

Trade-offs +4 · −4

+ You gain

  • Long context: no chunking, embeddings or index to build and keep in sync.
  • Long context: the model sees whole documents, so cross-document reasoning and summaries don't fragment.
  • RAG: cost and latency scale with the k chunks retrieved, not with corpus size.
  • RAG: per-user access control, freshness and citations come naturally.

− You pay

  • Long context: you pay for and prefill the whole corpus on every uncached call.
  • Long context: quality drops for facts buried mid-window and near the window's limit.
  • RAG: if retrieval misses the passage, the model never sees it; recall becomes the ceiling.
  • RAG: a pipeline to build, tune and evaluate (parsing, chunking, embeddings, reranking).

Use it when

Long context for small (under ~200k tokens), static, shared corpora, whole-document tasks and prototypes; RAG for large, fast-changing or permissioned corpora at volume; hybrid when you need both scale and whole-document reasoning.

Avoid it when

Don't decide by window size or vendor marketing: run both on your eval set and compare accuracy, p95 latency and cost per query before committing.

Numbers to know 4
  • Anthropic's rule of thumb (2024): under ~200k tokens (~500 pages), consider skipping RAG and putting the whole knowledge base in the prompt, with caching.
  • Lost in the Middle (2023): GPT-3.5-Turbo's multi-document QA accuracy fell ~20 points when the answer moved from the start to the middle of 20 documents.
  • RULER (2024): only about half of models claiming 32k+ windows held satisfactory performance at 32k.
  • Worked example at $3/M input, 150k-token corpus: ~$0.45/query stuffed, ~$0.05 cached, ~$0.015 with 5k-token RAG.
Common pitfalls 4
  • Trusting needle-in-a-haystack scores: retrieving one planted fact is far easier than reasoning across many.
  • Stuffing a multi-tenant corpus: any user can coax out documents they aren't allowed to see.
  • Comparing naive stuffing to RAG on cost while ignoring prompt caching, or pricing RAG without its infrastructure and eval work.
  • Retrieving tiny chunks for a task that needs whole documents; retrieve documents, then read them in full.
Interview questions 3

Answer out loud first, then reveal.

Context windows are 1M tokens now. Is RAG dead?Show answer

No, but the threshold moved. For a small, static corpus shared by all users, I'd now often stuff it and use prompt caching: less to build and better whole-document reasoning. RAG still wins when the corpus is bigger than any window, changes constantly, needs per-user permissions or citations, or runs at volume, where paying for 150k tokens per query costs ~30× a 5k-token prompt. And effective context is shorter than advertised: accuracy drops on facts buried mid-window. So I decide with evals, not window size.

Walk me through the cost of each for a 150k-token corpus at 100k queries a day.Show answer

At $3 per million input tokens: stuffing is ~151k tokens, ~$0.45 per query, ~$45k a day. With prompt caching the corpus is billed at ~10%, so ~$0.048 per query, ~$4.8k a day, as long as traffic keeps the cache warm. RAG with eight 500-token chunks plus instructions is ~5k tokens, ~$0.015 per query, ~$1.5k a day plus retrieval infrastructure. Output cost is identical, so it cancels. A ~3× gap against cached stuffing is small enough that quality and access control usually decide.

How would you combine the two?Show answer

Two patterns. First, retrieve at document level rather than chunk level, the top 5–20 documents, and give them whole to a long-context model, so retrieval narrows the corpus but reasoning isn't fragmented. Second, route: answer with RAG first and let the model flag when the retrieved context is insufficient, then escalate to long context. The Self-Route paper found RAG and long context agree on most queries, so most traffic stays on the cheap path while quality stays close to full long context.

Deep dive 2

Advertised vs effective context

A 1M-token window means the model accepts 1M tokens, not that it reasons equally well over all of them. Needle-in-a-haystack tests, which retrieve one planted sentence, are nearly saturated and flatter models. Harder benchmarks tell a different story: NVIDIA's RULER (2024) found only about half of models claiming 32k+ windows held satisfactory performance at 32k, and Lost in the Middle (2023) showed a U-shaped accuracy curve by position. Multi-hop questions, aggregation ("count every clause that…") and distractors degrade fastest. Practical rule: measure on your task at your real context length, and treat the advertised window as a ceiling, not a working size.

Routing between them (Self-Route)

Li et al. (2024) compared RAG and long context across several models and benchmarks: with enough budget, long context scored higher on average, RAG was far cheaper, and the two gave the same answer on most queries. Self-Route exploits that: retrieve top-k chunks and ask the model whether they are enough to answer; if yes, answer with RAG; if it declines, send the full context to the long-context model. Most queries take the cheap path, so cost drops substantially with quality close to full long context. In production, the same idea works with a confidence score or an explicit "not enough information" answer that triggers the pricier path.