CAG (Cache-Augmented Generation) works by loading an entire knowledge base into the model's context once, computing its KV cache, and reusing that cache for every question that follows — no per-query retrieval. It takes the opposite path from RAG: RAG searches fresh each time, CAG prepares in advance, saving retrieval time and the risk of missing a key passage.
How CAG actually runs
It works in two steps. First, preprocessing: the full text of the knowledge base is placed at the start of the prompt in a fixed order, one forward pass computes the KV cache, and that cache is stored — it is only rebuilt when the knowledge base changes. Second, question answering: each question is appended after the cache, and the model only computes the small new part, skipping the cost of re-encoding the whole corpus. When calling an API, the same effect comes from prompt caching: an identical prefix hits the cache, change one character and it must be recomputed. The idea comes from the paper "Don't Do RAG", whose authors verified on SQuAD and HotpotQA that CAG can match or even beat RAG on small knowledge bases.
How CAG differs from RAG, prompt caching, and the KV cache
| Approach | What happens per question | Knowledge base it suits | Weakness |
|---|---|---|---|
| RAG | Retrieves a few relevant passages, then answers | Large, frequently changing | May miss a key passage |
| CAG | Reuses a precomputed cache of the whole library | Small, stable | Cannot fit a large library; one edit forces a rebuild |
| Prompt caching | Reuses computation for an identical prefix | Multi-turn chats with a fixed prefix | Breaks as soon as the prefix changes |
| KV cache | Stores already-computed attention state per round | Every conversation | A low-level mechanism, not a knowledge strategy |
The easiest pair to confuse is the last two: the KV cache is a low-level mechanism used by every model inference run — What Is KV Cache? explains how it saves repeated computation; prompt caching exposes that mechanism as an API capability; CAG is an application pattern built on top of both, answering how knowledge gets into the model. CAG and What Is RAG? are not either-or choices either: core documents can live in CAG so they are always present, while the rest stays with retrieval — hybrid systems like that are the more common setup in real deployments.
When not to use CAG
The test is twofold: can the knowledge base fit entirely in the context window, and how often does it change? When it exceeds the window, changes daily, or each question touches only a tiny slice of it, CAG is not worth it: the cache is a snapshot, change one thing and the whole thing must be rebuilt, and the cost grows with the size of the library. Benchmarks back this up: CustomGPT's HotpotQA comparison found CAG ahead with 50 documents and RAG more stable with 500; another test scaled to 620,000 tokens found CAG ahead on all fronts within 36,000 tokens and roughly even with retrieval at 108,000 tokens. So its best fit is specific: a bounded, stable internal knowledge base that gets asked about again and again, such as product manuals or policy Q&A; beyond that, retrieval remains the safer default.