Semantic Caching: How It Works and When to Reuse Answers

Understand semantic caching, its four-step lookup, and how thresholds, expiry and user scope prevent similar questions from receiving the wrong answer.

Published

Semantic Caching: How It Works and When to Reuse Answers

Semantic caching can remove repeated model calls, but only where differently worded requests can safely receive the same answer. Its 4-step path is simple: embed the prompt, find neighbours, apply a threshold, then return a stored answer or call the model. The difficult part is deciding when a close match is still the wrong answer.

Start with a stable help centre or public product FAQ. A large language model (LLM), the system generating the answer, should not have to explain the same published policy whenever someone changes the wording. Keep account balances, order status and actions such as issuing refunds outside that shared cache.

The implementation details below were verified against primary documentation on 21 October 2026. The worked shop example is fictional; its similarity scores illustrate a decision, not a measured embedding result.

Semantic Caching vs Exact Caching and Prompt Caching

A semantic cache reuses a previous response when a new request appears to mean the same thing. Think of a help-desk clerk retrieving an approved answer from a filing cabinet: different wording can lead to the same file, but the clerk still checks which policy and customer it applies to.

An embedding is a numerical representation of text used to compare meaning. The cache stores an earlier prompt's embedding alongside its answer and identifying context. A new prompt becomes another embedding, and a vector search looks for nearby entries. Microsoft's semantic cache documentation describes this response-reuse pattern and why conversational context matters.

These cache layers save different work:

Cache layerWhat has to matchWhat gets reusedWhat still happens
Exact response cacheThe chosen request key, including relevant contextA previously generated responseKey lookup and eligibility checks
Semantic response cacheMeaning close enough, plus all required context and scope checksA previously generated responseEmbedding, search and eligibility checks
Provider prompt cacheAn eligible matching prompt prefixPreviously processed input stateThe model generates the response

With exact response caching, “Can I return these headphones?” and “Are these headphones returnable?” normally have different keys. Semantic caching can connect the paraphrases. Neither approach makes a stale or incorrectly scoped answer safe.

Provider prompt caching does not retrieve an old answer because the question sounds similar. Anthropic's prompt caching documentation describes reuse of matching prompt prefixes and explicitly separates that from output generation. Your application still asks the model to produce a response. The KV cache explainer covers the model-side state behind this distinction.

The layers can coexist: check an exact response cache, then a semantic response cache, then call the model with provider prompt caching where available. Measure semantic caching against that already-optimized baseline.

Semantic Caching LLM Responses: Four Steps

The lookup should find a reusable answer within an allowed context. Establish that context before searching: the authenticated user or tenant, applicable policy, language, and any other facts that change what the answer may say.

  1. 1. Embed the prompt

    Convert the incoming question into an embedding using the same embedding model and compatible configuration used for stored entries. Include the conversational context needed to interpret the request, or restrict caching to self-contained questions.

    “What about mine?” is not a complete cache key. If resolving “mine” requires private order data, bypass the shared response cache.

  2. 2. Look up neighbours

    Search stored prompt embeddings for nearby candidates inside the permitted scope. Retrieve each candidate's answer and metadata, meaning the fields that describe its owner, source version and expiry.

    A nearest neighbour is simply the closest entry found. It can still be irrelevant. Finding something is not the same as finding a hit.

  3. 3. Apply the threshold and eligibility checks

    Compare the candidate's similarity or distance with your calibrated cutoff. Then require exact agreement on answer-changing fields, such as product condition, policy version and access scope.

    If the score passes but the context does not, treat it as a miss. If the required context cannot be established reliably, also treat it as a miss.

  4. 4. Return the answer or call the model

    On an accepted hit, return the stored answer without a new generation call. On a miss, run the normal model or retrieval workflow, return its result, and store it only if it passes your cache-admission rules.

    Do not automatically turn every model output into a reusable answer. Errors, personal details and responses tied to transient tool results may be unsuitable.

Architectural paths pass through Embed, Search and Check, then either go directly to Return or pass through Model.
A hit bypasses generation. A miss still pays for the lookup before taking the model path.

The important difference from semantic search is what happens after retrieval. Search presents relevant material; a response cache may directly present a previous answer as the answer to this request. That makes a false match more consequential.

A Worked Hit and a Near-Miss That Must Not Hit

A single changed condition can invalidate an otherwise convincing match. Consider an imaginary shop whose policy permits returns of unopened headphones and excludes opened headphones. This is teaching data, not a statement about any retailer's policy.

