Cache-augmented generation

Also known as: CAG

On this page 6

Cache-augmented generation (CAG) is a technique in which the entire relevant knowledge collection is loaded into a language model’s context window in advance, its key-value (KV) cache is precomputed and stored, and every later query is answered on top of that cached state without a retrieval step. In the field of Retrieval & knowledge, CAG is positioned as a simpler alternative to retrieval-augmented generation (RAG) for knowledge sets small enough to fit in a long context.

The approach became practical once models offered context windows of 128,000 tokens or more and inference servers supported prefix caching. Its central promise is to remove retrieval errors and retrieval latency at the cost of a fixed context budget and higher memory use per session.

How cache-augmented generation works

CAG runs in three phases. First, the documents are cleaned, concatenated and placed into a fixed prompt prefix, usually after the system instructions. Second, the model processes that prefix once in a prefill pass, producing the attention keys and values for every token, which together form the KV cache. Third, for each user query the server loads the stored cache, appends only the new question tokens and generates an answer.

Because the transformer has already computed attention states for the knowledge prefix, the expensive prefill work is paid once and amortized over many queries. After each answer, the cache is truncated back to the knowledge prefix, so earlier questions do not accumulate in the context.

PhaseWhat happensWhen it runs
Knowledge preloadingDocuments assembled into a fixed prefixOnce per knowledge update
Cache precomputationPrefill pass stores keys and valuesOnce per model and prefix
Cached inferenceQuery appended to the stored cacheEvery request
Cache resetContext truncated to the prefixAfter each answer or session
The three phases of a CAG system

Cache-augmented generation compared with RAG

The essential difference is where selection happens. RAG uses a retriever, embeddings or BM25, to choose a few relevant chunks per query, so the model sees only a fragment of the corpus. CAG lets the model see the whole corpus every time and relies on the attention mechanism to find relevant passages. This removes failure modes such as poor chunking, a weak embedding model or a missed document.

The table compares the two architectures on dimensions that usually drive the design decision.

DimensionCache-augmented generationRetrieval-augmented generation
Knowledge sizeBounded by the context windowPractically unbounded
Per-query latencyLow; no retrieval or full prefillRetrieval plus prefill of chunks
Retrieval errorsNone; whole corpus is visibleMissed or irrelevant chunks possible
InfrastructureModel server with cache storageVector index, retriever, pipelines
Knowledge updatesRecompute the whole cacheRe-index only changed documents
Memory per sessionHigh; full KV cache in GPU memoryModerate; short contexts
CAG and RAG on key design dimensions

Neither approach is universally better. CAG suits small, stable, frequently queried knowledge; RAG suits large, growing or permission-filtered collections. Hybrid systems cache a core set of documents and retrieve the long tail on demand.

CAG should also be distinguished from semantic caching, which stores previous answers and returns them when a new query is similar, and from fine-tuning, which writes knowledge into model weights. CAG changes neither the weights nor the answers: it caches only the internal attention state of the source text, so every response is still generated fresh from the full material. This makes CAG easy to update compared with fine-tuning, and more faithful than answer caching when questions vary in wording or detail.

When teams decide between the approaches, the practical test is simple. If the complete corpus fits comfortably in context, changes rarely and is shared by all users, CAG usually reduces moving parts. If any of those conditions fails, a retrieval layer remains the more robust and economical choice.

Sizing and configuring a CAG deployment

The first design question is whether the knowledge fits. A useful rule is to keep the cached prefix well below the model’s maximum context, leaving room for the query, instructions and answer, and to verify that answer quality does not drop at that length. KV cache memory grows linearly with prefix length, and for large models a cache of 100,000 tokens can occupy tens of gigabytes, depending on layer count, attention heads, precision and whether grouped-query attention is used.

ParameterTypical range or choiceWhat it affects
Cached prefix length10,000 to 200,000 tokensCoverage versus memory and accuracy
Cache precision16-bit, or 8-bit quantizedMemory use versus small accuracy loss
Cache storageGPU memory, CPU memory or diskReload latency and concurrency
Refresh frequencyPer release, daily or on changeFreshness versus recompute cost
Document orderingMost important material first or lastAttention to critical passages
Main parameters of a CAG deployment

Many hosted model APIs expose the same mechanism as prompt caching, which bills cached prefix tokens at a reduced rate and returns faster first tokens. Self-hosted stacks implement it through prefix caching in the inference server.

Limitations, risks and common mistakes

Long-context models do not attend equally well to every position. The lost-in-the-middle effect means facts buried in the middle of a very long prefix may be overlooked or confused, so a working CAG system must be evaluated on questions targeting every region of the corpus.

SymptomLikely causeRemedy
Answers ignore relevant documentsLost-in-the-middle attentionReorder or shorten the prefix
Outdated answersStale cache after content changeAutomate cache recomputation
High serving costLarge caches per concurrent sessionShare one cache, quantize it
Data leakage between usersOne cache mixing restricted contentSeparate caches per permission level
Typical problems in CAG systems
  • Access control is coarse: everything in the cache is visible to every query that uses it.
  • Any change requires recomputation: editing one paragraph invalidates the cache from that position onward.
  • Citations are harder: without retrieved chunks, source attribution depends on prompt instructions.

The short version

Cache-augmented generation means giving an AI model all the reading material before anyone asks a question. The model reads it once and keeps its notes. Each new question is answered from those notes. Nothing needs to be searched again.

Compare it to a student who memorizes a short handbook before an exam. That student answers quickly and never picks the wrong page. It only works when the handbook is short enough to remember. If the handbook changes, the student has to study it again.

Where it makes a difference

A software company runs an internal help assistant for its support staff. The relevant material is a product manual of about 60,000 tokens, updated once per release. The first version used RAG, but agents complained that answers sometimes missed steps spread across several chapters, because the retriever returned only five chunks.

The team switches to cache-augmented generation. At each release, a job precomputes the KV cache for the full manual and stores it on the inference server. Questions now reuse that cache, so the time to first token drops noticeably, and multi-chapter procedures are answered completely. Evaluation on 200 historical support questions shows fewer incomplete answers. The trade-off is accepted knowingly: the cache is recomputed on every manual update, and confidential customer records stay in a separate RAG pipeline with per-user access checks.