Articles

What Is Semantic Caching for LLM Applications?

Semantic caching reuses an LLM's past response for a new prompt that means the same thing, by comparing embeddings instead of exact text.

Chisato Chisato · · 4 min read
Abstract purple neural network fibers

Semantic caching stores LLM responses keyed by the meaning of a prompt rather than its exact text, so that “what’s the capital of France” and “tell me France’s capital city” can hit the same cached answer even though the two strings never match character-for-character. It’s a way to skip an expensive model call when a functionally identical question has already been asked, even if it was phrased differently.

Why exact-match caching falls short

A traditional cache — a hash map keyed by the literal request string — only helps when the same input recurs verbatim. LLM inputs are natural language, and natural language rarely repeats exactly: users rephrase questions, add or drop pleasantries, reorder clauses, or make small typos. A support chatbot might see “how do I reset my password” and “password reset steps?” as effectively the same request, but a literal cache treats them as two unrelated keys and calls the model twice.

Given how LLM pricing typically works — you pay per token processed — every avoidable call is a direct cost saving, plus a latency win: a cache hit returns in milliseconds instead of waiting on a model’s generation time.

How it works

  1. Embed the incoming prompt. Convert the prompt into a vector using an embedding model, the same technique described in what vector embeddings are.
  2. Search for a similar cached vector. Compare the new embedding against previously cached prompt embeddings, usually with cosine similarity, often backed by a vector database or an in-memory index for smaller caches.
  3. Apply a similarity threshold. If the closest cached entry scores above a chosen threshold (say, 0.92 cosine similarity), the cached response is returned directly. Below the threshold, the request goes to the model as normal, and the new prompt-response pair is added to the cache.

This is architecturally similar to retrieval-augmented generation, which also embeds a query and searches a vector index — the difference is that semantic caching retrieves a previous answer to potentially return verbatim, while RAG retrieves source documents to feed into a fresh generation.

Picking a similarity threshold

The threshold is the whole tradeoff in one number:

  • Too loose, and semantically different prompts get treated as equivalent — “cancel my subscription” and “cancel my order” might sit close enough in embedding space to collide, returning the wrong cached answer entirely.
  • Too strict, and the cache rarely hits anything beyond near-exact duplicates, undermining the reason to build it.

Most production systems tune the threshold empirically against a labeled set of prompt pairs that should and shouldn’t match, and re-check it whenever the embedding model changes, since similarity scores aren’t portable across different embedding models.

Cache invalidation

Semantic caches face the same invalidation problem every cache does, with an added wrinkle: the thing that makes a cached answer stale isn’t always visible in the prompt’s wording. If the underlying source of truth changes — a documentation page is updated, a policy changes, a product is discontinued — every cached response derived from the old information is now wrong, even though the prompts that would trigger a cache hit haven’t changed at all. Time-to-live expiry on cache entries is the simplest mitigation: force a fresh model call after a fixed window regardless of hit rate, trading some redundant calls for a bound on how stale a cached answer can get.

A more targeted approach ties cache invalidation to the underlying data: when a document a response was derived from changes, explicitly evict cache entries associated with it, similar in spirit to how change data capture propagates updates from a source system rather than waiting for a blind expiry window.

Measuring whether it’s working

The two numbers that matter are hit rate and precision. Hit rate — the fraction of requests served from cache — is straightforward to track and is the number most dashboards surface first, since it converts directly into cost and latency savings. Precision is harder and easy to neglect: it’s the fraction of cache hits that were actually correct to serve, as opposed to false positives where a prompt was similar enough to clear the similarity threshold but different enough in meaning to warrant a different answer. A cache with a high hit rate and poor precision is actively harmful — it’s confidently returning wrong answers faster than the system would have gotten them wrong before. Teams that take semantic caching seriously usually sample a portion of cache hits for manual or automated review specifically to track precision, not just hit rate, since hit rate alone can look great right up until a threshold that’s slightly too loose starts quietly serving the wrong answers.

Where it fits — and where it doesn’t

Semantic caching pays off best for high-traffic, low-variance use cases: FAQ-style support bots, documentation search assistants, and any endpoint where many users end up asking near-identical questions. It’s a poor fit for prompts with hidden state — anything that depends on a specific user’s account data, a running conversation history, or the current date — since two prompts can be semantically similar in wording while requiring completely different, non-interchangeable answers.

It also doesn’t replace prompt-level caching offered by some LLM providers, which caches the model’s internal processing of a repeated prefix (like a long system prompt) to cut compute on the provider’s side. Semantic caching operates one layer up, at the application level, deciding whether to call the model at all.

The takeaway

Semantic caching trades an embedding lookup and a similarity comparison for the chance to skip a full model call, which is a good trade whenever a system sees many differently worded versions of the same underlying question. It needs a carefully tuned similarity threshold to avoid returning a plausible-but-wrong cached answer, and it’s best reserved for stateless, high-repetition prompts rather than anything that depends on per-user or per-conversation context.

Chisato Chisato · · 4 min read

What Is a KV Cache? Why LLM Inference Speeds Up

A KV cache stores past attention keys and values during LLM inference so each new token reuses prior work instead of recomputing it from scratch.

#AI #LLMs #Performance
Chisato Chisato · · 4 min read

What Is Quantization? Smaller, Faster AI Models

Quantization reduces the numeric precision of a model's weights — e.g. FP16 to INT8 or INT4 — to shrink memory use and speed up inference with minimal accuracy loss.

#AI #LLMs #Performance