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.
11 min readIntermediateOccasional · estimateFacts checked 27 November 2026
Say it out loud
Read it once, then say it without looking. Start with the short one; use the long one when they ask you to go deeper.
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.
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.
Key words
- 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.
Three caches and the answer, in order
User question
Arrives in the chat box
1. Answer cache
Hit: answer now. Miss: keep going
2. Search cache
Same query, rights, index version?
3. Prompt prefix
Fixed start reused by provider
Answer with sources
Stored under its keys
- 1The question goes to the answer cache first.
- 2On a miss, the search cache is checked before any search runs.
- 3The chunks go to the model after its fixed prompt start.
- 4The 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.
Two cache keys, two outcomes
Key on question only
Question text only
Ignores who is asking
Key with rights and version
Question, rights, version
Filed per group and version
Private answer leaks
Sales sees the HR answer
Safe, current answer
Only allowed groups, latest
- 1A sales rep asks, and the cache hands over HR's answer.
- 2Each 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."
BetterThat 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."
BetterA 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."
BetterCaching 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?
Quick check
A cached answer is served after its source policy changed. What fixes it?
Can you answer these from memory?
Flash cards · tap a card to flip it
What will they ask next?
Learn it in depth
Last updated on 27 November 2026
Running a RAG system that is slow or costly at scale?
I design and build AI products and agents, the UI and the system behind them. Book a short call and tell me how many questions a day you get and how often your documents change.