AI / Cache Augmented Generation (CAG) Interview questions
Last updated
1. What is Cache-Augmented Generation (CAG)?
Cache-Augmented Generation (CAG) is a way of grounding an LLM in your own knowledge without a retrieval step. You load the whole knowledge base into the model's context ahead of time, run it through the model once, and keep the resulting key-value (KV) cache.
Every later question reuses that stored cache. The model only has to process the new question tokens, so there is no vector search, no chunk selection and no re-reading of the documents.
It works when the knowledge base is small and stable enough to fit inside the model's context window. The idea was popularised by the December 2024 paper Don't Do RAG: When Cache-Augmented Generation is All You Need for Knowledge Tasks.
Take quiz
The embedding vectors of every user question
A fine-tuned copy of the model weights
The KV cache of the preloaded documents
A ranked list of top-k document chunks
The knowledge base changes every few seconds
The corpus is far larger than any context window
Each user needs a different set of documents
The knowledge base fits inside the model's context window
2. What problem does CAG solve?
CAG targets the cost and fragility of the retrieval step in a RAG pipeline. In RAG, every query pays for embedding, vector search, optional reranking and a fresh prefill of the retrieved chunks.
It also inherits retrieval mistakes. If chunking splits an answer in half, or the top-k cutoff misses the one document that mattered, the model never sees the evidence and answers from incomplete context.
- Latency: no retrieval hop and no re-reading of documents.
- Accuracy: the model sees the full corpus, so nothing is left out by a retriever.
- Complexity: no vector database, embedding model or chunking strategy to tune.
Take quiz
Missing the right chunk because retrieval ranked it too low
Hallucinations caused by high temperature
Tokenizer mismatches between languages
Model weights going out of date
The tokenizer
The vector database and embedding pipeline
The GPU serving the model
The system prompt
3. What is a KV cache in transformers?
In self-attention, every token produces a key and a value vector at each layer. When the model generates the next token, it needs the keys and values of all previous tokens.
Instead of recomputing them every step, the model stores them in the KV cache and only computes keys and values for the newest token. This turns generation from quadratic recomputation into a cheap append-and-attend step.
The price is memory: the cache grows linearly with the number of tokens. CAG simply takes this existing mechanism and applies it to the knowledge documents, so the expensive part is done once.
Take quiz
The raw text of previous prompts
The final output logits of every step
Key and value vectors of tokens already processed, per layer
Embedding vectors from an external vector store
It stays constant
Exponentially with every token
Only when the vocabulary grows
Linearly with the number of tokens
4. What are the three phases of a CAG workflow?
The CAG paper describes three phases:
- Knowledge preloading: documents are formatted into one prompt, passed through the model, and the resulting KV cache is saved.
- Inference: the user's question is appended to the cached context, only the new tokens are processed, and the answer is generated using the cache.
- Cache reset: the question and answer tokens are trimmed off so the cache returns to its clean preloaded state, ready for the next query.
Phase 1 happens offline or at startup. Phases 2 and 3 repeat for every request.
Take quiz
Knowledge preloading
Cache reset
Inference
Answer reranking
Re-embed the documents into a vector index
Return the cache to its preloaded state for the next query
Fine-tune the model on the latest answer
Delete the documents from disk
5. What is knowledge preloading in CAG?
Knowledge preloading is the one-time step where you turn your documents into a reusable KV cache. You gather the documents, clean them, and concatenate them into a single prompt, usually after an instruction such as "answer only from the documents below".
You then run one forward pass (the prefill) over that prompt and save the past_key_values the model produced. That cache represents everything the model "knows" about your corpus for this deployment.
Document order and formatting matter, because any later change to the prefix invalidates the cache from that point onwards.
Take quiz
A vector index of document chunks
A set of new model weights
A KV cache representing the whole document prefix
A list of likely user questions
The tokenizer sorts them alphabetically anyway
Only the last document is ever cached
Order controls the embedding dimension
Changing earlier text invalidates the cached values after it
6. What is Retrieval-Augmented Generation (RAG)?
RAG grounds an LLM by fetching relevant text at query time. Documents are split into chunks, embedded, and stored in a vector database. When a question arrives, it is embedded too, and the closest chunks are pulled back.
- Chunk and embed the corpus (offline).
- Embed the user query.
- Run similarity search and take the top-k chunks, optionally reranking.
- Insert the chunks into the prompt and generate the answer.
RAG scales to corpora far larger than the context window and is easy to update. CAG is the alternative that skips steps 2 to 4 by keeping the knowledge in context.
Take quiz
A retriever that matches the query against stored chunks
A precomputed KV cache of the whole corpus
The model's training data cutoff
The user's browser history
Zero retrieval latency
Corpora much larger than the context window
No dependency on a vector store
Perfect recall of every document
7. What is a context window?
The context window is the maximum number of tokens a model can handle in one request, counting the prompt, any cached knowledge, and the tokens it generates. Modern models range from about 8K up to 1M tokens or more.
It is the hard ceiling for CAG. If your documents, the question and the expected answer do not fit together, the model cannot use them. Leave headroom: a corpus using 100% of the window leaves no room to answer.
A rough rule is that one token is about 0.75 English words, so a 128K window holds roughly 90,000 words of source material.
Take quiz
Only the user's question
Only the output tokens
Prompt tokens, cached knowledge and generated tokens
Only tokens stored in the vector database
The model refuses prompts above 50% usage
The KV cache is deleted at 100% usage
Output tokens are free and unlimited
The question and the answer also need token space
8. What is prefix caching?
Prefix caching reuses the KV cache for the leading tokens that two prompts have in common. It works because of causal attention: the key and value of a token depend only on the tokens before it, so an identical prefix always yields identical cached values.
If a new prompt starts with the same 50,000 tokens as an earlier one, the server loads the stored cache and only processes the different tail. The first token that differs breaks the match, and everything after it must be recomputed.
This is the serving-side mechanism that makes CAG efficient, and it is built into engines like vLLM and SGLang.
Take quiz
Each token's keys and values depend only on earlier tokens
Because the model memorises full prompts
Because the tokenizer hashes the whole prompt
Because attention is bidirectional
Only the last token is recomputed
The whole cached prefix no longer matches
The cache is automatically rewritten correctly
Nothing, the cache is position independent
9. What is prompt caching in LLM APIs?
Prompt caching is the hosted version of prefix caching. The provider stores the processed prefix of your prompt for a short time and charges less to read it back, while also lowering latency.
Anthropic lets you mark cache breakpoints with cache_control, with a default lifetime of about 5 minutes and an optional longer one. OpenAI applies caching automatically to long prompts, and Google Gemini offers both implicit and explicit context caching.
Minimum lengths, lifetimes and discounts differ by provider and change over time, so check the current documentation. In effect it is a time-limited, provider-managed form of CAG.
Take quiz
At double the normal rate
It is always free and unlimited
At a discounted rate compared with fresh input tokens
At the output token price
Your own browser
A vector database you host
The model weights permanently
The API provider, for a limited lifetime
10. What are the main benefits of CAG?
- Lower latency: no retrieval hop, and the documents are never re-processed per request.
- Fewer retrieval errors: the model has the complete corpus, so no relevant chunk is dropped by a ranker.
- Better multi-document reasoning: information spread across files is all visible at once.
- Simpler stack: no vector database, chunking rules or reranker to maintain.
- Consistent behaviour: the same context gives more predictable answers than varying top-k results.
The CAG paper reported lower generation time than RAG on its benchmarks, with the gap widening as the reference text grew.
Take quiz
Fewer missed facts caused by retrieval
Smaller GPU memory use
Faster fine-tuning
Automatic knowledge updates
The inference server
Chunking, embedding and a vector database
The tokenizer
The user interface
11. What are the main limitations of CAG?
- Size cap: the corpus must fit in the context window with room to spare.
- Memory cost: KV caches are large, often several gigabytes for long contexts.
- Staleness: any document change means rebuilding the cache.
- Long-context weakness: models can overlook facts buried in the middle of huge prompts.
- No per-user filtering: everything in the cache is visible to the model for every request.
None of these are fatal for a stable, bounded corpus, which is why CAG suits some jobs and not others.
Take quiz
Inability to use any tokenizer
Mandatory fine-tuning
High GPU or CPU memory consumption
Lack of answer streaming
Nothing, the cache updates itself
Re-embed only the user question
Restart the vector database
Rebuild or refresh the affected cache
12. What kind of knowledge bases suit CAG?
CAG fits knowledge that is bounded, stable and queried often. The same expensive prefill is paid once and amortised across many questions.
| Good fit | Poor fit |
| Product manual for one release | Company-wide wiki with millions of pages |
| Internal HR or policy handbook | News feeds updated every minute |
| Fixed FAQ and support playbook | Per-user private documents |
| Contract or regulation set for one case | Rarely queried, one-off documents |
A quick test: if all the text fits in about half of your model's window and changes less than daily, CAG is worth prototyping.
Take quiz
A stable 60-page product manual
A live social media stream
A 50 GB data lake
Private mailboxes of 10,000 users
The model cannot read old text
The upfront preloading cost is never amortised
Caches cannot store rare words
Rare queries always fail
13. What is cache reset in CAG?
After each query, the cache holds the preloaded documents plus the question and the generated answer tokens. Cache reset removes that extra tail so the cache again contains only the original knowledge.
In Hugging Face Transformers this is done by remembering the original length and cropping back to it:
origin_len = cache.get_seq_length() # after preloading # ... answer a question ... cache.crop(origin_len) # reset
Without it, memory keeps growing and one user's question can leak into the next user's context.
Take quiz
The preloaded documents
The model weights
The question and answer tokens appended during a query
The tokenizer vocabulary
Doubles the context window
Saves the cache to disk
Re-encodes the documents
Trims the cache back to its preloaded length
14. What is the research paper behind CAG?
The reference paper is Don't Do RAG: When Cache-Augmented Generation is All You Need for Knowledge Tasks by Brian J. Chan and colleagues, published on arXiv in December 2024.
It argues that with long-context models, preloading a small, manageable corpus and caching its KV states can match or beat RAG without retrieval errors or latency. The authors compared CAG and RAG on question-answering benchmarks, namely SQuAD and HotPotQA, using a long-context Llama 3.1 model.
The paper also introduced the three-phase view: preload, inference and cache reset.
Take quiz
SQuAD and HotPotQA
ImageNet and COCO
GLUE and SuperGLUE only
MNIST and CIFAR-10
RAG is always cheaper than any alternative
With long context, preloading can replace retrieval for bounded corpora
Fine-tuning is the only reliable method
Vector databases are required at every scale
15. List the main components of a CAG system?
- Knowledge corpus and preprocessor: cleans, orders and formats documents into the prefix.
- Long-context LLM: a model whose window comfortably fits the corpus.
- KV cache store: GPU memory, CPU memory or disk holding the precomputed cache.
- Inference wrapper: loads the cache, appends the question, generates the answer.
- Reset and refresh logic: trims per-query tokens and rebuilds the cache when sources change.
- Monitoring: tracks hit rate, TTFT and memory use.
Notice what is missing compared with RAG: no embedding model, vector index or retriever.
Take quiz
The embedding model
The tokenizer
Reset and refresh logic
The reranker
The language model
The prompt
The user interface
The retriever
16. What is time to first token (TTFT)?
Time to first token is the delay between sending a request and receiving the first generated token. It is dominated by the prefill stage, where the model processes the whole prompt.
Prefill cost grows with prompt length, so a 100K-token prompt can take many seconds on a single GPU. CAG attacks this directly: the document tokens are already in the cache, so only the short question is prefilled.
This is why TTFT is the headline metric when comparing CAG to RAG or to plain long-context prompting.
Take quiz
Prefill of the prompt tokens
Detokenization of the output
Network DNS lookup
Model download
It generates fewer output tokens
Document tokens are already cached, so only the question is prefilled
It compresses the model weights
It skips the tokenizer
17. What is a cache hit and a cache miss in CAG?
A hit means the incoming prompt starts with exactly the tokens already cached. The engine loads the stored keys and values and prefills only the new tail.
A miss means the prompts diverge somewhere inside the cached prefix. Everything from that point on must be recomputed, which can be most of the prompt.
| Situation | Result |
| Same documents, same order, new question at the end | Hit |
| Timestamp inserted at the top of the system prompt | Miss |
| Two documents swapped in order | Partial or full miss |
| New document appended after the cached prefix | Hit on old part |
Take quiz
Appending a new question at the end
Increasing max_new_tokens
Editing the first line of the cached prefix
Lowering the temperature
Generating the answer
Tokenizing the question
Loading the model
Reprocessing the shared prefix tokens
18. Describe a simple CAG implementation using Hugging Face Transformers?
The pattern is: build the prefix, prefill it once into a DynamicCache, then reuse that cache for each question and crop it afterwards.
import torch from transformers import AutoTokenizer, AutoModelForCausalLM, DynamicCache name = "meta-llama/Llama-3.1-8B-Instruct" tok = AutoTokenizer.from_pretrained(name) model = AutoModelForCausalLM.from_pretrained( name, torch_dtype=torch.bfloat16, device_map="auto") docs = open("handbook.txt").read() prefix = f"Answer only from the documents below.\n\n{docs}\n\n" prefix_ids = tok(prefix, return_tensors="pt").input_ids.to(model.device) cache = DynamicCache() with torch.no_grad(): model(input_ids=prefix_ids, past_key_values=cache, use_cache=True) origin_len = cache.get_seq_length() def ask(question): q = tok(f"Question: {question}\nAnswer:", return_tensors="pt").input_ids.to(model.device) full = torch.cat([prefix_ids, q], dim=-1) out = model.generate(full, past_key_values=cache, max_new_tokens=200) cache.crop(origin_len) # cache reset return tok.decode(out[0, full.shape[-1]:], skip_special_tokens=True)
APIs shift slightly between Transformers versions, so verify against the release you use.
Take quiz
The forward pass that fills the DynamicCache with the prefix
tok.decode on the output
cache.crop(origin_len)
AutoTokenizer.from_pretrained
To reload the model weights
To remove the question and answer tokens after each query
To translate the answer
To shorten the prefix permanently
19. What is a hybrid CAG and RAG approach?
A hybrid keeps the most important, stable knowledge in a precomputed cache and uses retrieval only for what does not fit or changes often.
For example, a support assistant might cache the product manual and policy pages, then retrieve fresh release notes or a customer's ticket history and append those after the cached prefix. The cached part stays valid because the retrieved text is added at the end.
You get CAG's speed and full-context reasoning on the core material, and RAG's reach on the long tail.
Take quiz
Before the system prompt
Inside the middle of the cache
After the cached prefix
Instead of the question
Per-request ticket data
Random web search results
Only the user's question
Stable, high-value core knowledge
20. How do you apply CAG to a customer support FAQ bot?
- Collect the FAQ and support policies, remove duplicates, and keep a clear structure (title, then answer) for each entry.
- Estimate tokens. Around 300 entries at roughly 150 tokens each is about 45K tokens, comfortable inside a 128K window.
- Build the prefix: instructions first, documents in a fixed order, no timestamps.
- Preload once at deployment and keep the cache in GPU or CPU memory.
- Serve each question by appending it, generating the answer, then cropping the cache.
- Rebuild the cache from a CI job whenever the FAQ content hash changes.
Add an instruction to say "I don't know" when the answer is not in the FAQ, otherwise the bot may guess.
Take quiz
About 45,000
About 4,500
About 450,000
About 1.5 million
Every user question
A change in the FAQ content
A change of browser
Each new session cookie
21. What is the difference between CAG and RAG?
The core difference is when and how knowledge reaches the model. RAG fetches relevant chunks per query. CAG preloads the whole corpus once and reuses its KV cache.
| Aspect | CAG | RAG |
| Knowledge access | Entire corpus preloaded in context | Top-k chunks retrieved per query |
| Latency | No retrieval, only question prefill | Embedding, search and chunk prefill each time |
| Retrieval errors | None, but model may overlook facts | Possible missed or wrong chunks |
| Corpus size | Limited by context window | Practically unbounded |
| Freshness | Cache rebuild needed | Re-index changed documents |
| Access control | Per cache, coarse | Per document, fine-grained |
| Infrastructure | Long-context LLM and cache memory | Vector DB, embedder, retriever |
Neither is universally better. CAG trades scale and freshness for speed and simplicity.
Take quiz
CAG needs a vector database, RAG does not
RAG caches the whole corpus, CAG retrieves chunks
CAG preloads all knowledge, RAG retrieves it per query
Both retrieve top-k chunks, only the model differs
CAG, because caches compress any size
Neither can handle it
CAG, because it skips attention
RAG, because it is not bound by the context window
22. Why does CAG reduce latency compared to RAG?
A RAG request pays for several steps before generation starts: embedding the query, a vector search, often a reranker, and then a prefill over the retrieved chunks. Each step adds time, and the chunks are re-encoded on every request.
In CAG the documents were encoded once during preloading. A request only prefills the short question, then decodes. There is no network hop to a vector store and no reranking.
One honest nuance: with a very long cache, each decoded token attends over more keys, so per-token decode speed can be slightly lower. The gain comes from skipping retrieval and re-prefill, which are usually the bigger costs.
Take quiz
Vector search and reranking
Tokenizing the question
Generating output tokens
Loading the model
The cache is stored as text
Each new token attends over more cached keys
Output tokens are re-embedded
The model is retrained per query
23. Why does CAG eliminate retrieval errors?
Retrieval errors come from the selection step: poor chunk boundaries, an embedding that misjudges similarity, a top-k limit that drops the second document needed, or a reranker that demotes the right passage.
CAG has no selection step. The model sees every document, so a multi-hop question can combine facts from different files without a retriever having to guess both beforehand.
The failure mode does not vanish, it moves. Instead of the retriever missing a fact, the model can fail to attend to it in a very long prompt. So CAG removes retrieval errors, not reading errors.
Take quiz
The model misreading a long prompt
A stale cache after an edit
A relevant chunk dropped by the top-k cutoff
Running out of memory
The vector index becoming corrupt
The embedding model drifting
The chunk size setting
The model failing to use facts buried in long context
24. How does KV cache memory grow with context length?
It grows linearly with the number of tokens. Each token stores one key and one value vector in every layer:
bytes per token = 2 x layers x KV heads x head dim x bytes per value
For Llama 3.1 8B (32 layers, 8 KV heads, head dim 128, FP16) that is 2 x 32 x 8 x 128 x 2 = 131,072 bytes, or 128 KB per token.
| Context length | KV cache size (FP16) |
| 8K tokens | about 1 GB |
| 32K tokens | about 4 GB |
| 128K tokens | about 16 GB |
That is on top of the model weights, so long CAG contexts can need as much memory for the cache as for the model itself.
Take quiz
128 KB
128 bytes
128 MB
1.28 GB
Stays the same
Roughly doubles
Quadruples
Halves
25. How do you estimate KV cache memory for a CAG deployment?
- Read the model config: layers, number of KV heads and head dimension.
- Compute bytes per token with the formula, using 2 bytes for FP16 or BF16.
- Multiply by the number of cached tokens.
- Add room for the question, the answer and prefill activations.
- Multiply by the number of independent cache copies you keep in memory.
def kv_bytes(layers, kv_heads, head_dim, tokens, bytes_per=2): return 2 * layers * kv_heads * head_dim * tokens * bytes_per # Llama 3.1 70B, 128K tokens, FP16 print(kv_bytes(80, 8, 128, 128_000) / 1e9) # about 42 GB
The 70B model needs roughly 320 KB per token, so a full 128K cache is around 40 GB before weights. Sharing one prefix across requests, as vLLM does, keeps the number of copies at one.
Take quiz
Number of layers
Number of KV heads
The vocabulary size
Head dimension
It makes the tokens longer
It changes the model weights
It disables attention
It multiplies the memory by the number of copies
26. Why can't a cached chunk be reused at an arbitrary position?
A token's key and value are not a fixed property of its text. They depend on every earlier token (through causal attention in all layers above the first) and on its position (for example RoPE position encoding).
If you compute a chunk's cache on its own and later place it after a different set of documents, its keys and values no longer match what the model would have produced in that context. The result is degraded answers, since cross-document attention is missing.
That is why exact reuse only works for an identical prefix. Techniques such as CacheBlend try to approximate non-prefix reuse by selectively recomputing parts of the cache.
Take quiz
They are computed through attention over all earlier tokens
They are derived from the token's spelling only
They are sampled randomly each run
They come from the tokenizer vocabulary file
Any chunk placed anywhere
An identical leading prefix
Chunks from different models
Shuffled documents
27. When should you choose CAG over RAG?
- The corpus fits in the window with comfortable headroom (a common target is half or less).
- Knowledge is stable, changing weekly or slower rather than hourly.
- Query volume is high against the same documents, so preloading is amortised.
- Questions need cross-document reasoning that a top-k retriever might split apart.
- Latency matters and you want to avoid a retrieval hop.
- The team is small and a vector stack would be extra operational weight.
If most of these hold, prototype CAG first. If two or three fail, RAG or a hybrid is safer.
Take quiz
A 5 GB archive updated every minute
A different private corpus per user
A stable 40K-token policy set queried thousands of times a day
A single one-off lookup
Caches get smaller with traffic
The model gets cheaper per token
Retrieval gets slower with traffic
The one-time prefill cost is spread over many queries
28. When would you choose RAG over CAG?
- The corpus is larger than the context window, even after cleaning and deduplication.
- Data changes constantly, such as tickets, prices or news, and rebuilding a cache each time is wasteful.
- Per-user permissions must be enforced at document level.
- You need chunk-level citations or an auditable record of exactly which passages were supplied.
- Your model has a short window or limited GPU memory.
RAG also costs less per query when each question needs only a few hundred tokens of the corpus and you pay per input token.
Take quiz
Per-user document-level access control
A fixed 20-page FAQ
A need for zero retrieval hops
A corpus that never changes
Keeping all text in the window
Re-indexing only the changed documents
Avoiding any vector store
Preloading everything for free
29. What is the difference between CAG and fine-tuning?
Fine-tuning changes the model's weights so knowledge is baked in. CAG leaves the weights alone and supplies knowledge as context with a precomputed cache.
| Aspect | CAG | Fine-tuning |
| Where knowledge lives | Context and KV cache | Model weights |
| Update cost | Re-prefill (minutes) | Retrain (hours or more) |
| Exactness of facts | Text is literally in context | Facts may be blurred or forgotten |
| Removing a document | Delete and rebuild cache | Hard, may need retraining |
| Upfront cost | Prefill compute and cache memory | Training compute and data prep |
Fine-tuning is better for style, format and behaviour. CAG is better for facts that must stay accurate and updatable.
Take quiz
In a KV cache
In a vector database
In the model weights
In the system prompt only
Fine-tuning, by editing one weight
Neither can ever change
Fine-tuning, with zero compute
CAG, by rebuilding the cache without it
30. What is the difference between CAG and long-context prompting without caching?
Both put the full documents in the prompt, so answer quality is similar. The difference is what happens on every call.
Plain long-context prompting re-processes all document tokens on each request. CAG processes them once and reuses the stored keys and values.
Take a 100K-token corpus and 1,000 queries a day. Without caching you prefill about 100 million document tokens a day. With CAG you prefill 100K once, plus the short questions.
So CAG is not a new way to read documents, it is a way to stop paying repeatedly for reading them.
Take quiz
The document prefill is paid once instead of per request
The model reads fewer documents
Answers are always more accurate
The context window becomes unlimited
About 100 thousand
About 100 million
About 1 million
About 10 billion
31. How does prefix caching work in vLLM?
vLLM stores KV memory in fixed-size blocks (PagedAttention). With automatic prefix caching, each full block is identified by a hash of its tokens together with the hash of the block before it.
- A new request is split into blocks and hashed.
- Hashes are matched against blocks already in GPU memory.
- Matching blocks are shared by reference, and only the remaining tail is prefilled.
- Unused blocks are evicted with a least-recently-used policy when memory runs low.
from vllm import LLM llm = LLM(model="meta-llama/Llama-3.1-8B-Instruct", enable_prefix_caching=True, max_model_len=65536)
Recent vLLM versions enable this by default, but the flag shows the intent. Because the hash chains through previous blocks, any early change breaks every later block.
Take quiz
The filename of the prompt
The user's API key
A hash of its tokens chained with the previous block's hash
A random UUID per request
They are written back to the prompt
They are converted to text
They are never removed
They are evicted, least recently used first
32. How does RadixAttention in SGLang support CAG-style reuse?
SGLang keeps KV caches in a radix tree keyed by token sequences. A shared system prompt becomes the trunk, document sets become branches, and individual questions become leaves.
flowchart TD R[Root] --> S["System prompt"] S --> A["Handbook A tokens"] S --> B["Handbook B tokens"] A --> Q1["Question 1"] A --> Q2["Question 2"] B --> Q3["Question 3"]
A new request walks the tree to find the longest matching prefix and reuses that node's cache. Leaves that are no longer referenced are evicted by LRU, and the scheduler can group requests that share prefixes.
It suits CAG deployments where several corpora or few-shot setups share a common beginning.
Take quiz
The longest matching token prefix in a radix tree
A similarity score between embeddings
A hash of the user's name
The output length
The root node
Unreferenced leaves, by LRU
The most frequently used trunk
The system prompt always
33. How do you structure a prompt for maximum cache hits?
Put everything that never changes first, and everything that changes last. Since a cache is a prefix match, the stable part must come before any variable part.
| Position | Content | Cache behaviour |
| 1 | System instructions | Stable, cacheable |
| 2 | Tool or schema definitions | Stable, cacheable |
| 3 | Knowledge documents in fixed order | Stable, cacheable |
| 4 | Conversation history | Grows, partly cacheable |
| 5 | Current user question | Always new |
- Never put timestamps, request IDs or user names near the top.
- Serialise JSON with sorted keys and consistent whitespace.
- Keep tool definitions identical between calls.
- Place cache breakpoints right after the documents.
Take quiz
At the very start
Between system prompt and tools
At the end of the prompt
Inside the documents
A fixed instruction sentence
A stable tool definition
A static document header
A current timestamp
34. How does provider prompt caching relate to CAG?
Provider prompt caching is CAG delivered as a service. You send the same long prefix, the provider reuses the KV state it computed earlier, and you pay a reduced rate for those cached tokens.
The differences are control and lifetime. You cannot download or inspect the cache, it expires after a short time unless refreshed, and writing it may cost more than a normal request. You also have to mark where the cache should end.
client.messages.create( model="claude-sonnet-5-5", max_tokens=500, system=[ {"type": "text", "text": "Answer only from the handbook."}, {"type": "text", "text": HANDBOOK, "cache_control": {"type": "ephemeral"}}, ], messages=[{"role": "user", "content": question}])
Check usage.cache_read_input_tokens in the response to confirm hits. Exact pricing and lifetimes change, so verify them in the provider docs.
Take quiz
A later call may need to re-create the cache
The model forgets the question
The API key is revoked
The model weights reload
max_tokens
cache_read_input_tokens
stop_reason
temperature
35. How do you handle knowledge updates in a CAG system?
- Hash the sources. Keep a content hash per document and the whole corpus so you know when the cache is stale.
- Rebuild on change. A CI job or scheduler re-prefills the prefix when the hash changes.
- Swap versions safely. Build the new cache beside the old one, then switch traffic, so users never hit a half-built cache.
- Order by volatility. Put rarely changing documents first and fast-changing ones last, so an edit only invalidates the tail.
- Append urgent facts. New documents added at the end leave the existing prefix valid.
If updates arrive faster than a rebuild can finish, that is a signal to move the fast-changing part to RAG.
Take quiz
The model reads the end first
It shrinks the vocabulary
An edit only invalidates the cache after the change
End tokens are never cached
Delete the old cache before building
Edit the live cache in place
Restart every client
Build it alongside the old one, then switch
36. What happens when the knowledge base exceeds the context window?
The prompt cannot be built. The request fails or the oldest tokens are truncated, and either way the model works with incomplete knowledge. You need to reduce or split the problem.
| Option | Trade-off |
| Deduplicate and trim | Cheap, but may remove useful detail |
| Summarise sections | Smaller prefix, risk of losing exact facts |
| Shard by topic with a router | Several caches, plus a routing step |
| Hybrid with RAG | Core in cache, tail retrieved |
| Move to a longer-window model | Simple, but more memory and cost |
| Compress or evict KV entries | Saves memory, may hurt accuracy |
Most teams end up with a hybrid, because the long tail rarely deserves a slot in the cache.
Take quiz
Cache the core and retrieve the long tail
Ignore the limit and send it all
Lower the temperature
Increase the batch size
A larger tokenizer
A router that picks the correct cache per question
A second copy of every user
Fine-tuning each shard
37. Why does the lost-in-the-middle problem affect CAG?
Studies of long-context models (Liu et al., 2023) found that accuracy is highest when the relevant passage sits at the start or end of the prompt and dips when it is buried in the middle.
CAG makes this more likely, because the corpus is large and the answer can be anywhere in it. Removing the retriever does not help if the model skims past the passage.
- Put the most important documents first or last.
- Keep the corpus lean, since every extra page dilutes attention.
- Label each document with a clear ID and title so the model can locate it.
- Ask the model to quote the supporting passage before answering.
- Test with needle-in-a-haystack checks at different depths.
Newer models are noticeably better here, but you should measure your own model rather than assume.
Take quiz
Exactly in the middle
Only in the final sentence
At the beginning and end of the prompt
Evenly everywhere, always
Raising the sampling temperature
Adding a second vector database
Shortening the output limit
Ordering key documents at the edges of the prefix
38. How do you troubleshoot a low cache hit rate?
- Log cached token counts per request (for example
cache_read_input_tokensor the engine's hit metrics). - Diff two prompts that should match and find the first differing character.
- Look for dynamic text near the top: timestamps, request IDs, user names, random few-shot order.
- Check serialisation: unsorted JSON keys, trailing whitespace and changing delimiters all alter tokens.
- Check parameters: a changed model version, tool list or system setting can invalidate the prefix.
- Check expiry: sparse traffic may let a time-limited cache lapse, so a periodic keep-alive may help.
- Check minimum length: prefixes shorter than the provider's threshold are not cached.
- Check routing: with several replicas, send repeat prefixes to the same one.
Most real problems turn out to be step 3 or step 4.
Take quiz
Diff two prompts that should match to find where they diverge
Increase max_new_tokens
Switch to a bigger GPU
Lower the temperature
Replicas change the tokenizer
Each replica holds its own cache, so requests may land elsewhere
Replicas delete the system prompt
Caches are only allowed on one server
39. How can KV cache quantization reduce CAG memory?
KV cache quantization stores keys and values in lower precision, such as FP8 or INT8 instead of FP16. Fewer bytes per value means a smaller cache, so a longer corpus fits or more caches fit on the same GPU.
| Format | Bytes per value | Llama 3.1 8B at 128K tokens |
| FP16 / BF16 | 2 | about 16 GB |
| FP8 / INT8 | 1 | about 8 GB |
| INT4 (with scales) | about 0.5 | about 4 to 5 GB |
The cost is a small risk of accuracy loss, which tends to grow at lower bit widths and on tasks that need exact recall. Research methods such as KIVI and KVQuant push towards 2-4 bit caches, and serving engines such as vLLM expose a KV cache dtype option.
Always re-run your evaluation set after switching precision.
Take quiz
The number of layers in the model
The size of the vocabulary
The number of bytes used per stored key and value
The tokenizer's merge rules
The prompt becomes illegal
The model forgets its language
The GPU stops supporting FP16
Reduced accuracy on exact-recall questions
40. How does grouped-query attention reduce CAG memory costs?
In standard multi-head attention (MHA) every query head has its own key and value head. Grouped-query attention (GQA) lets several query heads share one KV head, and multi-query attention (MQA) shares a single one across all heads.
Since the cache stores only keys and values, fewer KV heads means a proportionally smaller cache.
| Variant (32 query heads) | KV heads | KV per token (32 layers, FP16) |
| MHA | 32 | 512 KB |
| GQA (Llama 3.1 8B) | 8 | 128 KB |
| MQA | 1 | 16 KB |
At 128K tokens, the MHA cache would be around 64 GB, while the GQA one is 16 GB. That is why GQA models are the practical choice for CAG.
Take quiz
Key and value heads
The output projection
The tokenizer
The feed-forward layers
2 times
4 times
32 times
It does not change
41. How do you persist and reload a KV cache from disk?
Persisting a cache avoids repeating the prefill after a restart or when scaling out. You serialise the cache tensors, store them with metadata, and load them into GPU memory at startup.
legacy = cache.to_legacy_cache() # tuple of (key, value) per layer torch.save({"kv": legacy, "meta": {"model": name, "corpus_sha": sha}}, "kv_handbook_v12.pt") blob = torch.load("kv_handbook_v12.pt", weights_only=True) cache = DynamicCache.from_legacy_cache(blob["kv"])
- Match exactly: store model name, dtype, tokenizer and corpus hash, and refuse to load on a mismatch.
- Mind the size: a 128K cache for an 8B model is about 16 GB, so load time depends on disk and PCIe bandwidth.
- Stay safe: never load pickled files from untrusted sources.
Helper names change between Transformers versions, so verify against your installed release.
Take quiz
To compress the tensors
To speed up the tokenizer
To refuse loading a cache built from different documents
To rename the model
The number of users
The temperature setting
The prompt language
Its size and the disk and PCIe bandwidth
42. How do you compare the cost of CAG and RAG?
Cost depends on how you run the model. Build a per-query model for each side rather than assuming CAG is cheaper.
- CAG: one-time prefill, cache storage (memory), plus question and output tokens each query.
- RAG: embedding and vector DB costs, plus the retrieved chunks as input tokens each query.
Take a hosted API with a 100K-token corpus. With provider caching, cached reads might be billed at about 10% of the normal input price, so each query costs the equivalent of roughly 10K full-price tokens. A RAG query that retrieves five 500-token chunks pays for about 2.5K full-price tokens.
| Setup | Input cost per query (full-price token equivalents) |
| Uncached long context | 100,000 |
| CAG with provider cache (about 10% rate) | about 10,000 |
| RAG with 5 x 500-token chunks | about 2,500 |
So on an API, CAG may not win on token spend, but it can win on latency and answer quality. On self-hosted GPUs the cost is cache memory and throughput, so measure both. Use real prices from your provider.
Take quiz
A RAG query pulling a few small chunks
Uncached long context
Sending the whole corpus every time with no cache
They are always exactly equal
Corpus size limits
Latency and cross-document answer quality
Data freshness
Per-document permissions
43. How do you design a hybrid CAG and RAG pipeline?
The design question is what belongs in the cache, what is retrieved, and how they are combined without breaking the cache.
flowchart LR
Q["User question"] --> R{Router}
R -->|Covered by core knowledge| C["Cached core prefix"]
R -->|Needs fresh or long-tail data| S[Retriever]
S --> T["Top-k chunks appended after cache"]
C --> L[LLM]
T --> L
L --> A[Answer]
- Core tier: stable, high-traffic documents go into the cached prefix.
- Retrieval tier: volatile or rarely used documents live in a vector store.
- Append, never insert: retrieved text goes after the prefix so the cache stays valid.
- Budget tokens: reserve window space for retrieved chunks, question and answer.
- Deduplicate: drop retrieved chunks already present in the core cache.
Measure each tier separately, otherwise you cannot tell which one is helping.
Take quiz
Inside the cached documents
Before the system prompt
After the cached prefix
In place of the question
To increase the temperature
To reduce the vocabulary
To reset the cache
To avoid wasting tokens on text the model already has
44. How do you enforce access control in a CAG system?
A CAG cache holds knowledge for the model, and the model can repeat anything in its context. So access control must happen before content enters a cache, not by instructing the model to hide things.
- One cache per permission tier: for example public, employee and finance caches, with users routed to the highest tier they may see.
- Never mix restricted and open documents in one shared prefix.
- Use RAG for per-user data, where document-level filters are natural.
- Isolate tenants: do not share prefix caches across customers, because shared caches can leak information through timing differences.
- Log which cache version served each answer for audits.
A prompt that says "do not reveal salaries" is a guideline, not a security control.
Take quiz
Separate caches per permission tier
One shared cache with a polite instruction
Hiding the cache from logs
Lowering the temperature
The model ignores all instructions
The text is still in context and can be extracted
Caches cannot hold text
Instructions delete the cache
45. Explain the execution flow of a CAG request?
sequenceDiagram participant U as User participant S as Serving layer participant K as Cache manager participant M as LLM U->>S: Question S->>K: Load cache for corpus version K-->>S: KV cache (preloaded prefix) S->>M: Question tokens + cache M-->>S: Prefill question, decode answer S->>K: Crop cache to original length S-->>U: Answer
- The serving layer receives the question and picks the cache matching the current corpus version.
- The cache manager returns the preloaded KV state, or builds it on a miss.
- Only the question tokens are prefilled. Their keys and values attach to the existing cache.
- The model decodes the answer token by token, attending over the cache plus the new tokens.
- The cache is cropped back to its original length so the next request starts clean.
- The answer is returned along with metrics such as TTFT and cached token count.
Take quiz
All document tokens
The entire previous conversation of every user
Only the new question tokens
Every token in the vocabulary
The documents are re-uploaded
The model is retrained
The cache is deleted permanently
The cache is cropped back to its preloaded length
46. How do you evaluate a CAG system against RAG?
Compare both systems on the same model, corpus and questions, and measure quality, speed and cost together.
| Dimension | Metrics |
| Answer quality | Exact match, F1, BERTScore, LLM-judge against reference answers |
| Faithfulness | Share of claims supported by the corpus, refusal rate on unanswerable questions |
| Latency | TTFT, total response time, p95 |
| Cost and capacity | Cost per 1,000 queries, cache memory, throughput |
| Cache health | Hit rate, rebuild time, staleness |
Build the test set from three groups: single-hop questions (SQuAD style), multi-hop questions (HotPotQA style) and questions the corpus cannot answer. Add needle-in-a-haystack probes at different positions to catch long-context weaknesses.
Run each system repeatedly under realistic load, because cold-start and warm-cache numbers differ widely.
Take quiz
To measure whether the system admits it does not know
To make the test faster
To reduce memory
To warm the cache
Disk capacity
Time to first token
Tokenizer size
Embedding dimension
47. What happens if you skip cache reset between queries?
Each request leaves its question and generated answer inside the cache. Without a reset these tokens pile up in front of the next request.
- Memory growth: the cache gets larger every query until the GPU runs out of memory.
- Window overflow: eventually the cache exceeds the model's context window and requests fail or get truncated.
- Cross-query contamination: the model may answer from earlier questions, and one user's data can surface in another user's response.
- Slower decoding: attention scans an ever longer cache.
origin_len = cache.get_seq_length() try: answer = generate(question, cache) finally: cache.crop(origin_len) # always reset, even on errors
If you do want multi-turn memory, keep a separate per-session cache rather than growing the shared one.
Take quiz
The model weights are overwritten
The tokenizer becomes case sensitive
One user's question can leak into the next user's context
The API key is exposed
To speed up decoding
To increase the window size
To avoid tokenization
So the cache resets even if generation raises an error
48. How does CacheBlend reuse non-prefix KV caches?
RAG systems retrieve different chunk combinations, so exact prefix reuse rarely applies. Reusing each chunk's independently computed cache is fast but skips cross-attention between chunks, which hurts answer quality.
CacheBlend (Yao et al.) precomputes the KV cache of each chunk separately. At query time it concatenates them, then selectively recomputes the KV values of a small fraction of tokens whose values would change most because of the new context, layer by layer.
The aim is a cache close to what a full prefill would give, at a much lower cost, so TTFT drops with little quality loss. It is a research technique that shows how CAG-style reuse can extend beyond one fixed prefix.
For a pure CAG system with a fixed prefix you do not need it. It matters when you mix cached chunks dynamically.
Take quiz
Reusing chunk caches that lack cross-chunk attention
Tokenizer vocabulary growth
GPU driver crashes
Slow disk writes
It retrains the model per query
It recomputes KV values for a small subset of tokens
It removes all chunks
It doubles the context window
49. How can you reduce hallucinations in a CAG system?
Putting documents in context reduces hallucination but does not remove it. The model may still guess when the answer is missing, or blend two conflicting documents.
- Instruct grounding: answer only from the documents, and say "not found" otherwise.
- Label documents with IDs and titles, and ask for citations to those IDs.
- Require quotes: have the model extract the supporting sentence before answering.
- Clean the corpus: remove duplicates and outdated versions that contradict each other.
- Lower the temperature for factual question answering.
- Test with traps: unanswerable and near-miss questions, and track the refusal rate.
A second verification pass can catch unsupported claims, at the price of extra latency.
Take quiz
Guess the most likely answer
Use its training data silently
State that the information was not found
Repeat the question
They reduce the tokenizer size
They disable the cache
They raise the temperature
They can contradict current documents and cause blended answers
50. How do you troubleshoot out-of-memory errors when loading a CAG cache?
- Estimate first: weights plus KV cache (2 x layers x KV heads x head dim x tokens x bytes) plus working memory. Compare it with GPU capacity.
- Look for duplicate copies: a deep copy of the cache per request multiplies memory. Share one prefix and crop instead.
- Check prefill peaks: processing a very long prompt in one pass needs large activation memory. Use FlashAttention or chunked prefill.
- Shrink the cache: trim and deduplicate the corpus, or quantize the KV cache to FP8.
- Choose a GQA model with fewer KV heads.
- Spread the load: use tensor parallelism across GPUs, or offload cold caches to CPU memory or disk.
- Limit concurrency: lower the batch size or the maximum model length.
Most surprises come from step 2 or step 3, because the cache itself is predictable but copies and prefill peaks are not.