Glossary

Semantic cache

A semantic cache is a cache for LLM applications that returns a stored response when a new query is similar in meaning to an earlier one, so different wordings of the same question share one answer.

A semantic cache is a cache for language model applications that reuses a stored response when a new query means the same thing as an earlier one, even if the wording differs. It embeds each incoming query, looks for a previous query within a similarity threshold, and returns the cached answer instead of calling the model. Semantic caching cuts cost and latency for repetitive traffic, at the risk of serving an answer to a question that only looks similar.

How a semantic cache works

A semantic cache sits in front of the model, or in front of a retrieval step, and follows four steps on every request. First it converts the incoming query into an embedding with an embedding model. Second it searches a small vector index of previously seen queries for the nearest neighbour, usually with cosine similarity. Third it compares the best match’s similarity with a threshold. If the score clears the threshold, the cache returns the stored response and the model is never called. If it does not, the request goes to the model as normal, and the new query and its response are written to the cache for next time.

The pieces are familiar from vector search: an embedding model, an index that supports approximate nearest neighbor lookup, and some metadata stored alongside each entry. What makes it a cache is the extra logic around it: a similarity threshold, an expiry time for each entry, rules about which requests are eligible, and a way to evict or invalidate entries when the underlying information changes.

Semantic cache vs exact-match cache vs prompt caching

Three different things are called caching in LLM applications, and they solve different problems.

Type

What is reused

Matches on

Typical saving

Main risk

Exact-match cache

The full response

Identical request text

Whole model call on repeats

Low hit rate, since wording varies

Semantic cache

The full response, or retrieved context

Similar meaning above a threshold

Whole model call on near-repeats

Serving a wrong answer to a different question

Prompt caching

The model’s processed prefix

Identical prompt prefix

Cheaper, faster input tokens

Little, but only helps static prefixes

An exact-match cache, keyed on a hash of the request, is safe and cheap but rarely hits for natural language, because people phrase the same question in many ways. Prompt caching, offered by model providers, stores the processed state of a repeated prompt prefix (the KV cache) so that system prompts and long shared documents are cheaper to resend. It never skips the model call. A semantic cache is the only one of the three that can skip the call for a question it has not seen word for word, which is where both its value and its risk come from. The trade-offs between prompt caching and memory are covered in prompt caching vs memory.

A worked example

A support assistant for a software product receives thousands of questions a day. Many are variations of the same few dozen. On Monday, a user asks “How do I reset my password?”, the model answers, and the pair is cached. On Tuesday, another user asks “forgot my password, how do I change it”. The embedding similarity between the two queries is 0.94, above the 0.92 threshold, so the cache serves Monday’s answer in about 20 milliseconds instead of two seconds, and no tokens are spent.

An hour later, a third user asks “How do I reset my 2FA?”. Its similarity to the password question is 0.89. That is close, because the sentences share most of their words and structure, but the answer is different: resetting two-factor authentication is a separate flow with a support check. With the threshold at 0.92 the cache misses and the model answers correctly. With the threshold at 0.85, the cache would have returned the password instructions, confidently and wrongly. This one example captures the whole design problem: the threshold that gives a high hit rate is often the threshold that produces wrong answers.

Choosing a similarity threshold

There is no universal value. The right threshold depends on the embedding model, the domain and the cost of a wrong answer. A cache for general chit-chat can afford a loose threshold. A cache for billing, medical or legal questions needs a strict one, or should not exist at all. Teams usually pick the threshold empirically: collect pairs of real queries, label whether they should share an answer, and measure the hit rate and the false hit rate at different thresholds. The false hit rate is the number that matters most, because a cache miss costs money while a false hit costs trust.

Embedding similarity is also a blunt instrument for small but decisive differences: negations (“can I” and “can’t I”), numbers (“plan for 5 users” and “plan for 50 users”), and named entities (“reset my password” and “reset my 2FA”). Some systems add a cheap second check, such as a cross-encoder or a rule that entities and numbers must match exactly, before serving a cached answer.

What to cache

Caching the final answer saves the most, and carries the most risk. A safer variant caches the retrieval step instead: the query embedding maps to the set of passages that were retrieved last time, and the model still writes a fresh answer from them. That saves search latency and some cost without freezing the wording, and it degrades more gracefully when a near-match is not quite the same question. Rewriting queries into a normalised form before lookup, as in query rewriting, raises hit rates for both variants.

Some requests should never be cached. Anything personalised, anything that depends on the current time or live data, anything involving account state, and anything containing personal information are poor candidates. A useful default is to cache only requests that would produce the same answer for every user.

