Topic 3 of 5 · 5 min read
Semantic caching by embedding similarity
Goal: Choose a similarity threshold from labelled query pairs and estimate the false-hit rate it allows.
A semantic cache turns each query into an embedding, finds the closest stored query, and returns its answer if the similarity passes a threshold. The threshold decides how many wrong answers you serve, so tune it on labelled pairs, not by feel.
Check yourself · 1 of 3
What does raising a semantic cache's similarity threshold do?
Why it matters
Users rarely repeat a question word for word. "How do I cancel?" and "cancel my subscription" would miss an exact cache. A semantic cache catches those. The paper on GPT Semantic Cache reports hit rates of 61.6% to 68.8% on repetitive customer service queries, with positive hit rates above 97%. That is one test on one kind of traffic; your numbers depend on how repetitive your queries are.
How it works
- Embed the incoming query with an embedding model.
- Search the stored query embeddings for the nearest neighbour (tools such as RedisVL and GPTCache do this).
- If similarity is at or above the threshold, return the stored answer. Otherwise call the model and store the new pair.
Apply the same scoping as an exact cache: filter by tenant, language, model and prompt version before the vector search. A near match from another tenant is still a leak.
Tuning the threshold
Collect 200 or more real query pairs and label each one: same intent (the answer would be identical) or different intent. Score every pair, then try thresholds:
| Threshold | Same-intent pairs hit | Different-intent pairs hit (false hits) |
|---|---|---|
| 0.80 | 96% | 18% |
| 0.90 | 81% | 4% |
| 0.95 | 52% | 0.5% |
At 0.90, 4 in 100 look-alike queries get the wrong answer. If 10% of live traffic is look-alike-but-different, roughly 0.4% of all requests are wrong, silently. Pick the point where that false-hit rate is acceptable for the route, and use a stricter one for anything involving money or policy.
What goes wrong
- Close in meaning, different in answer. "Refund for order 4471" and "refund for order 4472" embed almost identically. Numbers, names and negations ("do" versus "do not") are weak spots, so keep identifiers out of semantic matching or in the filter.
- A threshold copied from a blog post. Scores depend on the embedding model; change the model and re-tune.
- No labelled set. Without pairs you cannot compute a false-hit rate, only a hit rate, which always looks good.
Examples
Reading the threshold table
A team labels 300 pairs. At 0.90 similarity, 81% of same-intent pairs hit and 4% of different-intent pairs also hit. The FAQ route accepts that. The billing route cannot afford 4% wrong answers, so it uses 0.95 and a smaller hit rate.
A near-identical wrong hit
stored: "Can I get a refund if I cancel within 14 days?"
incoming: "Can I get a refund if I cancel after 14 days?"
similarity: 0.97
The two differ by one word and have opposite answers. A threshold alone will not catch this; route policy questions to the model or add a rule for numbers and time words.
Key points
- A semantic cache returns the answer of the nearest stored query when similarity passes a threshold.
- Tune the threshold on labelled same-intent and different-intent pairs from real traffic.
- A higher threshold lowers false hits and also lowers the hit rate; choose per route by cost of a wrong answer.
- Filter by tenant, language, model and prompt version before the similarity search.
- Embedding scores are model-specific, so re-tune whenever the embedding model changes.
Common mistakes
- Reporting hit rate without measuring false hits.
- Using one global threshold for FAQ and billing questions.
- Skipping tenant or language filters before the vector search.
- Keeping the threshold after switching to a new embedding model.
Check yourself · 2 of 3
How do you find out how many wrong answers a threshold of 0.90 would serve?
Check yourself · 3 of 3
Two queries, "refund for order 4471" and "refund for order 4472", score 0.99. What should the cache design do?
Practice items, not counted towards your mastery.
Start here
- Prompt caching (Claude docs) (opens in a new tab)DocsFree
How a cached prefix is marked, how long it lives and how cache read and write tokens are reported.
- Prompt caching (OpenAI API docs) (opens in a new tab)DocsFree
Automatic prefix caching on another provider, with the rules for what counts as a repeated prefix.
- RedisVL: semantic caching for LLMs (opens in a new tab)DocsFree
A working semantic cache: distance threshold, time to live and filters for scoping entries.
More for this topic
- GPT Semantic Cache: reducing LLM costs and latency via semantic embedding caching (opens in a new tab)PaperFree
Reports a 61.6% to 68.8% hit rate and over 97% positive hits on repetitive support queries.
- GPTCache (Zilliz) (opens in a new tab)ToolFree
An open-source semantic cache for LLM calls; read how it splits embedding, storage and similarity evaluation.