Semantic Caching
Cache answers for near-duplicate queries using embedding similarity thresholds.
In plain words
If someone asks nearly the same thing as before, the system reuses the saved answer instead of asking the AI again, which is faster and cheaper, as long as the questions really mean the same.
Think of it like…
A help-desk binder of previous tickets: a clerk who finds a near-identical past case answers instantly, but if "cancel order 1234" is matched to "cancel order 1243", the clerk cancels the wrong order.
Where you’ve seen it
A support assistant gets 100,000 requests a day, and each model call costs about $0.01195 and takes ~3.4 s. The team adds a semantic cache scoped by tenant, language and prompt version, with the query embedded at about $0.0001 per request (embedding plus vector lookup, 15 ms). On labelled pairs they tune the threshold: at cosine 0.92 the hit rate is 38% but 3.5% of hits are wrong (1,330 wrong answers a day); at 0.97 the hit rate is 18% with 0.2% wrong (36 a day); at 0.95 it is 30% with 1.0% wrong (300 a day). They choose 0.95 for FAQ-type intents only, add an LLM verifier for refund or account questions, and exclude personalised queries. Net saving at 0.95: 30,000 hits × $0.01195 = $358.50 a day, minus $10 of lookup cost, about $348.50 a day (roughly $10,500 a month), plus 3.4 s saved on each hit.
Words you’ll hear
- Similarity threshold
- The minimum cosine similarity between a new query and a cached one to count as a hit.
- Hit rate / false-hit rate
- Share of requests served from cache, and share of those hits that are wrong.
- Cache scope / key
- Attributes (tenant, language, prompt version, model) that must match for a hit.
- TTL and invalidation
- Expiry and explicit removal of cached answers when facts change.
Visual diagram
Theory & deep dive
How it works and how to scope it
A semantic cache stores entries of the form (query embedding, normalised query, answer, metadata, timestamp). On a request, normalise (trim, lowercase, strip stop-noise), compute an embedding, run a nearest-neighbour search restricted by scope (tenant, language, prompt version, model, and any feature flags), and compare the best similarity with a threshold. Layer it after a cheap exact-match cache (hash of the normalised prompt), which has zero false hits. Store with a TTL matched to how fast the answer changes (hours for pricing, days for stable docs), and tag entries with the source documents so that updating a document invalidates the related entries.
Decide what may be cached. Good candidates: public, non-personalised, stable FAQs and documentation answers. Bad candidates: answers that depend on user identity, session history, real-time data (order status, balances), permissions or non-deterministic creative outputs. Never share entries across tenants or permission levels, because a cache hit can leak one customer's data to another. Include the conversation context in the key for follow-ups ("and for international orders?" means nothing alone), or cache only the first turn.
"Cancel order 1234" and "cancel order 1243" have embedding similarity above 0.97 because the text is nearly identical, yet the correct actions differ. Intents with identifiers or personal data should bypass the semantic cache or require an exact match on the extracted entities.
Tuning, verifying and measuring
Tune the threshold on labelled query pairs (same intent and answer versus different) sampled from real traffic: plot hit rate against false-hit rate and choose a point per intent group, because the right threshold differs (0.92 may be fine for "opening hours" and dangerous for "refund eligibility"). Embedding models matter: a model tuned for retrieval may rate topic similarity, not answer equivalence; consider a cross-encoder or small LLM verifier on borderline hits (similarity between 0.90 and 0.97), or a lightweight check that extracted entities (numbers, product names, dates) match.
Measure hit rate, false-hit rate, latency saved, cost saved and the cost of the cache itself (embedding calls, vector database, verifier). Savings = hits × cost of a model call − lookup cost for all requests − verifier cost; with 30,000 hits × $0.01195 = $358.50 against $10 of lookups the net is about $348.50 a day. Audit a random sample of hits with a judge or humans every week, watch for drift when documents or prompts change (version the cache), and give users a path to bypass it (a "regenerate" action). A cache is also a security surface: poisoned entries (a malicious answer stored and served to others) are a real risk, so restrict who can write and validate what is stored.
Threshold 0.92: 38% hits, 3.5% wrong (1,330 wrong answers a day). 0.95: 30% hits, 1.0% wrong (300). 0.97: 18% hits, 0.2% wrong (36). Each step up costs savings but removes wrong answers, so the threshold should follow the risk of the intent.
How it works, step by step
Step 1 of 41. Choose cacheable intents
Identify public, stable, non-personalised intents; exclude identity-, session- and real-time-dependent ones.
- Scope
- tenant + language + version
When to use it & trade-offs
What it is
Semantic caching stores past question-and-answer pairs and returns a stored answer when a new query is close enough in meaning, instead of calling the model again. Each query is embedded, a vector search finds the nearest cached query, and if the cosine similarity exceeds a threshold (for example 0.95) the cached answer is served. It extends exact-match caching to paraphrases ("how do I reset my password" and "I forgot my password, what now?"), saving the full cost and latency of a model call on every hit. Its central risk is a false hit: returning a cached answer to a query that looks similar but needs a different answer.
Why it matters: Many production workloads (FAQ bots, support, search assistants) get the same questions in many phrasings. A well-tuned semantic cache can cut spend and latency dramatically: a hit returns in tens of milliseconds instead of seconds. But a poorly tuned cache is worse than none, serving wrong or stale or someone else's answers with confidence, so the threshold, the scope of what may be cached and the evaluation of false hits are the engineering substance.
When to reach for it
- FAQ, help-centre and documentation assistants
- High-volume repeated questions
- Cutting latency for common intents
When to avoid or be careful
- Caching personalised or real-time answers
- Sharing entries across tenants
- Choosing a threshold without labelled pairs
Caching layers
Strengths: No false hits; trivial to build.
Watch out: Low hit rate for natural language.
Strengths: High hit rate on FAQ-like traffic.
Watch out: False hits and staleness risk; embedding cost.
Strengths: Cuts input cost and TTFT with no answer reuse.
Watch out: Still pays for generation.
Interview signals & numbers to know
When this topic comes up
- “Users ask the same question in many ways”
- “Can we cache LLM answers?”
- Similarity threshold
- Cache poisoning
- False-hit rate
Benchmarks & rules of thumb
- 0.95
- example cosine threshold: 30% hits, 1.0% wrong
- ~$348.50/day
- net saving from 30,000 hits at $0.01195 less $10 lookup cost
- 15 ms vs 3.4 s
- cache hit vs model call latency
Common mistakes & follow-ups
Common interview mistakes
- Serving a cached answer for "order 1243" to a question about order 1234.
- Optimising hit rate without measuring false hits.
- Forgetting to invalidate entries when the source document changes.
Likely interviewer follow-ups
- How would you choose the similarity threshold?
- What shouldn't be cached?
- How can semantic caches create security problems?
Knowledge check
Put it into practice
Practice related interview Q&A
Apply Semantic Caching with generative AI interview Q&A and hands-on practice.