Search Tutorials
--Ads--Long-Context Models vs RAG
When to put everything in the context window and when retrieval still wins.
Current Claude models accept up to one million tokens of input: thousands of pages. That raises a fair question: why build retrieval at all, instead of putting all the documents in the prompt? The answer depends on the size of your data, how often it changes, how many questions you serve, and who is allowed to see what. This lesson compares the two approaches with concrete numbers and shows how to combine them.
Head-to-head
| Long context (send everything) | RAG (send relevant chunks) | |
|---|---|---|
| Engineering effort | Minimal | A pipeline to build and maintain |
| Data size limit | The context window (about 750,000 words at 1M tokens) | Practically unlimited |
| Cost per question | Proportional to corpus size (much lower with caching) | Proportional to a few chunks |
| Latency | Higher; large inputs take time to process | Lower |
| Cross-document reasoning | Strong: the model sees everything at once | Limited to what was retrieved |
| Missing information | Nothing is missed by retrieval | Retrieval misses are possible |
| Freshness | Rebuild the prompt | Re-index changed documents |
| Per-user permissions | A different prompt per permission set | Filters at retrieval time |
Running the numbers
Take a corpus of 500,000 tokens (roughly 1,500 pages). At the time of writing, Claude Opus 5.5 input costs $4 per million tokens and cache reads cost $0.20 per million tokens. Ignoring output tokens:
| Approach | Input tokens per question | Approximate input cost per question | Per 10,000 questions |
|---|---|---|---|
| Long context, no caching | 500,000 | $2.00 | $20,000 |
| Long context, corpus cached | 500,000 (cached) | about $0.10 | about $1,000 plus cache writes |
| RAG, 8 chunks | about 5,000 | $0.02 | $200 |
Prices change; check the current price list before deciding. The pattern holds: caching makes long context viable for moderate traffic, but RAG stays an order of magnitude cheaper per question, and the gap grows with corpus size. Latency follows the same pattern.
When long context is the right choice
- The data is small (a few hundred pages), fixed for a while and the same for every user.
- Questions need the whole picture: "summarise the differences between these five contracts", "find inconsistencies in this specification".
- You are prototyping and want a quality baseline before investing in a pipeline.
- Traffic is low, so per-question cost does not matter much.
Combining them
The best systems often use both:
- Retrieve documents, not chunks. Use retrieval to pick the 3 to 10 most relevant documents, then give the model those documents in full. Retrieval handles scale; long context handles reasoning within documents.
- Generous context. Send 20 to 50 reranked chunks instead of 5. Current models handle that well, and recall improves.
- Route by size. If the relevant material fits a budget, send it all; otherwise fall back to chunk retrieval.
import anthropic
client = anthropic.Anthropic()
def count(text: str) -> int:
return client.messages.count_tokens(
model="claude-opus-5-5", messages=[{"role": "user", "content": text}]).input_tokens
def build_context(question: str, doc_retriever, chunk_retriever, budget: int = 60_000) -> str:
"""Send whole documents when they fit the budget, otherwise the best chunks."""
docs = doc_retriever.search(question, k=5) # document-level search
full = "\n\n".join(f'<document title="{d.metadata.get("title", d.id)}">\n{d.text}\n</document>'
for d in docs)
if count(full) <= budget:
return full
chunks = chunk_retriever.search(question, k=20)
return "\n\n".join(f'<chunk id="{c.id}">\n{c.text}\n</chunk>' for c in chunks)
If your corpus fits in the context window, run your evaluation set through both a long-context baseline and your RAG pipeline. If RAG is not clearly better on quality, its advantage has to come from cost, latency, freshness or permissions, and you should know which.
Key takeaways
- Long context is simplest and strongest for small, stable corpora and whole-picture questions.
- RAG wins on cost, latency, scale, freshness and per-user permissions.
- Prompt caching narrows the cost gap for long context but does not close it at scale.
- Combine them: retrieve documents or many chunks and let the model read them in full.