Semantic cache: hit vs miss
Answer repeated questions without calling the model, and learn why a cache that matches on meaning can return the wrong answer.
Sign in to save this lessonYou will learn to
- Trace the hit path and the miss path through a semantic cache.
- Explain how a similarity threshold trades hit rate for wrong answers.
- Decide when a semantic cache is worth adding.
Before you start: The life of an LLM request. A rough idea of what an embedding is.
Think of it like
A help desk with a binder of past answers. If someone asks a question that means the same as one in the binder, the clerk reads out the old answer instead of calling the expert. Fast and cheap, unless the clerk decides two different questions are the same.
Step 1 of 6
A question arrives
An exact-match cache keyed on the raw text would miss "how can I reset my password" because the strings differ. A semantic cache matches on meaning instead.
Try it: Cache hit versus miss
See how hit rate and wrong-hit rate change latency, cost and correctness. All defaults are illustrative; measure your own traffic before relying on any of these numbers.
Questions arriving per second.
Share of questions answered from the cache. Lower thresholds raise it.
Hits that returned an answer to a different question. Lower thresholds raise it too.
Embedding the question plus the vector search.
Full time for the model to answer on a miss.
- Latency on a hit
- 25 ms
- lookup = 25 ms
- Latency on a miss
- 3,025 ms
- 25 + 3000 = 3025 ms
- Average latency
- 2,125 ms
- 30% × 25 + 70% × 3025 = 2125 ms
- Model calls
- 35 per second
- 50 × (1 − 30%)
- Model cost saved
- 30 %
- equal to the hit rate
- Wrong answers
- 6 per 1000 questions
- 1000 × 30% × 2%
Assumptions
- Every request pays for the lookup; misses also pay the full model time.
- Hit rate and wrong-hit rate are inputs you would measure on your own traffic, not predictions.
- Model cost is proportional to the number of model calls.
Key takeaways
- A semantic cache matches on meaning, so it catches rephrased questions that an exact cache misses.
- Hits skip the model entirely; misses pay for the lookup and the model call.
- The similarity threshold trades hit rate against wrong answers: tune it on real data.
- It pays off for repetitive traffic (FAQs, support) and rarely for open-ended conversation.
The parts
- Client:
- Sends a question.
- App server:
- Runs the cache-aside logic: check the cache, fall back to the model.
- Embedder:
- Turns the question into a vector that captures its meaning.
- Semantic cache:
- Stores past questions as vectors next to their answers and finds the closest match.
- LLM:
- Generates a fresh answer. Slow and paid per token.
Check your understanding
Finish every step and answer the checks to complete this lesson.