The cache contains:

  • Stored question: “Can I return unopened headphones?”
  • Stored answer: “Yes. Unopened headphones are eligible under this shop's return policy.”
  • Stored context: the shop's current policy version, the headphones category, English, and condition=unopened.

For this illustration only, suppose the minimum cosine similarity is 0.90. Cosine similarity compares vector direction; it is not a probability that an answer is correct. The scores below are invented to show the decision.

Incoming questionIllustrative scoreEligibility checkResult
“May I send back headphones if the box is still unopened?”0.96Same shop, policy and unopened conditionHit: return the stored answer
“Can I return headphones after opening the box?”0.94Condition is opened, not unopenedMiss: reject that cached answer

Both candidates clear the numerical threshold. Only the paraphrase is allowed to reuse the answer. The near-miss shares the product, action and much of the wording, but changes the fact that decides the response.

The application should carry the condition as a structured field when it is known. If it must extract the condition from text, uncertain extraction should fail to a normal answer path. Another embedding comparison is not a substitute for knowing whether the item was opened.

Two architectural service bays show unopened and opened headphone boxes, with Reuse available only for the unopened condition.
Similar topic, different eligibility: the changed condition must block reuse.

The Knobs That Decide Whether Reuse Is Safe

Threshold, time to live and scope work together. Tuning only the threshold leaves the cache vulnerable to perfectly similar requests that need different answers.

Similarity Threshold: Calibrate Against Wrong Answers

A stricter cutoff trades some useful hits for fewer false matches. A looser cutoff increases reuse and can increase incorrect reuse. Build a labeled set of paraphrases and hard negatives, meaning similar questions whose answers must differ, from your own traffic.

Include changed dates, product variants, negation, currencies, account roles and conversation histories. Judge whether the stored answer is valid for the new request, rather than merely whether the prompts discuss the same topic.

Read the metric before changing the dial. Redis Vector Library (RedisVL), a library for building applications with Redis vector search, documents distance_threshold in Redis COSINE distance units from 0 to 2, where lower values mean stricter matching. A minimum similarity threshold usually moves in the opposite direction. RedisVL API reference.

There is no universal cutoff to copy between embedding models, datasets or products. Changing the embedding model also requires a deliberate index migration and fresh calibration; old and new vectors should not be mixed casually.

Time to Live: Expiry Is Only a Backstop

Time to live (TTL) is how long an entry remains eligible before it expires. Choose it from how long you can tolerate an old answer, not from how long the cache can hold it.

A stable documentation answer and a live stock-availability answer have different freshness needs. The latter may be better left uncached. An emergency policy correction should invalidate affected entries immediately, even when their TTL has not elapsed.

Use a source or policy version as part of the eligibility check. When that version changes, old entries stop matching. RedisVL exposes both a default TTL and per-entry overrides, but the application must decide the appropriate lifetime. RedisVL cache API.

Per-User Scope: Identity Must Be an Exact Boundary

Use shared caching only for answers intended to be shared. A tenant is a customer organization; a tenant boundary alone is insufficient when employees inside that organization have different access rights.

An account-specific answer needs a scope that reflects the authenticated user and current permissions. “What is my renewal date?” must not search across everyone who asked the same question. Obtain identity from trusted application authentication, not from a user-supplied sentence or an embedding.

Apply scope filters before selecting the candidate, then validate authorization before returning it. Permission changes and deletions must invalidate the affected reuse path. RedisVL's filterable_fields and filter_expression provide filtering primitives; they do not automatically define your authorization policy. Filtering parameters.

Context and Versions: Keep the Answer's Assumptions Attached

Scope entries by the answer-affecting system prompt, model requirements, output schema, language and source version. A cached paragraph cannot satisfy a new request that requires a different structured response.

For conversation caching, the same final sentence can refer to different earlier subjects. Microsoft's context-window explanation makes this failure explicit. Start with standalone questions unless you can represent and validate the relevant history.

Implementation Options: Gateway, Redis Pattern or Library

Choose the integration point according to where you can enforce the answer's boundaries. These are implementation examples, not a ranking of interchangeable products.

Gateway Feature: Kong AI Semantic Cache

Kong AI Semantic Cache, a plugin in Kong's AI Gateway Enterprise offering, places semantic response caching at the gateway, the entry point through which model requests pass. Its documentation describes embedding-based lookup with Redis or pgvector configurations. The configuration reference exposes cache_ttl, message_countback and vectordb.threshold.

Kong documentation describing the AI Semantic Cache plugin and its request flow.
Kong AI Semantic Cache documentation

This fits centrally managed, repeatable request flows. Its main limitation is application context: a gateway can only enforce the distinctions you carry into its configuration and requests. Verify user isolation and history handling before enabling it for personalized chat.

