Name the layers of a RAG pipeline where caching can save cost, then explain why a semantic (embedding similarity) answer cache offers the biggest savings but introduces the most risk.
Cache at three layers: query embedding, retrieval results, full answers. The semantic answer cache saves most by matching paraphrases, but a loose threshold serves near-misses and a changed index goes stale.
Imagine a help desk where the same questions come in all day, just worded differently. To save effort you keep a folder of pre-written answers. The cheapest move is: when a new question looks like one you already answered, hand back the saved reply without doing any research. That saves the most time. But it has two traps. If you decide two questions are 'the same' too loosely, you hand someone the wrong answer because it only sort of matched. And if the rules changed since you wrote the saved reply, you are now handing out advice that used to be right and is now wrong. So you only reuse an answer when the new question is a very close match, and you throw out saved replies whenever the underlying facts change.
Concept explanation~2 min read
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Concept explanation~2 min read
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Caching is the first lever most teams reach for when a RAG bill gets uncomfortable, and rightly so — a cache hit can take a request from a full retrieve and generate cycle down to a lookup. But caching in RAG is not one thing. The pipeline has several stages, each producing an output you could in principle reuse, and they differ enormously in how much they save and how dangerous they are to get wrong.
The headline case is the semantic answer cache, which is simultaneously the biggest saver and the easiest to ship as a quiet correctness bug. This walkthrough lays out the three cache layers, explains why matching by meaning rather than exact text is what makes answer caching actually pay off, and then dissects the two ways a cached answer goes wrong — near-misses and staleness — along with the mitigations that keep the cache honest.
The three cache layers
A RAG request runs through stages: embed the query, search the index for the top-k chunks, then generate an answer from those chunks. You can place a cache after any of these, and each layer skips strictly more work than the one before it. The further down the pipeline the cache sits, the more it saves on a hit — and, as we will see, the more carefully it has to be guarded.
The query-embedding cache stores the embedding for a query so a repeat does not re-invoke the embedding model. This is the smallest saving — embedding is the cheapest step — but it is also the safest, because an embedding is a deterministic function of the query text and the embedding model. It never goes stale on its own; the only event that invalidates it is swapping the embedding model itself.
The retrieval cache stores the top-k chunks returned for a query, so a repeat skips the vector search. This saves the search cost and is moderately safe, but it does go stale when the index changes: if a document was added or edited, the cached top-k may no longer be the true top-k, so this layer already needs invalidation on index updates.
The answer cache stores the final generated response, so a hit skips retrieval and the LLM call entirely. This is by far the largest saving because it short-circuits the two dominant costs in the pipeline. It is also the riskiest, because the thing being reused is a synthesized answer that depends on both the query's intent and the current state of the corpus — so it inherits the retrieval layer's staleness risk and adds a matching risk of its own. The rest of the discussion is really about this layer.
Situations where this technique stops working.
2–4 min · Everything important, quickly.
Real products, models, and research that use this idea.
- GPTCache stores past query-answer pairs and returns a cached answer when a new query is embedding-close, exposing the similarity threshold as the central tuning knob.
- Customer-support assistants cache answers to evergreen 'how do I' questions while bypassing the cache for account-specific or recently changed policy queries.
What an interviewer would ask next. Try answering before peeking at the approach.
QHow do you set the similarity threshold for the answer cache without either serving near-misses or killing the hit rate?
Talk about calibrating against a labeled set of equivalent and near but different query pairs, choosing the threshold from the precision-recall curve, and weighting it by how costly a wrong cached answer is in the domain.
Red flags & common mistakes
The phrases that signal junior thinking. Click to expand.
Red flags & common mistakes
The phrases that signal junior thinking. Click to expand.
Loosening the similarity threshold to lift the cache hit rate, which starts serving near-miss answers to genuinely different questions — and forgetting to invalidate the cache when the index updates, so cached answers quietly go stale.
60 second bullets to scan on the way to the call.
List the three cache layers in a RAG pipeline
Order the layers by how much cost each hit saves
Primary sources. Browse if you want the original framing.
Same topic, related formats. Practice these next.