Staleness and invalidation

A cached answer is a snapshot of what the model and the knowledge base said at a point in time. When a policy, price or product changes, every cached answer that mentions it becomes stale knowledge. Time-based expiry is the simplest guard: entries live for hours or days, depending on how fast the content changes. Event-based invalidation is better where it is possible: when a document is updated, evict cache entries whose answers drew on it. That requires storing which sources each cached answer used, which is worth doing anyway for document freshness tracking and for audits.

Multi-tenant and privacy risks

In a product that serves many customers, a shared semantic cache is a data leak waiting to happen. If customer A’s question about their contract terms is cached and customer B asks something similar, B can receive an answer built from A’s documents. The cache key must include the tenant, and usually the user, so that lookups can only match entries from the same scope. This is the same principle as tenant isolation for search indexes, and cross-tenant data leakage through caches is one of the less obvious ways it fails. Cached responses are also stored data, so they fall under your data retention policy and deletion requests.

Tools and where caches live

Semantic caching is available as open-source libraries such as GPTCache, as features of vector databases and key-value stores such as Redis, through framework integrations in LangChain and LlamaIndex, and in some LLM gateways that sit between an application and its model providers. A gateway-level cache is convenient because it covers every service, and risky for the same reason, since it knows least about which requests are safe to reuse. Check each tool’s current documentation for how it scopes entries and sets thresholds.

When a semantic cache is worth it

A semantic cache pays off when traffic is repetitive, answers are the same for everyone, and the content changes slowly: public help centres, product FAQs, documentation assistants and internal IT questions. It pays off less for agents that act on user-specific data, for long multi-turn conversations, and for questions whose answers depend on context the cache cannot see. In those systems, most of the token spend comes from the context window itself rather than from repeated questions, and the larger saving usually comes from sending less context per request.

Exabase and semantic caching

Exabase does not offer a semantic cache as a product feature. It reduces token spend in a different way: Memory extracts durable facts from conversations so an agent can retrieve a few hundred relevant tokens instead of resending the full history, and Deep Search returns sub-document passages instead of whole chunks. The reduce token costs solution explains the approach, and the token cost calculator estimates the saving for your traffic. The two approaches combine well: a cache cuts repeated questions, and better context cuts the cost of the rest.

If you build a cache yourself, Exabase can serve as its storage. Approved question and answer pairs can be saved as notes in a Base, searched with Deep Search at a high precision setting so only close matches return, and scoped per customer by giving each tenant its own Base, which keeps one tenant’s cached answers out of another’s results. The Bases guide and searching resources guide cover the calls. The threshold logic and invalidation rules stay in your application. The posts on why prompt caching won’t save you and how your LLM bill is a retrieval problem cover the cost side in more depth.

Frequently asked questions

What is a semantic cache for LLMs?

A semantic cache stores LLM responses and returns a stored response when a new query is similar enough in meaning to a previous one. It compares query embeddings against a similarity threshold, so different wordings of the same question can share one answer.

How is semantic caching different from prompt caching?

Semantic caching can skip the model call entirely for a similar question. Prompt caching reuses the model’s processed prompt prefix to make input tokens cheaper, but the model still runs on every request.

What similarity threshold should a semantic cache use?

There is no fixed value. Teams measure hit rate and false hit rate on labelled pairs of real queries and choose the threshold where wrong answers are acceptably rare, which is often above 0.9 cosine similarity for factual domains.

What are the risks of a semantic cache?

The main risk is serving the answer to a different question that happens to look similar, such as a password reset answer for a 2FA question. Others are stale answers after content changes and cross-tenant leakage when cache entries are not scoped by customer.

Should I cache final answers or retrieved context?

Caching retrieved context is safer, because the model still writes a fresh answer and small differences between questions are handled. Caching final answers saves more and suits public, non-personalised FAQs.

Is semantic caching safe in a multi-tenant app?

Only if every cache entry is scoped to its tenant, and usually to its user, so lookups cannot match another customer’s entries. A shared cache across tenants can leak private information.

Does Exabase include a semantic cache?

No. Exabase cuts tokens through Memory and precise retrieval instead. You can build a cache on top of it by storing approved answers in a Base per tenant and matching them with Deep Search.

Related pages: prompt caching, prompt caching vs memory, KV cache, embedding, semantic similarity, LLM latency, Exabase vs Redis, customer support agents, multi-tenant SaaS, reduce token costs

Cut your token spend and give your agent precise context.

Get started in minutes.

Cut your token spend and give your agent precise context.

Get started in minutes.