There is also a naming detail worth checking: Kong's plugin overview says enabling its exact-caching option does not disable similarity-based matches. Do not interpret that switch as an exact-only safety mode.

Redis Semantic Caching

Redis, a data store with vector-search capabilities, lets the application own the lookup and miss path. RedisVL's documented SemanticCache provides check and store operations, metadata, filters and TTL controls. RedisVL documentation.

RedisVL SemanticCache API showing lookup, storage, filtering and expiration controls.
RedisVL SemanticCache API

Use this pattern when your application already knows the tenant, policy and record versions and needs to enforce them before returning a result. The same architectural pattern can be implemented around another suitable vector store: prompt vectors locate candidates, while stored answers and metadata decide what can be reused.

The tradeoff is ownership. A vector index does not supply cache-admission rules, invalidation, authorization or a correct answer. You must implement and monitor those behaviours, and operate a deployment that supports the vector queries you use.

Library: GPTCache

GPTCache, a library for constructing semantic caches, separates embedding generation, answer storage, vector search and similarity evaluation into configurable components. Its documentation explicitly warns that not every combination of modules is compatible.

GPTCache API documentation describing its similarity evaluators.
GPTCache similarity evaluation API

This fits a builder who wants to experiment with the retrieval and evaluation pieces inside an application. Its main limitation is integration responsibility: verify the chosen adapters against your current model SDK and deployment, and keep the same scope and freshness checks around it. A library abstraction does not make a semantic hit correct.

If you are also choosing the request-routing layer, use the AI gateway comparison and LiteLLM vs OpenRouter for that broader decision. Caching is one requirement alongside routing, credentials and observability.

Work Out Savings From Your Own Request Mix

A cache saves money only when avoided generation costs exceed embedding, lookup, storage and operating costs. Count misses too: they incur cache overhead and still call the model.

Use one measurement period and one currency. Let N be eligible requests reaching the semantic layer, after any exact-cache hits; H be accepted semantic hits; G be the generation cost per request under a simplifying equal-cost assumption; E be embedding plus lookup cost per eligible request; W be cache-write cost per miss; and F be fixed infrastructure and operating cost for the period.

The worksheet is:

  • Without the semantic layer: N × G
  • With it: (N − H) × G + N × E + (N − H) × W + F
  • Net saving: H × G − N × E − (N − H) × W − F

Work through it using your logs: multiply your accepted hits by the model cost those hits avoided, subtract lookup costs for all eligible requests, subtract writes for misses, then subtract fixed costs. Use generation costs after any provider prompt-cache benefit, not the uncached list rate.

Under those assumptions, the break-even condition is:

H > [N × (E + W) + F] / (G + W)

For a mixed workload, replace H × G with the sum of avoided generation costs for the actual hit requests. Count actual writes if some responses are not admitted. Include retries, evaluation calls and operational work in the relevant totals. A hit rate alone cannot tell you whether reuse paid for itself.

The same reasoning applies to latency. Hits remove generation from their path; misses add embedding and search before generation. Record both paths, including slow-tail latency. A mostly unique workload can get slower even though its occasional hits are fast.

What Is Overhyped About Semantic Caching?

“Similar meaning” is not a guarantee of answer equivalence. The cache selects an existing answer; it does not establish that the answer remains correct, authorized or current.

The most misleading metric is an improving hit rate without an accompanying wrong-hit audit. A relaxed threshold can make the dashboard look better while increasing rework for support staff. Review cached answers against the new requests, especially the ones closest to your acceptance boundary.

Nor does response caching replace retrieval-augmented generation (RAG), where a model receives retrieved source material to help produce an answer. A cache can sit before that workflow, but skipping it also skips a fresh source lookup. Tie cached answers to source versions or bypass reuse when the underlying facts must be current.

Treat “cancel my order” as an action, not a reusable question. A previous confirmation proves nothing about whether a new action was authorized or executed. Keep execution and its current result on the normal application path.

Who Should Act Now, and What to Do Next

Builders with repeated public questions should start now, narrowly. Choose one stable intent, define its allowed scope and retain an exact-cache baseline. The benefit is fewer repeated generation calls without expanding the kinds of answers the application is allowed to reuse.

Operators should run a shadow evaluation first. Log which cached answer would have been returned while continuing to serve the normal answer. Label true paraphrases and near-misses, then tune the threshold around unacceptable errors. Measure cache overhead, expiry behaviour and the quality of the stored answer itself.

