What is a semantic cache for LLMs?
A semantic cache reuses LLM answers by meaning, not exact text. How it works, where it goes wrong, and how it differs from prompt caching.
A semantic cache is a cache for LLM answers that matches questions by meaning instead of by exact text. It turns each incoming question into an embedding, searches for a stored question that means the same thing, and returns the stored answer without calling the model. A reworded question can hit, where an exact-match cache would miss.
In plain words. Different words, same question: answer it once, then serve the stored answer. Finding similar questions is the easy part. Deciding when similar is safe to reuse is the hard part.
Why does exact-match caching fail for LLM traffic?
An ordinary cache keys on the exact bytes of the request. That works for a database row or an API response, where the same request is literally the same string. People do not ask questions that way. "How do refunds work?", "What's the refund window?" and "refund timeline?" are one intent and three strings, so a key-value cache stores three entries and you pay the model three times.
Provider prompt caching does not close this gap either, because it solves a different problem. OpenAI's documentation says prompt caching reuses work when requests share the same prompt prefix, and Anthropic describes it as resuming from specific prefixes in your prompts. Both make the input you resend cheaper on a call that still happens. Neither skips the call when a user asks an old question in new words. The two approaches stack, and prompt caching vs semantic caching covers how.
How does a semantic cache work?
Every semantic cache runs the same loop on the read path.
- Embed the question. An embedding model maps the text to a vector: a point in space, placed so that sentences with similar meaning land near each other. Sentence-BERT is the paper that made this practical, producing sentence embeddings that can be compared with cosine similarity.
- Search for neighbours. The cache looks up the stored questions nearest to that vector. At any real size this is an approximate nearest neighbour search, usually over an HNSW graph. HNSW explained walks through the index.
- Decide. If the nearest stored question is close enough, measured by cosine similarity against a threshold, it is a hit and the stored answer is returned.
- On a miss, call the model and write. The new question and its answer are stored, so the next paraphrase can hit.
Steps one, two and four are engineering. Step three decides whether the cache is useful or dangerous, because it is a judgement and not a lookup.
Why is similarity alone not safe?
"Cancel my subscription" and "pause my subscription" sit close together in embedding space. Serving one answer for the other is a customer-facing mistake. So is answering a question about one order with the status of another. A vector match says two sentences are near each other. It does not say one answer is correct for both.
Research says the same. The MeanCache paper finds that existing caching methods produce unacceptable false hit-and-miss rates. The vCache authors observe that a single static similarity threshold gives no formal correctness guarantee and results in unexpected error rates.
The practical answer is to stop treating a vector match as the whole decision. Crowkis is one implementation of that idea. Its CGET lookup can return four kinds of hit: exact, vector, structural, or dual. The structural path classifies the question into one of 12 intent classes and matches it against an intent template, alongside the vector search. Before anything is served, the candidate also has to clear a reuse threshold that depends on the intent class, a confidence score computed at read time, freshness rules and tenant isolation. Semantic cache false positives and confidence scoring covers that side in detail.
The write path matters as much as the read path. A semantic cache spreads a bad answer to every nearby question, so Crowkis scores each write through a five-stage pipeline and rejects incoherent pairs. See LLM cache poisoning and how to prevent it.
Semantic cache vs exact-match cache, prompt caching and a vector database
| Matches on | On a match | Gap | |
|---|---|---|---|
| Exact-match cache | Identical request text | Returns the stored answer | Misses every rephrasing |
| Provider prompt caching | An identical prompt prefix | Makes the repeated input cheaper; the model still runs | Does nothing for a reworded question |
| Vector database | Nearest vectors | Returns the nearest items; your code decides what to do with them | No reuse decision, lifetime or write checks of its own |
| Semantic cache | Meaning, then a reuse decision | Returns the stored answer with no model call | Must be right about when similar is safe |
A vector database is a building block. A semantic cache is the policy on top of one. Semantic cache vs vector database goes deeper on where the line sits.
What does a semantic cache look like in practice?
Crowkis speaks RESP3, so it is a drop-in for your existing clients, and the cache commands sit beside the usual GET and SET. Three commands cover the core.
CSET "How do refunds work?" "Refunds take 5 to 7 business days." EX 86400 TENANT acme MODEL gpt-4o
CGET "What is the refund timeline?" TENANT acme WITHCONFIDENCE
CSIM "How do refunds work?" "refund process?"CSET query response [EX secs] [TENANT t] [MODEL m] [MODEL_VERSION v]stores an answer.EXis the time to live in seconds,TENANTkeeps one customer's answers apart from another's, andMODELrecords which model produced it.CGET query [THRESHOLD f] [TENANT t] [WITHCONFIDENCE] [WITHROUTE]is the semantic lookup. It returns null on a miss.WITHCONFIDENCEadds the trust signals behind a hit, andWITHROUTEadds a routing recommendation (cache, cheap or expensive) derived from the confidence score.CSIM query1 query2returns the similarity score between two questions through the live embedder. It is the quick way to see why two phrasings did or did not match.
The embedder runs inside the server. The default model, crowsight, is bundled, works offline and produces vectors of dimension 384. You can point CROWKIS_EMBED_MODEL at your own ONNX sentence-embedding model instead; local embeddings with CEMBED has the details.
If you would rather not touch application code, the opt-in AI gateway puts the cache in front of POST /v1/chat/completions. A semantically matching earlier request is served from cache with the header x-crowkis-cache: hit. A miss is forwarded, cached and returned. Streaming requests are proxied but not cached yet.
What does a semantic cache save, and what does it cost?
A hit means the request never reaches the provider, so you do not pay for it and the user does not wait for generation. That is the whole saving, and it grows with how repetitive your traffic is. A support bot that hears the same questions all day gains a lot. A tool where every prompt is unique gains little. How much semantic caching can cut LLM costs works through the reasoning.
The cost is that every semantic lookup has to embed the question first. On Crowkis's own measurements, taken on a development laptop, a plain GET runs at 103,000 ops/sec at 512 connections, while a cold CGET runs at about 250 ops/sec. The gap is the embedding step, not the index search. Plan capacity around the semantic figure, not the key-value one.
When should you not use a semantic cache?
- Every prompt is unique. Summarising a new document, one-off code generation, anything with no repetition. There is nothing to reuse.
- The answer depends on who is asking. Balances, order status, anything personal. Scope those per tenant and user, or do not cache them. PII in an LLM cache explains what happens to personal data.
- The answer changes faster than you can invalidate it. Prices and live status need a short time to live or an explicit purge. See cache invalidation and freshness.
- The question only makes sense inside a conversation. "And the second one?" has no stable meaning by itself, so it should not be cached as a standalone question.
- Very long messages. A long message that contains a short cached question almost word for word can still miss, because the surrounding words dominate the embedding. The Crowkis release notes list this as a known limitation.
- Very large corpora, today. Crowkis has verified its vector index up to 1.18M vectors. Nothing above that has been measured successfully.
Common questions
Is a semantic cache the same as a vector database?
No. A vector database stores vectors and returns nearest neighbours. A semantic cache uses that search as one step, then decides whether a stored answer is safe to return, and manages lifetimes, tenants and writes. You can build a cache on a vector database, but the search is the easy part.
Does a semantic cache replace prompt caching?
No. Use both. Prompt caching lowers the price of a prefix you resend on calls that still happen. A semantic cache removes the call when the question has been answered before. They act on different parts of the same request stream.
Can a semantic cache return a wrong answer?
Yes, if it treats similar as identical. That is the false positive problem, and it is why thresholds, confidence scores and write checks exist. A well-built cache prefers a miss over a doubtful hit, because a miss only costs one model call.
Do I have to change my application?
Not much. You either call CGET before the model and CSET after it, from the RESP3 client you already have, or you point an OpenAI-compatible client at the gateway. The command reference and the Crowkis docs list every option, and the Crowkis product page has the install commands.