# How Do You Cache RAG Results?

> Where a RAG system can cache its work, when a cached answer is safe to reuse, and how to keep it fresh, for your AI interview.

**In short:** Cache a RAG system where its work repeats: the fixed start of the prompt, the search results for a query, and full answers to near-identical questions. Key every entry by the user's access rights, the document version and the model, so no cached answer leaks or goes stale.

AI and LLM interview question Q145, Retrieval-augmented generation (RAG). Intermediate, occasional (our estimate). From Venkat R. Murty's free Interview Kit: https://designervenkat.online/interview/ai/how-do-you-cache-rag-results

## Say it in 30 seconds

I cache at three places. First, the fixed start of the prompt, which the model provider can reuse. Second, search results, keyed by the user's access groups and the index version. Third, full answers to repeated questions, but only with a strict similarity threshold. Every entry expires, and a document change clears the entries that cite it. A cached answer must never reach someone who isn't allowed to see its source.

## Say it in 2 minutes

A lot of a RAG bill can come from repeated work, and that is the part caching can cut. The same question comes back, the same documents get searched again, and the same long instructions get sent again. So I cache where the work repeats, and I treat every entry as a copy that must expire.

First is the prompt start. Anthropic and OpenAI can reuse stored work for a prompt prefix that stays the same, so I keep the system prompt and tool definitions first and put the retrieved text after them. Second is search. I cache the chunks for a query under the user's access groups and the index version, so a permission change or a re-indexed document stops matching the old entries. Third is the full answer. A semantic cache returns a saved answer when a new question is close in meaning, and I set that threshold strictly, because a close match can still be a different clause.

The cache key decides safety. A key made from the question alone can hand one team's private answer to another team. Every entry has a time limit, and when a source document changes I clear the entries that cite it.

The trade-off is plain. Caching cuts cost and speeds up answers, but a wrong cached answer is worse than a slow fresh one. So I measure how often answers come from the cache and how often a reviewer marks one wrong, before I loosen the threshold.

## What is caching a RAG result, in plain words?

A RAG system (retrieval-augmented generation) searches your documents first, then has the model write the answer from what it found. Much of that work repeats. The same question comes back, the same documents get searched again, and the same long instructions get sent again. Caching saves the result of one of those steps, so the next request can reuse it.

Think of the research desk at a law firm. An associate who already pulled the clauses on one liability cap does not search the whole library again when a partner asks the same thing next week. That only works while the contracts are unchanged and the partner is allowed to see them. Caching has the same two conditions. In a RAG system the same idea holds: a saved answer is only safe while the source documents are unchanged and the person asking may see them.

- **Cache key:** The label a saved result is filed under. Change any part of the request and you look up a different entry.
- **Hit or miss:** The saved result was found (a hit, so the work is skipped) or not (a miss, so the work is done in full).
- **Similarity threshold:** How close in meaning a new question must be before a saved answer is reused. Some products count the other way round: a lower number is stricter.
- **Chunk:** A short piece of a document, cut so search can return only the relevant part.
- **Index:** The searchable store built from those chunks. Its version changes when documents are re-indexed.

## Which steps in a RAG system are worth caching?

There are three caches. The first two are yours to build, and the third is the provider's prompt prefix cache. A question checks them in order, and each one files its result under its own key.

*Diagram, Three caches and the answer, in order: A question checks the answer cache first. On a hit the answer comes straight back, with no search and no model call. On a miss it passes the search cache and the prompt prefix, and the answer is stored for next time.*
1. The question goes to the answer cache first.
2. On a miss, the search cache is checked before any search runs.
3. The chunks go to the model after its fixed prompt start.
4. The new answer is stored under its keys for next time.

1. **1. Answer cache:** Full answers, reused for questions close enough in meaning. It saves the most and carries the most risk, so it needs the strictest checks.
2. **2. Search cache:** The chunks found for a query, kept under the same filters and index version. It skips the search, but the model still runs.
3. **3. Prompt prefix cache:** The fixed start of the prompt: system instructions and tool definitions. Once it is long enough (the minimum depends on the model), the provider stores it and reuses it.

The provider cache has its own rules. Anthropic's docs say the cache covers the tools, the system prompt and then the messages, in that order, up to a block you mark. OpenAI's guide says the whole prefix has to match.

> "Cache reuse requires the entire rendered prefix to match." (OpenAI, prompt caching guide)
>
> "Changes at each level invalidate that level and all subsequent levels." (Anthropic, prompt caching docs)

So retrieved chunks go after the fixed start, never before it. If they sit above the system prompt, every new question breaks the cache from the system prompt onward, because a change at one level invalidates that level and all later ones. Anthropic's default lifetime is five minutes, so the saving is biggest when similar requests arrive close together.

## How does a semantic answer cache decide two questions match?

It turns each question into a vector, a list of numbers that stands for its meaning, then looks for a saved question with a close vector. Microsoft's Azure API Management docs describe the same pattern: cached answers can be returned for prompts that are similar in meaning as well as identical ones.

Azure calls the dial score-threshold, and it runs the other way round: the lower the number, the stricter the match. Its docs say: "A lower score threshold means that prompts must have higher semantic similarity to return cached responses." The example below uses plain similarity, where a higher number is a closer match.

Here is the trade-off, with made-up scores. "What is the late fee?" and "How much is a late payment fee?" might score 0.93, a match. A question about the fee for a missed direct debit might score 0.87. A threshold of 0.92 serves the first and sends the second to search.

