Semantic Caching

14 min read

Many users ask the same question in different words. A semantic cache sees that "When do you open?" and "What are your opening hours?" mean the same thing. It then returns a saved answer and does not call the LLM (the AI model behind a chatbot) at all.

New words

Semantic cache
A store of saved answers that matches new questions by meaning.
Embedding
A list of numbers that stands for the meaning of a text.
Cosine similarity
A score where 1.0 means the same meaning and 0 means unrelated.
Threshold
The lowest similarity score at which you accept a saved answer.
Hit and miss
A hit reuses a saved answer, and a miss has to call the LLM.
False hit
A wrong match: the cache returns an answer that does not fit.
TTL
Time to live: how long a saved answer lasts before it expires.

What you will learn

  1. The problem: same question, different words
  2. How does a semantic cache work?
  3. Three kinds of caching, compared
  4. Try it: find the right threshold
  5. Where does a semantic cache go wrong?
  6. How to make it safe in production
  7. In code
  8. In a real system
  9. Interview tip
  10. Key takeaways

10 quiz questions at the end.

Start this lesson

The full lesson, with interactive demos and the quiz, opens after you sign in with Google. It is free.

All lessons · Back to the roadmap