RAG Fetches. CAG Remembers. The Choice Is Quietly Rewriting Your Context Budget.
Part 1 of 2: Two ways to feed a model your codebase — and why the "obvious" one is wrong more often than you'd think. A companion to the Context Engineering series.
Series — RAG vs CAG: (1) RAG fetches, CAG remembers — how context gets supplied. (2) Token economics and the VRAM wall.
I spent a chunk of last month watching an agent burn through a 200K-token window on a repo that was only 60K tokens on disk. The math didn't add up until I looked at how the context was being fed in. That's when it clicked: the interesting question in 2026 isn't "how big is your context window." It's "how does anything get into it in the first place."
There are two answers competing for that job. One fetches. One remembers. And the folk wisdom — "just cache everything, windows are huge now" — turns out to be a fast way to set money on fire.
Let me walk you through both, the way I'd sketch it on a whiteboard for a colleague who's about to make an architecture call.
The Two Philosophies, In One Picture

Here's the whole debate in one sentence. RAG treats the model as a stateless engine. CAG treats it as a stateful processor.
RAG — Retrieval-Augmented Generation — assumes the model remembers nothing between turns, so it assembles a small, bespoke context payload for each query. Find the relevant bits, inject them, throw them away.
CAG — Cache-Augmented Generation — assumes you'd rather pay once to precompute everything and then reuse it. Load the entire repo into the window, freeze the model's internal state, and reload that frozen state on every subsequent query.
Same goal — get the right context in front of the model. Opposite strategies. And the strategy you pick has real downstream consequences for latency, cost, and whether your agent quietly starts hallucinating around hour three.
How Does RAG Actually Find the Right Code?
RAG is the incumbent. It's what most tools were built on, and it works on a just-in-time premise: don't overwhelm the model, fetch what matters when it matters.

The pipeline reads left to right. The repo gets segmented into chunks, each chunk becomes a high-dimensional embedding, and everything lands in a vector database. When you ask a question, the system runs a cosine-similarity search, grabs the closest chunks, and staples them onto your prompt.
The naive version of this splits code by character count, which is how you end up with a chunk that ends mid-function. Modern tools stopped doing that. They parse code into an Abstract Syntax Tree first — using something like tree-sitter — and split at functional boundaries. A function stays whole. A class stays whole. The splits land where a human would put them.
Two more refinements matter, and they're the reason RAG survives contact with a real enterprise:
- Merkle-tree hashing. The system hashes every file and diffs the tree. When you save one file, only that file re-embeds. No full re-index. This is the property that makes RAG cheap to keep current.
- Client-side path masking. File paths get cryptographically obfuscated before any metadata leaves the machine, so the cloud search engine never sees your directory structure.
So RAG is dynamic, cheap to update, and privacy-aware. Sounds great. Then you hit the wall everyone hits.
Why Does RAG Lose the Plot in Long Contexts?
Two problems, and the second is the one that keeps me up at night.
The first is retrieval quality. Vector embeddings compress every document into a fixed-dimensional space before your query ever arrives. That compression loses nuance — especially the cross-file architectural dependencies that don't sit near each other in vector space. And "semantically similar" is not the same as "logically relevant." Ask for a specific auth mechanism and the search may hand you a probabilistically similar but functionally unrelated crypto routine. Close in vector space. Useless in practice.
The second problem is nastier because it's about the model's attention itself.

This is the "Lost in the Middle" phenomenon, and it's not a quirk — it's a structural property of how transformers allocate attention. Models pay disproportionate attention to the very start of the context (primacy bias) and the very end (recency bias). The middle gets shortchanged.
Here's the part that changes how you should think about window sizing. The biases don't scale uniformly. When your input sits under 50% of the window, primacy holds — the model still reliably recalls early instructions. Cross that 50% threshold and primacy degrades hard. Now only recency is really working. Everything you carefully placed at the top of an over-stuffed prompt? Effectively invisible.
Read that again, because it reframes the whole "million-token window" marketing. A bigger tank doesn't help if the model stops reading the middle of it.
RAG's mitigations are real but partial — two-stage retrieval with cross-encoder reranking to push the best chunks to the absolute start and end, positional-encoding tricks like Ms-PoE to preserve mid-range dependencies. They help. They don't solve holistic reasoning, where you genuinely need the whole codebase in view at once. And that's exactly the gap CAG walks into.
How Does CAG Skip the Whole Search Problem?
CAG's pitch is blunt: stop searching. Just load everything.

