Search⌘ K
AI Features

RAG vs. Long-Context Trade-Offs in Cost, Freshness, and Accuracy

Explore how retrieval-augmented generation and long-context prompting differ in fit, cost, freshness, caching, and accuracy. Learn to calculate daily usage, account for document edits, and build a decision function that selects retrieval, long context, or testing for each corpus.

We'll cover the following...

So far, the on-call assistant has searched a small store of eight notes. Real teams have far more. Over a few years, an engineering organization collects thousands of runbooks and postmortems, plus the threads from every incident channel. Engineers also keep editing them. A runbook changes after each incident, and a new postmortem lands every week.

There are two basic ways to give the assistant this knowledge. With retrieval, often called RAG (retrieval-augmented generation), we search the documents for each question and send only the few that match. With long context, we put the whole document set into every call and let the model find what it needs. Modern models accept a million tokens or more, so the second option can look like the simpler one.

In this lesson, we’ll compare the two with real numbers. We’ll check whether a document set fits at all, work out what each approach costs per call and per day, see how daily edits change the picture, and then look at what we know about accuracy. At the end, we’ll put the whole decision into one function.

Does the corpus fit?

The full set of documents an assistant can draw from is called its corpus. Let’s describe three corpora our assistant might use. For each one, we note its size in tokens, how many documents are edited per day, and how many questions it serves per day:

  • Incident knowledge base: Every runbook, postmortem, and incident thread the company has. It’s 60 million tokens, with about 200 edits and 500 questions a day.

  • Checkout runbook set: Only the checkout team’s runbooks and recent postmortems. It’s 150,000 tokens, with about 5 edits and 500 questions a day.

  • On-call handbook: The policy document for how on-call works, such as rotations, paging rules, and escalation. It’s 80,000 tokens, serves about 20 questions a day, and is updated only once a quarter.

The first check is the one from Chapter 1: compare the size with the model’s context window. We divide the corpus size by the window to get a fit ratio. A ratio above 1 means the corpus can’t fit in a single call.

corpora.py · Python 3.14.0
import litellm
from engine_config import MODEL
CORPORA = { # tokens, edits per day, calls per day
"incident knowledge base": {"tokens": 60_000_000, "edits_per_day": 200, "calls_per_day": 500},
"checkout runbook set": {"tokens": 150_000, "edits_per_day": 5, "calls_per_day": 500},
"on-call handbook": {"tokens": 80_000, "edits_per_day": 0, "calls_per_day": 20},
}
def context_window(model=MODEL):
info = litellm.get_model_info(model)
return info.get("max_input_tokens") or info["max_tokens"]
def fit_ratio(corpus, model=MODEL):
return corpus["tokens"] / context_window(model)
if __name__ == "__main__":
print(f"{MODEL} window: {context_window():,} tokens")
for name, corpus in CORPORA.items():
ratio = fit_ratio(corpus)
print(f"{name:<24}| {corpus['tokens']:>11,} tokens | ratio {ratio:6.2f} | fits: {ratio <= 1}")

In the code above:

  • Lines 4–8: We describe the three corpora in CORPORA, each ...