Buyers should ask for isolation and invalidation evidence. A semantic-cache checkbox is not enough. Ask how the system handles identical questions from different users and what happens immediately after a policy changes.

Wait if most questions depend on changing records or multi-turn context you cannot isolate. If exact repeats already cover the reusable traffic, semantic lookup may add little. Provider prompt caching can still help repeated input prefixes without requiring you to reuse a finished answer.

Your Monday move: take one help-centre intent, collect its paraphrases and deliberately confusing alternatives, define the cache scope and invalidation rule, and replay them in shadow mode. Enable response reuse only after the wrong-answer cases fail safely.

Frequently Asked Questions

What is semantic caching?

It stores previous answers and retrieves them using meaning-based similarity. Reuse should also require matching context, valid permissions and adequate freshness.

What are the three types of cache?

For this LLM decision, distinguish exact response caching, semantic response caching and provider prompt caching. That is a useful comparison for this article, not a universal classification of every computer cache.

How to implement semantic caching?

Define the allowed scope, embed incoming prompts, retrieve candidates, apply a calibrated threshold and eligibility checks, then return the stored answer or call the model. Add expiration, invalidation and wrong-hit monitoring before enabling reuse.

Is a CDN just a cache?

No. A content delivery network (CDN) distributes content delivery across locations; caching is one mechanism it uses. See Cloudflare's CDN architecture.

Is Cloudflare a CDN or WAF?

Cloudflare provides both CDN services and a web application firewall (WAF), which filters application traffic. Neither label by itself means semantic LLM response caching. Cloudflare architecture.

What is the difference between CDN and Redis?

A CDN delivers content across an edge network. Redis is a data store that an application can use for caching, including a configured semantic-cache pattern. They occupy different parts of the system.

Is CloudFlare just a CDN?

No. Cloudflare also documents DNS, firewall and other edge services in its reference architecture.

Is Kafka better than Redis?

They solve different jobs. Apache Kafka is an event-streaming platform. It is not a drop-in replacement for the Redis semantic-response-cache pattern described here.

Does Netflix use Redis?

Yes. Redis documents Netflix's use of Redis for priority queuing, with a link to Netflix's Timestone write-up. That historical example is not evidence of semantic LLM caching or a reason to enable it in your application.

For practical notes on building reliable AI applications, join the newsletter.

Published
Category
Build
Related Articles
Pydantic AI vs LangChain

Pydantic AI vs LangChain

Choose a Python agent framework with matching ticket-triage code, validated outputs, LangGraph workflows, tracing, evals and migration guidance.Oct 21, 2026Build
Zed vs Cursor 2026: Editing Feel, Agents and Teamwork

Zed vs Cursor 2026: Editing Feel, Agents and Teamwork

Zed vs Cursor for developers: compare editing feel, agents, model choice, collaboration and the limits of switching or using both.Oct 21, 2026Build
LLM Inference Explained: Prefill, Decode and Serving Choices

LLM Inference Explained: Prefill, Decode and Serving Choices

Understand LLM inference, from prefill and decode to caching and batching, then choose between a model API, an inference provider and your own GPUs.Oct 21, 2026Build
Semantic Search: How It Works and When to Use It

Semantic Search: How It Works and When to Use It

Learn how semantic search uses embeddings, when keywords still win, and how hybrid retrieval and reranking fit a production search system.Oct 21, 2026Build
Factory AI

Factory AI

Factory Droids for engineering teams: verified capabilities, workflow examples, limitations, and when to choose it beside Claude Code, Codex or Cursor.Oct 21, 2026Build
Speech to Text API 2026: Mistral, Deepgram, AssemblyAI, OpenAI, Google, ElevenLabs and Speechmatics Compared

Speech to Text API 2026: Mistral, Deepgram, AssemblyAI, OpenAI, Google, ElevenLabs and Speechmatics Compared

Compare Mistral Voxtral 3, Deepgram, AssemblyAI, OpenAI, Google, ElevenLabs and Speechmatics for files, live agents, languages and speaker labels.Oct 21, 2026Build
Cerebras Pricing: API Costs and the Production Catch

Cerebras Pricing: API Costs and the Production Catch

Cerebras API rates, model speeds, the $5 trial and a $53.75 agent budget. See why Developer access differs from production, plus Groq comparisons.Oct 20, 2026Build
How to Get OpenAI API Key

How to Get OpenAI API Key

Create an OpenAI project key, add billing, set a hard spend limit, store it safely, and test one request. Includes errors and leaked-key recovery.Oct 20, 2026Build
Newsletter

One letter, every Sunday.Working systems, not hot takes.

Weekly. No spam. Unsubscribe anytime.