Semantic caching: when it saves money and when it is wrong
Semantic caching returns a stored LLM answer when a new prompt means the same as an old one. How it works, where it returns wrong answers, and how to scope it.
- Published
- Reading time
- 10 min read
In short
- Semantic caching stores model responses and returns one when a new prompt's embedding is close enough to a stored prompt's. A hit skips the model call entirely.
- The similarity threshold trades hit rate for wrong answers. Two prompts can sit close together in embedding space and still need opposite answers, so one global threshold gives no correctness guarantee.
- Scope the cache by tenant, user, model, and request parameters, and do not cache tool calls, personalized answers, or long conversations. A shared cache is a path for one user's answer to reach another.
- Measure the false-hit rate as well as the hit rate. Sample cached hits, run the same prompts through the model, and grade whether the stored answer was acceptable.
Semantic caching is a technique that stores a model's responses and returns a stored response when a new prompt means the same thing as an earlier one. Instead of matching text exactly, a semantic cache compares embeddings and treats two prompts as equal when their similarity score clears a threshold. A hit returns an answer without calling the model at all.
That is a much stronger kind of reuse than prompt caching, and it carries a risk prompt caching does not: a semantic cache can return the wrong answer. Whether it is worth running depends on how often your traffic repeats and how much a slightly wrong answer costs you.
How a semantic cache works
A semantic cache sits in front of the model and answers in two stages. It first looks for an exact match on a hash of the normalized request. On a miss, it embeds the prompt, searches a vector store for the nearest stored prompt, and returns that prompt's stored answer if the similarity score is above a threshold. Otherwise the request goes to the model, and the answer is written back.
The pieces are the same in every implementation: an embedding model, a vector store, a similarity function (almost always cosine similarity), a threshold, and a time to live. GPTCache, an open-source Python library described in a 2023 NLP-OSS workshop paper, is an early and widely used example of this design, with a separate similarity-evaluator step that decides whether a candidate counts as a hit.
The cache can live in the application or in the gateway. A library inside the application is easy to try. A gateway-level cache covers every application at once and keys entries by the gateway's own notion of caller identity. Bifrost, an open-source AI gateway by Maxim AI, runs semantic caching this way, and its defaults are a useful reference for the decisions below.
Exact match first, similarity second
Run the exact-match lookup before the semantic one. Many repeats are byte-for-byte identical, such as retries, page reloads, and batch jobs that resend the same prompt, and a hash lookup catches them in microseconds without any risk of matching a different meaning.
Bifrost orders its lookups this way: a direct hash match runs first, and the semantic search runs only on a direct miss. A direct-only mode needs no embedding provider at all, which is a reasonable first deployment. You get the safe hits immediately and can measure how much traffic is left over before deciding whether similarity matching is worth its risk.
Similarity matching has a cost on every miss. Each semantic lookup makes an embedding call and a vector search before the request goes to the model anyway, so a semantic miss is slower than no cache at all. If your hit rate is low, the cache makes most requests slower to save money on a few.
Where semantic caching returns the wrong answer
A semantic cache returns a wrong answer when two prompts are close in embedding space but need different responses. Embeddings capture topic and phrasing well. They are much weaker at negation, word order, numbers, and who is asking, and those are exactly the details that change an answer.
| Prompt A | Prompt B | Similar to an embedding? | Same answer? |
|---|---|---|---|
| How do I cancel my order? | How do I not cancel my order by mistake? | Very | No |
| Flights from Boston to Denver | Flights from Denver to Boston | Very | No |
| Summarize the 2025 annual report | Summarize the 2026 annual report | Very | No |
| What is my account balance? | What is my account balance? (different user) | Identical | No |
| And how do I do that in Python? | And how do I do that in Python? (different conversation) | Identical | No |
| How do I reset my password? | I forgot my password, how do I change it? | Very | Yes |
Only the last row is the case semantic caching is built for. The other five are false hits. The fourth and fifth are the dangerous ones, because the prompts are identical: no threshold can separate them. They have to be handled by what the cache is scoped to, not by how strict it is.
The research agrees that a single threshold cannot guarantee correctness. The vCache paper from 2025 argues that a static similarity threshold applied to every request gives no formal correctness guarantee and can produce unexpected error rates. It proposes learning a separate threshold for each cached prompt instead, and reports up to 12.5 times higher hit rates and 26 times lower error rates than static-threshold baselines on its benchmarks.
Choosing a similarity threshold
There is no correct default threshold. The right value depends on your embedding model and your traffic, so measure it: collect a few hundred prompt pairs from real traffic, label whether each pair should share an answer, and pick the threshold that keeps false hits at a rate you can accept.
The published results show what is possible on narrow traffic. The GPT Semantic Cache paper reported cache hit rates between 61.6% and 68.8% across the query categories it tested, with more than 97% of hits judged correct. Those categories were chosen to repeat. A general-purpose assistant will see far fewer repeats and far more near-misses.
Three rules hold regardless of the number:
- Set the threshold per route, not per deployment. A public FAQ bot can tolerate a looser match than a billing assistant.
- Let callers tighten the threshold, never loosen it. Bifrost's default threshold is a cosine similarity of 0.8, and a per-request
x-bf-cache-thresholdheader can make matching stricter, but a lower value is raised back to the configured floor. - Re-tune when you change the embedding model. Similarity scores from two models are not comparable.
What to scope the cache by
Scope a semantic cache so that a stored answer can only reach callers who would have received the same answer. That means partitioning by tenant and user, by model and provider, by request parameters, and by prompt version. Each partition you remove raises the hit rate and widens the set of people a wrong or private answer can reach.
Scoping by user is a privacy control, not only a correctness one. A cache shared across users can return one user's answer, including anything personal in it, to another user who asked a similar question. MeanCache, a 2024 paper on semantic caching for LLM web services, goes as far as keeping the cache on each user's device for this reason.
Bifrost's defaults lean narrow, which is the right starting point:
- A cache key is required, sent per request or set as a default. A request with neither bypasses the cache.
- Identity scoping is on by default. An entry is served only to the virtual key and user that wrote it, and two virtual keys never share entries.
- Entries are separated by model and by provider unless you turn that off.
- The exact-match hash covers the normalized input, the request parameters, and whether the request streams. Any difference is a miss.
Start with every partition on. Remove one only when you can show that the wider scope does not change answers.
What not to cache
Some responses should never come from a semantic cache, however similar the prompt. Skip responses that contain tool calls, answers inside long conversations, answers that depend on time or on live data, and answers personalized to the caller. For each of these, a stored answer can be fluent, confident, and wrong.
- Tool calls. A cached tool call replays an action against state that may have changed. Bifrost does not store responses containing tool calls unless you enable
cache_tool_call_responses. - Long conversations. A follow-up like "and in Python?" means nothing without its history. Bifrost skips caching when a conversation has more than three messages by default, through
conversation_history_threshold. - Time-sensitive answers. Prices, inventory, status pages, and anything that says "today" go stale. Keep the time to live shorter than the rate at which the underlying facts change; Bifrost's default is five minutes.
- Retrieval over changing data. If the answer was built from documents that have since been updated, the cached answer is out of date even when the question is identical.
How to measure a semantic cache
Measure four numbers: the hit rate, split into exact and semantic hits; the false-hit rate; the latency saved on hits and added on misses; and the net cost saved after embedding calls. The hit rate on its own will always look good, because a looser threshold always raises it.
The false-hit rate needs a process, not a metric. Sample a few hundred semantic hits a week, send the same prompts to the model, and grade whether the stored answer was acceptable in place of the fresh one. An LLM judge can do the grading if you check it against human labels first. Track the rate per route and alert when it rises.
At the gateway, the counters come for free. Bifrost exports a bifrost_cache_hits_total Prometheus counter broken down by cache type, so exact and semantic hits can be charted separately. Store the similarity score of every semantic hit in your request logs as well. When a user reports a strange answer, the score tells you whether the cache served it and how close the match was.
Semantic caching and prompt caching are different tools
Semantic caching reuses an output. Prompt caching reuses the processing of an input. One skips the model; the other makes the model cheaper and faster to run on a repeated prefix. They solve different problems, carry different risks, and work well together.
| Semantic caching | Prompt caching | |
|---|---|---|
| What is reused | A stored response | The provider's work on a prompt prefix |
| Match rule | Similar meaning, above a threshold | The exact same prefix |
| Can it return a wrong answer? | Yes | No. The model still runs on the full prompt |
| Who runs it | You, in the application or the gateway | The model provider |
| What it saves | The whole call | Part of the input price, and some latency |
Turn prompt caching on everywhere. Add semantic caching only on routes where the same questions repeat, a stored answer is acceptable to every caller in the cache's scope, and you have a way to measure false hits. MeanCache's authors put repeated queries at about 31% of the total; if your own logs show nothing like that, the cache will cost more than it saves.
Frequently asked questions
What is semantic caching in LLM applications?
Semantic caching stores LLM responses keyed by an embedding of the prompt. When a new prompt arrives, the cache embeds it, finds the most similar stored prompt, and returns that prompt's answer if the similarity score is above a set threshold. A hit avoids the model call, which saves its cost and its latency. A miss goes to the model as usual.
How is semantic caching different from prompt caching?
Semantic caching returns a stored answer for a similar question and never calls the model on a hit. Prompt caching is a provider feature that reuses the processing of an identical prompt prefix while the model still generates a fresh answer. Prompt caching cannot change the answer. Semantic caching can, when two similar questions need different answers.
What similarity threshold should a semantic cache use?
Pick the threshold from your own labeled data, not from a default. Collect prompt pairs from real traffic, mark which pairs should share an answer, and choose the value that keeps false hits acceptable for that route. Many tools default to a cosine similarity around 0.8, which is a starting point to measure against, not a guarantee of correct answers.
Is semantic caching safe for multi-tenant applications?
It is safe only if entries are scoped to the tenant, and usually to the user, that created them. A cache shared across tenants can return one customer's answer, including private details, to another customer who asked a similar question. Partition the cache by caller identity first, then by model and request parameters, and widen scope only where answers are truly the same for everyone.
Does semantic caching work with streaming responses?
Yes, if the cache stores the full response and replays it as a stream. The client sees the same chunked format it would get from the model, delivered faster. Bifrost, for example, caches streamed responses and replays them chunk by chunk. Check that your cache marks replayed responses so your logs can tell a cached stream from a live one.
Which vector database should a semantic cache use?
Any vector store with fast nearest-neighbor search works, and the choice matters less than the threshold and scoping. Redis or Valkey suits teams that already run them and want low latency. Dedicated stores such as Qdrant, Weaviate, Pinecone, or Milvus suit larger caches. Bifrost supports Redis or Valkey, Weaviate, Qdrant, and Pinecone, and recommends Redis or Valkey for exact-match-only mode.
Sources
- 01GPTCache: An Open-Source Semantic Cache for LLM Applications Enabling Faster Answers and Cost SavingsNLP-OSS 2023, ACL Anthology
- 02GPTCacheGitHub
- 03vCache: Verified Semantic Prompt CachingarXiv:2502.03771
- 04GPT Semantic Cache: Reducing LLM Costs and Latency via Semantic Embedding CachingarXiv:2411.05276
- 05MeanCache: User-Centric Semantic Caching for LLM Web ServicesarXiv:2403.02694
- 06Semantic cachingBifrost docs