← All questions
LLMOps & ProductionMedium· Production & Optimization

How does semantic caching work in RAG, and what are its...

A standard cache needs an exact text match to hit; semantic caching instead embeds the incoming query and checks cosine similarity against previously cached queries, returning the stored response whenever similarity clears a threshold — commonly somewhere around 0.92. That turns “how do I reset my password” and “I forgot my password, help” into the same cache entry even though they’re not identical strings.

The hit rate varies enormously by use case: customer support with lots of repeat questions can see well over a third of queries served from cache at effectively zero cost, while open-ended research or document-analysis queries see far fewer repeats and correspondingly lower hit rates.

The threshold is the whole game, and it cuts both ways — set it too high and the cache barely fires, set it too low and subtly different questions start getting served the wrong cached answer. Two other limits are non-negotiable: cached entries need a TTL, because a knowledge base update can make a cached answer wrong without anything about the query changing, and anything personalized — “what’s my account balance” — must be excluded from caching entirely regardless of how similar the phrasing looks.

Code playground (Python)