A semantic cache is keyed on query embeddings. If a new query is close enough in meaning to a past one, the system returns the stored response and skips the model call.

Semantic CachingDiagram with these labels, in reading order: If Similarity > Threshold; If No Close Match; Incoming Query; Embed Query; Vector Search Cache; Cached Response; Call Model & Store.If Similarity > ThresholdIf No Close MatchIncoming QueryEmbed QueryVector Search CacheCached ResponseCall Model & Store
Flow of Semantic Caching Process

The lookup sequence

  1. Embed the incoming query with a model such as Sentence-BERT or OpenAI's embedding API.
  2. Run a vector search against the cached queries.
  3. Compare the nearest hit's similarity score with a threshold.
  4. If it passes, return the cached answer.
  5. If it does not, call the model and store the new query and response pair.

Tools such as GPTCache implement this in front of an LLM. The method depends on two choices: the embedding model and the similarity threshold. Suppose a user asks "What are the store hours?" If "When does the store open?" is cached and meets the threshold, the stored answer is served, which saves latency and model load.

Questions about similarity thresholds

What happens if the threshold is too high? Valid cached answers are missed and the model is called unnecessarily. The hit rate drops.

What happens if it is too low? The cache can serve an answer that is subtly wrong or not quite relevant. That can mislead users and damage trust, so tune the threshold carefully.

How does this differ from Memcache or Redis in front of an API? Those caches match on an exact key. In a semantic cache the key is fuzzy: "reset my password" and "how do I change my password" differ in wording but share intent, and they collide on purpose. Correctness becomes probabilistic, which an exact-string cache never has to worry about.

Why not use exact-string caching? With free-text queries, users phrase the same question in many ways, so exact matching hits rarely. Semantic matching tolerates wording changes, cutting latency and token use.

Threshold tuning Set the similarity threshold too low and the cache serves incorrect responses.

The choice between exact and semantic keys comes down to how your users write. If they type the same string, exact matching is enough. If they paraphrase, an embedding lookup finds earlier answers that normalization alone would miss.

Workloads that fit a semantic cache

  • Fits: high-volume, repetitive and factual queries, such as FAQ bots and customer support.
  • Risky: personalized, time-sensitive or user-context-specific queries. Scope the cache narrowly or skip it.
  • Skip: queries that need real-time updates. Call the model directly to avoid outdated answers.

When the semantic cache hit rate falls

Example scenario: the hit rate drops from 75% to 45%. These numbers are illustrative.

Semantic CachingDiagram with these labels, in reading order: Cache Hit Rate; Low Embedding Similarity; Outdated Embedding Model; Inefficient Query; Preprocessing; Update Embedding Model; Optimize Preprocessing; Steps.Cache Hit RateLow Embedding SimilarityOutdated Embedding ModelInefficient QueryPreprocessingUpdate Embedding ModelOptimize PreprocessingSteps
Debugging semantic caching issue

A threshold set too strict is one likely cause. Similar queries then fail to match, and the model is called for them anyway.

  • Measure hit rate and response times over a week.
  • Log the similarity scores of queries against cached entries.
  • A/B test different thresholds to find a good setting.

Adjusting the threshold balances hits against accuracy. Holding more cached entries can raise memory use. Track hit rate and average response time to confirm the change.

Back to the knowledge baseRequest an assessment