Lead · 8 chapters
Long-context vs RAG
When to just fill the window, the cost/latency/quality trade, prompt caching over large documents
Choosing by fashion creates either unnecessary retrieval complexity or runaway context spend, so this belongs in the roadmap as an explicit routing and cost decision.
Read these first
What you will know
Compare stuffing the window against retrieving, with measured cost and quality
Use the same documents, questions, model, and evaluation set while accounting for input tokens, cache hits, latency, and answer correctness.
Measure how answer quality and latency change as irrelevant context grows
Long prompts can dilute relevant evidence, trigger lost-in-the-middle behavior, increase processing time, and leave less room for the answer.
Use prompt caching to make a large-document approach affordable
Savings depend on stable prefixes, provider-specific cache semantics, and disciplined invalidation when documents or instructions change.
Choose per feature and defend it with numbers
Set explicit break-even thresholds for p95 latency, token spend, freshness, and task success because the cheapest path varies by workload.
Break the design with corpus growth, document churn, and adversarial queries, then patch the fallback policy
A strategy that works on a stable small corpus can fail through stale cached context, oversized inputs, empty retrieval, or injected instructions.
Chapters
- 1
Input tokens per session, not per request, decide the cost
Work the per-session arithmetic for a 250k-token document asked twelve questions: twelve full reads against twelve slim retrieved prompts. That fifty-fold gap is real for uncached reads, and it is the number most teams stop at. This chapter sets the comparison up honestly — assumptions stated out loud, output tokens separated from input, retrieval's own indexing and embedding cost included — so later chapters can show what moves it.
- 2
A cached prefix only pays off if nothing mutable sits in front of it
Prompt caching collapses that raw token gap, but only under one condition: the document must sit ahead of everything that varies. You will see why a 200k-token prefix can measure a 4% hit rate when a timestamp or personalisation block precedes it, how cache-read pricing turns eleven of twelve reads into a fraction of full price, and why prefix order is a design decision rather than an implementation detail.
- 3
A cache entry ages: what invalidation and TTL do to the cost model
A cached prefix is not free forever. Here you build the cost model for a 400k-token corpus serving 2,000 queries a day and edited twice a week, including what a write does to the entry and what uneven traffic does at a TTL measured in minutes. Two traps follow: appending edits to preserve the cache, which quietly makes answers reflect the corpus as of the last write, and what changes at 1.2M tokens.
- 4
More context can lower accuracy, not just raise the bill
Filling the window is not a neutral act. Accuracy can fall six points when the same corpus moves from eight ranked chunks to 300k stuffed tokens, with the losses clustering on evidence positioned mid-context. Dilution, position effects and prefill latency are covered here — plus why a near-perfect needle-in-a-haystack score tells you a verbatim string is findable and almost nothing about reasoning over several dispersed, similar passages.
- 5
Retrieve into the window, then cache what you retrieved
Between eight ranked chunks and 300k stuffed tokens sits the design the binary framing hides: select a subset of a corpus too large to fit, put that subset in a large window, and cache it. The catch is prefix stability, because a subset that varies per query cannot belong to a shared cacheable prefix — it either sits after the cacheable part, or the cache is keyed per document set, which only pays when sessions revisit the same set. What it buys is prefill time, which scales with tokens in the window, so this is the one lever that moves a p95 regression when prefix layout and cache warmth cannot. It is also the third branch any routing rule needs before an engineer can apply it without asking you.
- 6
Proving a long-context design is at least as good, before you switch
Approval for a switch needs a comparison that means something. Hold model, question set, documents and grader constant; measure input tokens, the measured cache hit rate, p95 latency and answer correctness together. When the first result is better accuracy and 4x worse p95, you will know which knobs — prefix layout, cache warmth, hybrid routing — to try before conceding, and which result would genuinely settle it.
- 7
Truncating to fit answers from evidence nobody can name
What should happen when a user attaches 1.4M tokens to a feature with a 900k-token ceiling? Silent truncation produces a confident answer derived from an unknown subset of the evidence, which is worse than an error nobody can miss. You will work through corpus growth, document churn and empty retrieval, then write the fallback policy that degrades visibly instead. Injected instructions are the case that does not fit the ceiling frame at all: a retriever admits ranked chunks while a stuffed window admits everything the user attached, so document content has to be marked and handled as untrusted data rather than as prompt.
- 8
Route per feature, and re-run the numbers when the model changes
A blanket 'always fill the window' rule and a blanket 'retrieval only' rule fail in the same way. Here you write the per-feature rule instead: a 12M-token knowledge base asked one question per session routes differently from a 90k-token diff reviewed across thirty turns. Set explicit break-even thresholds on spend, p95 and freshness, and attach a re-measurement trigger so the next model release does not reopen the argument.
External resources
- U-NIAH: Unified RAG and LLM Evaluation for Long Context Needle-In-Aarxiv.org
Controlled comparisons of long-context stuffing and RAG, with quantitative evaluations and failure analyses.
- Long-context LLM serving: the real tradeoffs in memory, latency, cost, and accuracy | DigitalOceandigitalocean.com
This is a substantive engineering explainer with concrete KV-cache calculations and detailed memory, batching, and latency tradeoffs relevant to deciding when long context is practical, though the provided excerpt does not yet show the RAG or prompt-caching comparison.
- RAG vs Large Context Window: Real Trade-offs for AI Appsredis.io
Concrete cost and latency numbers for filling the window vs retrieving.
- Beyond the Window: The Complete Guide to Long Context Language Models (And Why They’re Changing AI)towardsdev.com
Explains when long-context beats RAG, covering cost, latency, quality, and prompt caching.
- The Prompt: What is long context — and why does it matter for your AI?cloud.google.com
Shows when to use long context over RAG, covering cost, quality, latency, and caching.
- Long-Context LLM Infrastructure: Building Systems for Million-Token Windowsintrol.com
Explains when million-token context beats RAG, covering cost, latency, quality, and prompt caching.
- Is RAG Still Needed? Choosing the Best Approach for LLMsIBM Technology
Decision framework for when a big window genuinely replaces retrieval.
- Making Long Context LLMs Usable with Context CachingPrompt Engineering
Shows caching applied to large static documents and what it saves.
- Deep Dive into Long ContextGoogle for Developers
Explains long context versus RAG tradeoffs, including cost, latency, quality, and prompt caching.