- **Too loose: Different clauses match.** A question about the main liability cap gets the answer for the subcontractor cap. The two sound alike, and the wrong clause gets cited.
- **Tuned: Only true repeats match.** Set the threshold from a labelled sample of real questions, then count the wrong matches before you ship.

## What goes into the cache key, and what clears it?

The key decides who can see an answer and when it is stale. The chart shows the difference.

*Diagram, Two cache keys, two outcomes: A key made from the question alone hands one team's answer to another team. Adding the user's groups and the index version keeps each group's answers apart, and drops them once a document is re-indexed.*
1. A sales rep asks, and the cache hands over HR's answer.
2. Each group gets its own entry, and only the latest version is served.

1. **Who is asking:** Put the user's access groups in the key. A Finance-only answer must never reach a sales rep.
2. **Which index version:** Re-indexing a document changes the version, so older entries stop matching.
3. **Which model and prompt:** A new model or system prompt gives different answers, so it gets its own key.
4. **How long it lives:** Every entry expires. Set the time limit from how often your sources actually change.
5. **When a source changes:** Clear the entries that cite that document, not the whole cache.

> **Tip:** Asked how to make a cache safe, start with the key. Name its parts, then the expiry, then the clear-out rule.

## Worked example: how would this work at a bank?

Take a mid-size bank's policy assistant for branch staff (made-up numbers). It gets about 1,000 questions a day. Around 40 percent are the same 60 questions about fees, cut-off times and ID checks. The answer cache and the search cache handle those, so about 400 searches and model calls a day are skipped.

The catch is the clear-out rule. When the fee schedule is re-issued, every entry citing it is cleared that day, so a branch never reads last year's fee. The team checks two numbers each week: the share of questions served from cache, and the share of cached answers a reviewer marks wrong. If the wrong share rises, the threshold goes up.

### How would you cache for staff who can see different documents?

Take an HR policy bot where only HR can see salary bands. Without care, HR asks about a band, the answer is cached, and a manager asks the same question an hour later and gets it. The fix is in the key: the user's groups go in, so each group has its own entries.

I would also check access at read time, not only when the entry was written. Permissions change while an entry is still alive, and a cached answer must follow the current rule.

## What does the interviewer want to hear?

**A strong answer covers**

- [ ] Name the three caches: the answer, the search and the model's prompt prefix.
- [ ] Say what the cache key holds, and why access rights belong in it.
- [ ] Explain the similarity threshold, and how to tune it on real questions.
- [ ] Name what clears the cache: a source change, a new index version, the time limit.
- [ ] Give the trade-off: saved cost and speed against wrong or stale answers.
- [ ] Say how you would measure the hit rate and the wrong matches.

**Common traps**

- Trap: "Key the cache on the question text." Better: That leaks private answers and goes stale. Add the user's groups, the index version and the model to the key.
- Trap: "A semantic cache is always safe, it only returns similar answers." Better: A near match can still be a different clause. Tune the threshold on real questions and measure the wrong matches.
- Trap: "Caching makes the answers correct." Better: Caching saves time and money. It reuses whatever was answered, so a wrong answer stays wrong until it is cleared.

## Can you spot the unsafe cache?

**Spot the unsafe key:** Which cache key is safe for an assistant that searches both HR and sales documents?

A. key = hash(question)
B. key = hash(question + index_version)
C. key = hash(question + user_groups + index_version + model_name)
D. key = hash(question + model_name)

**Answer:** C. Only the group-aware key keeps each team's answers apart and drops them when a document is re-indexed. The other keys either leak answers across teams or go stale.

**Quick check:** A cached answer is served after its source policy changed. What fixes it?

A. A longer time limit
B. Clear the entries that cite that document when it changes
C. A bigger embedding model
D. A lower temperature

**Answer:** B. The fix is a clear-out rule tied to the source. A longer time limit keeps stale answers longer, and model size or temperature do not change what the cache holds.

## Can you answer these from memory?

**Flash cards**

- **What is a RAG cache?** Saved work from a RAG step, reused when the same or a near-identical request comes back.
- **Which cache is the riskiest?** The semantic answer cache. A close match can be a different clause, so its threshold must be strict.
- **What goes in the cache key?** The question, the user's access groups, the index version and the model.
- **Where do retrieved chunks go in the prompt?** After the fixed start. Put them first and the provider's cache breaks on every new question.
- **What clears a stale entry?** A change to a source clears the entries that cite it. Every entry also expires after its time limit.
- **Does caching make answers correct?** No. It saves time and money, and reuses whatever was answered, right or wrong, until it is cleared.

## Likely follow-up questions

- Q139 How do you keep the index fresh? https://designervenkat.online/interview/ai/how-do-you-keep-the-index-fresh
- Q140 How do you enforce access control in RAG? https://designervenkat.online/interview/ai/how-do-you-enforce-access-control-in-rag
- Q146 How do you pick top-k? https://designervenkat.online/interview/ai/how-do-you-pick-top-k
- Q147 When must you re-embed everything? https://designervenkat.online/interview/ai/when-must-you-re-embed-everything
- Q150 How would you design RAG for 10 million documents? https://designervenkat.online/interview/ai/how-would-you-design-rag-for-10-million-documents
- Q138 RAG vs long context: which to use? https://designervenkat.online/interview/ai/rag-vs-long-context

## Learn it in depth

- [Prompt Caching](https://designervenkat.online/learn/ai-engineering/prompt-caching)