To see why this works, you need one mechanical detail about how transformers run. During the initial pass over a prompt — the prefill — the model computes Key and Value attention tensors for every single token. In a normal stateless call, all that work gets thrown away the instant the response finishes.
CAG's move is to keep it. Persist the KV cache in fast memory or on disk. When the next query arrives, reload the cached state, append the new query tokens, and jump straight to decoding. You never re-pay for the prefill on the stable part of the context.
The payoff is dramatic on latency. Time To First Token drops from seconds to milliseconds — reported speedups run up to ~56x on 130K-token prompts (see Asterisks). And because the entire codebase is genuinely present in the active cache, the model reasons across files without depending on a search algorithm's guess about what's relevant. No chunking loss. No "close in vector space." The whole thing is just… there.
If that sounds like it dodges every RAG problem at once — it does. Which is exactly why people over-adopt it. Because CAG has its own failure mode, and it's a brutal one.
What Breaks CAG? One File Save.

The KV cache is a sequential, mathematically interdependent structure. Each token's tensors depend on everything before it. So when you modify a file, the cached tensors for that file — and every token after it in the sequence — instantly become invalid. This is cache staleness, and it's the whole ballgame.
Now picture a real developer. They're saving files constantly, testing iteratively, editing in tight loops. That environment is the natural enemy of a frozen cache.
Under RAG, a save is a shrug: re-parse one file, re-embed, done. Under a pure CAG setup, a save to a core file blows up the prefix cache. The next query is a guaranteed cache miss, which forces a fresh, GPU-heavy prefill pass over the entire codebase. You pay full freight again.
The deciding factor is the ratio of saves to queries. Edit constantly and query rarely, and CAG makes you re-write massive contexts over and over — far more expensive than RAG's trivial vector updates. Query a stable repo many times without touching it, and CAG's cheap reads win. That ratio is the hinge the entire decision swings on, and I'll put hard numbers on it in Part 2.
So Which One Do the Real Tools Actually Use?
Neither, purely. And that's the honest answer that the RAG-vs-CAG framing tends to hide.

Line the five tools up and you get a spectrum, not a binary:
- Amazon Q leans RAG — a workspace-local index — but compacts aggressively (telemetry shows compaction kicking in around 71% of its 200K window) and can't natively see across multiple repos. An agent in the auth-service repo is blind to the payment-service repo.
- GitHub Copilot is RAG-first with model routing (the HyDRA engine cascades from small models to big ones by query complexity) plus heavy provider-level prompt caching. Switch models mid-session and you break that cache.
- Cursor is the hybrid poster child: AST-based local RAG through a serverless vector engine, wrapped in aggressive Anthropic prompt caching. Users report ~97% cache-hit rates on long, structured agentic threads.
- Kiro sidesteps the whole fight with Spec-Driven Development — it generates stable
design.mdandtech.mdartifacts, which are small and change rarely, so they cache near-perfectly and sit inside the peak-attention zone. It solves Lost-in-the-Middle and staleness at the same time. - Antigravity goes furthest toward CAG: explicit context caching via the Gemini API, persistent Cache IDs, contexts up to 2M tokens, with programmatic TTL lifecycle management.
And the bridges between the two philosophies already have names. CRAG adds a self-correcting evaluator that scores retrieved chunks and re-queries when they're weak. kvRAG retrieves chunks the RAG way but loads their precomputed KV states, skipping prefill for those segments. CacheRAG caches logical query plans instead of raw text.
What this means for engineering leaders: the question is never "RAG or CAG." It's "which parts of my context are stable enough to cache, and which are too volatile to freeze." Get that split wrong and you either pay for constant re-prefills or you fragment the reasoning you were trying to preserve.
The One-Sentence Summary
RAG fetches a little on demand and stays cheap to update; CAG remembers everything and stays cheap to read — and the entire decision comes down to how often your context changes versus how often you query it.
What Comes Next
That's the mechanics. Part 2 is where it gets expensive: the token economics that decide when each approach actually pays off (there's a breakeven point, and then there's an inversion that surprises people), and the hardware reality underneath — the VRAM math, the tiered storage, and why serious deployments physically split prefill from decode.
If Part 1 was "how do these two things work," Part 2 is "what do they cost, and what does it take to run them at scale without the GPU bill eating your margin."
Asterisks — verify before you cite
A few claims here are drawn from the underlying research brief and third-party reports rather than my own first-hand measurement. Treat these as directional and confirm against a primary source before repeating them:
- The ~56x TTFT speedup on 130K-token prompts comes from published CAG evaluations. It's a demonstration figure under specific conditions, not a guarantee for your workload.
- Tool-specific behaviors — Copilot's HyDRA routing, Cursor's ~97% cache-hit rate, Amazon Q's ~71% compaction trigger, Antigravity's 2M-token contexts and Cache IDs, Kiro's SDD artifacts — are a mid-2026 snapshot. These surfaces move fast; verify against current docs before relying on a specific path or number.
- The 50% positional-bias threshold for primacy degradation is from empirical long-context research. The exact crossover varies by model and evaluation setup.
This is Part 1 of a two-part companion to the Context Engineering series. Toolkit: github.com/navendubrajesh/context-management-for-agents · Writing: medium.com/@navendubrajesh