Semantic caching for RAG: how the similarity threshold trades cost against wrong answers
A semantic cache returns a stored answer when a new question looks similar enough to an old one, skipping retrieval and the LLM call. The single number that decides whether it saves money or embarrasses you is the similarity threshold. Here you set it on thirty real-looking question pairs.
Interactive simulation. It needs JavaScript; the text below explains what it shows.
What you are looking at
Each row is a question arriving at a RAG chatbot. Before running retrieval and the LLM, the system embeds the question and looks up the closest question already in the cache. The column sim is the cosine similarity between the two; the column same says whether they really ask the same thing, so that the cached answer is right (1) or wrong (0). The pairs are modelled on the Quora Question Pairs data, where over 400,000 question pairs are labelled as duplicates or not, which is the standard way to test whether "similar" means "the same question".
On the chart every dot is a question placed at its similarity, at the top if the cached answer fits and at the bottom if it does not. The step line is the cache's decision: everything right of the threshold is served from the cache. The markers at 1 show the wrong answers that were served. The bars show what each answer cost: a miss pays for a fresh RAG answer, a wrong hit pays the penalty you set, and a correct hit costs only the lookup.
The algorithm
- Embed and search. The question is turned into a vector and the nearest cached question is found, usually in the same kind of vector index the RAG retriever uses. Every query pays for this, hit or miss.
- Threshold. If the cosine similarity is at least τ the cached answer is returned. GPTCache expresses this as a similarity threshold between 0 and 1 (its Config default is 0.8); RedisVL's SemanticCache uses a cosine distance threshold between 0 and 2 with a default of 0.1, which is the same as a similarity of at least 0.90.
- Count the outcomes. A hit can be correct or wrong (a false positive); a miss can be necessary or a missed reuse (a false negative). GPTCache reports the hit ratio and the recall, the share of the queries that should have been served by the cache that actually were. Precision, the share of hits that were right, is the number that decides how often users see an answer to someone else's question.
- Price it. The cost of one answer is the lookup, plus the RAG cost if it missed, plus a penalty if a wrong answer was served. The mean over all queries is compared with running every question through RAG.
Worked example
With the defaults (τ = 0.90, a fresh answer at 1.65 ¢, a lookup at 0.01 ¢, a wrong answer charged 2 ¢) eleven of the thirty questions are served from the cache, a hit ratio of 36.7%. Eight of those are right and three are not (similarities 0.948, 0.921 and 0.902 belong to different questions), so precision is 72.7%. Fourteen questions could have been reused and eight were, a recall of 57.1%. The bill is 30 × 0.01 + 19 × 1.65 + 3 × 2 = 37.65 ¢, or 1.255 ¢ per answer, against 1.65 ¢ with no cache: a saving of 23.9%. A perfect cache would cost 0.89 ¢.
Things to try
- Lower the threshold to 0.77. Every reusable question is now served (recall 100%) and the cost falls to 1.018 ¢, a 38.3% saving, but 11 of the 25 hits are wrong answers.
- Keep τ at 0.90 and raise the cost of a wrong answer to 10 ¢. The cache now costs 2.055 ¢ per answer, 24.5% more than having no cache at all.
- At a wrong-answer cost of 5 ¢, try 0.95: only three hits, all correct, and 1.495 ¢ per answer, cheaper than the 1.555 ¢ that 0.90 costs at the same penalty.
- Drop the RAG cost to 0.3 ¢, as for a small model with short context. Even at τ = 0.90 the cache now loses money: 0.40 ¢ against 0.30 ¢.
- Paste pairs from your own logs and look for the similarity where the dots at 0 stop: that is where your threshold belongs.
How it works in real systems
The lesson of the simulation is that the threshold is a business decision, not a constant: the best value depends on how expensive a fresh answer is and how much a wrong one hurts. Similarity scores are also model-specific, so a threshold tuned for one embedding model does not carry over to another.
Production caches add safeguards. They scope entries with filters (user, tenant, language, document version) so that an answer about one customer's account is never returned to another; they give entries a time to live, because a cached RAG answer goes stale when the indexed documents change; and some re-check a candidate hit with a second model, as GPTCache does with an optional duplicate-question classifier, before serving it. Caching sits next to the other cost levers of a RAG system: fewer, better chunks from hybrid search and reranking, and provider-side prompt caching, which discounts repeated prompt prefixes but still calls the model. Measure the answers a cache serves with the same checks as fresh ones, for example RAGAS faithfulness.