A semantic cache reuses a previous LLM answer when a new question is “close enough” to one it has already seen. I measured how often “close enough” returns the answer to a different question. The short version: in the configurations I tested, tuning the similarity threshold could not make the cache safe without giving up most of its savings, and the second fix, an LLM judge, looked perfect on my own test set and much weaker on real questions.
Why this matters
Semantic caching is one of the standard patterns in an LLM gateway, next to model routing and confidence thresholds. The appeal is obvious: a cache hit costs no model call. The failure mode is less obvious. A cache miss costs money; a false hit returns a confident, well-written answer to a question nobody asked. In a domain like clinical trials, “what are the inclusion criteria?” and “what are the exclusion criteria?” should never share an answer.
My hypothesis was that questions on the same topic with a different meaning sit as close together in embedding space as true paraphrases do. If that holds, no threshold can tell them apart.
Experiment 1: the threshold alone
I wrote 56 question pairs about clinical trials: 18 paraphrases that should share an answer, 30 “hard negatives” that change one thing (an antonym, a negation, an entity, a number, the scope, or who acts on whom), and 8 unrelated pairs. I embedded them with nomic-embed-text and swept the threshold from 0.50 to 0.99.
The hypothesis was too optimistic. Hard negatives were not as close as paraphrases; they were closer. Paraphrases averaged 0.737 cosine similarity; every hard-negative category averaged between 0.81 and 0.97. “Does the sponsor pay the participant?” and “Does the participant pay the sponsor?” scored 0.993.
That turns the threshold into a trap. Raising it removes good hits before it removes bad ones. The lowest threshold that blocked every hard negative was above 0.99 in all three embedding configurations, and at that point the cache reused 0% of paraphrases. This is not a calibration problem; it is a property of what embeddings capture. They are good at topic and weak at the small details that change an answer.
It is not a property of one model either. I repeated the sweep with mxbai-embed-large and with the multilingual bge-m3. In both, every hard-negative category averaged higher than the paraphrases (0.761 to 0.961 against 0.744; 0.808 to 0.968 against 0.775), and the threshold that blocked every hard negative reused no paraphrase.
Experiment 2: adding an LLM judge
The natural next step is to let the threshold propose a candidate and ask a small local model whether one answer fits both questions. At a permissive threshold of 0.60, qwen2.5:7b kept every paraphrase and rejected all 30 hard negatives with a guided prompt (29 of 30 with a plain one).
Two results from this round shaped how I evaluate everything after it. First, llama3.2:3b answered “different” to every pair: zero wrong answers and zero savings. Any evaluation that reports only safety would have called it perfect, so savings and errors always have to be reported together. Second, 0 errors in 30 hard negatives still allows a true error rate of about 10% at 95% confidence. A clean result on a small set is a signal, not a guarantee.
I also asked each judge twice, with the two questions swapped. A judge that decides on meaning should not care about the order. The 3B judge changed its answer on 4 of the 56 pairs, all paraphrases. The 7B judge changed on 1, and that pair was its one miss: requiring both orders to agree removed it, at twice the calls. The cost is easy to state as a break-even. At this threshold the 7B judge only saved time when the answer it replaced took more than about 4.7 to 5.0 seconds to generate, or 9.9 seconds when asked in both orders.
Track A: real questions against a whole cache
My pairs were written by changing one word, which is exactly the kind of difference an LLM spots easily. So I reran the idea on real patient questions from the Medical Question Pairs dataset (McCreery et al., KDD 2020): 1,524 questions, each with a doctor-written rewrite that keeps the intent and a related question with similar keywords and a different answer. All 1,524 originals went into the cache, and 150 rewrites plus 150 related questions searched the entire cache, as they would in production.
Three things changed:
- Real questions separate better, but not enough. At a threshold of 0.75, the cache still served a wrong answer to 42% of related questions. Getting that below 1% took a threshold of 0.907, which reused only 29.5% of rewrites.
- A whole cache adds errors of its own. Even with no tricky traffic, 3.3% of rewrites matched a different cached question at thresholds up to 0.70. Testing isolated pairs hides this entirely.
- The judge was much weaker.
qwen2.5:3blet through 7.3% of related questions (versus 0 of 30 in Experiment 2) and rejected a third of valid rewrites. A larger judge,qwen2.5:7b, was more lenient rather than more accurate, at 9.2 seconds per query on CPU (p95 13.9 seconds).
The useful way to compare policies turned out to be wrong answers and answers saved per 1,000 queries that have a close match in the cache, for different shares of related questions in the traffic:
| Policy | 0% related | 10% related | 25% related | 50% related |
|---|---|---|---|---|
| threshold 0.80 | 13 / 773 | 28 / 696 | 50 / 580 | 87 / 387 |
| threshold 0.85 | 0 / 540 | 5 / 486 | 13 / 405 | 27 / 270 |
| 0.60 + qwen2.5:3b judge | 0 / 640 | 7 / 576 | 18 / 480 | 37 / 320 |
| 0.60 + qwen2.5:7b judge | 13 / 860 | 28 / 774 | 50 / 645 | 87 / 430 |
The 3B judge saves more than a 0.85 threshold but also serves more wrong answers, and it adds about 2.2 seconds per query on CPU. It moves along the same trade-off curve instead of escaping it. The honest conclusion is that Experiment 2 overstated the judge: hand-written, one-word hard negatives are easy; real near-misses are not.
The same table reads differently as the share of served cache hits that are wrong, which is what a user actually experiences. With 25% related traffic, that is 3.1% for the 0.85 threshold, 3.6% for the 3B judge, and 7.9% and 7.2% for the 0.80 threshold and the 7B judge. At 50% related traffic, every policy serves a wrong answer on roughly one hit in ten or more.
One caveat applies to every Track A number. The dataset labels each question only against its own original, so a hit on a different cached question is counted as wrong without a label saying so. The wrong-answer figures are therefore an upper bound and the answers saved a lower bound. Those hits are now exported for labeling by hand, and this article will be updated with the corrected figures.
A model trained for the task did not help
The obvious objection to an LLM judge is that a model trained to decide whether two questions are equivalent should do this better and far cheaper. I tested one: a cross-encoder trained on duplicate questions (cross-encoder/quora-distilroberta-base). On my hand-written pairs it was worse than any judge that worked: at a score threshold of 0.50 it reused 17% of paraphrases and still accepted 10% of hard negatives. On the real questions, compared pair by pair with the plain similarity threshold at the same share of related questions accepted, it reused fewer rewrites:
| Policy | Related accepted | Rewrites reused |
|---|---|---|
| similarity threshold 0.85 | 5.7% | 61.0% |
| cross-encoder 0.50 | 5.7% | 49.9% |
| similarity threshold 0.88 | 2.3% | 44.4% |
| cross-encoder 0.95 | 2.2% | 31.6% |
It is cheap, about 46 milliseconds per pair on CPU, but it sits below the threshold it was meant to improve on. This is one model, trained on general questions and used on medical ones, so it does not show that no such model would work. It shows that “use a model trained for equivalence” is not an automatic fix, and that it has to be measured on your own questions like everything else here.
What I would do when designing a semantic cache
- Start from a wrong-answer budget, not a threshold. Decide how many wrong answers per 1,000 queries the product can tolerate, then pick the policy that saves the most within that budget. A default like 0.75 says nothing about either number.
- Evaluate against the whole cache with real near-misses. Pair tests miss neighbor collisions, and hand-written negatives flatter any judge.
- Report savings and errors side by side, as a function of the traffic mix, because the right policy changes with how often related-but-different questions arrive. Only production traffic can answer that.
- Fail closed. Use embeddings to retrieve candidates, not to decide that two questions are equivalent, and when the decision is unclear, call the model.
- Equivalent questions are not enough. A cached answer can be stale, or belong to a user with different permissions, a different document version or a different conversation. None of that is visible to an embedding; it has to be part of the cache key or the reuse decision. This experiment does not test it.
- Treat a judge as a trade-off, not a fix. It costs latency on most hits and, with small models, does not clearly beat a stricter threshold. Whether it pays off depends on how expensive the answer it saves is.
Limitations
One domain, one language, short questions, and small samples: 5 pairs per hard-negative category in Experiments 1 and 2, and 150 + 150 queries in Track A. Labels are human judgment. Both orders were judged on the hand-written pairs only; Track A still judges one. Frontier models may judge better, at a much higher price per check.
Judges ran on free CPU runners, and the latency there is not stable: the same 3B judge took 1.7 seconds per call in one run and 0.9 in the next. Read the latencies and break-even times as orders of magnitude. Latency on a GPU would be much lower and was not measured. One verdict of that judge also changed between those two runs, at temperature 0, which is a reminder that a result on a small set can move by a pair.
The cross-encoder was tested pair by pair, not against the whole cache, and only one was tested. A cross-encoder trained on questions from the same domain, and a bidirectional entailment check, are still untested.
Code, data and raw results are on GitHub, and every number above comes from runs committed there by GitHub Actions.