What Is a Semantic Cache? Speeding Up Repeated LLM Calls

If your app sends the same question to a large language model ten times a day, you pay for the same answer ten times. A semantic cache for LLM calls fixes that waste. It stores answers by meaning, not by exact text, so a prompt that asks the same thing in different words gets the stored answer in milliseconds instead of a fresh model call. The savings add up in chatbots, support tools, and any app where users keep asking the same questions.

Picture a support chatbot for a small SaaS product. One user asks “how do I reset my password”, another asks “I forgot my password, what now”, and a third asks “where is the password reset link”. A normal cache sees three different strings and calls the model three times. A semantic cache sees three versions of one question and answers two of them from storage. Repeated LLM prompts are the whole reason this technique exists.

How a Semantic Cache for LLM Calls Works

Every prompt that arrives gets turned into an embedding, a list of numbers that captures what the text means. The cache compares that embedding with the embeddings of prompts it has already answered, using a similarity score such as cosine similarity. If the closest match scores above your threshold, the cache returns the stored answer. If nothing scores high enough, the request goes to the model as usual, and the new prompt and answer get stored for next time.

Three pieces do the work. The embedding model converts text to vectors. A vector database holds those vectors alongside the answers. The lookup logic runs the comparison and decides hit or miss. A managed vector store, pgvector inside Postgres, or a small in-memory store all work, depending on your traffic. A lookup takes a few milliseconds, while a model call takes seconds, so a cache hit feels instant.

The model that produces your semantic caching embeddings matters more than the store you pick. If two prompts mean the same thing but their vectors land far apart, the cache misses and you pay for a model call anyway. Use the same embedding model for every entry, because vectors from different models are not comparable. Store the model name with each entry so a future swap does not silently mix incompatible vectors.

Exact Caching vs Semantic Caching

A plain cache keys answers by the exact prompt string. It is simple, fast, and free of judgment calls, but it only helps when users type the same characters. A semantic cache keys answers by meaning, which catches reworded questions at the cost of a similarity check and the occasional wrong match.

Exact cache Semantic cache
Match type Identical prompt text Similar meaning
Hit rate on varied phrasing Low High
Risk of a wrong answer Almost none Real, needs a threshold
Extra cost per request Near zero One embedding call
Best for API retries, fixed templates Chatbots, support, search

Many teams run both. The exact cache sits in front and catches byte-identical repeats for free; the semantic cache behind it catches the reworded ones. Together they cover the two ways the same question shows up.

Where Semantic Caching Pays Off

The pattern fits anywhere the same questions return in new wording. A support chatbot is the clearest case: password resets, pricing questions, and refund policies get asked hundreds of ways. If you build a chatbot on your own data, a semantic cache keeps answers consistent and the bill down, because the same grounded answer serves every phrasing of the question.

Internal tools fit too. A team wiki assistant, a code helper that explains the same internal APIs, or a sales tool that answers the same product questions all see heavy repetition. Retrieval-augmented generation systems pair well with it too: the cache sits in front of retrieval, so a repeated question never reaches the retriever or the model.

It does not fit everywhere. Creative writing and brainstorming should differ each time, so caching them adds risk for no gain. Questions about live data, like stock levels or account balances, go stale the moment they are stored. And highly personal questions should never share a cached answer across users.

The Parts You Need to Build One

A working setup has five parts, and none of them is exotic:

  1. Embedding model. Pick one and stick with it. Your provider’s embedding endpoint or a small local model both work.
  2. Vector store. Redis, pgvector, SQLite with a vector extension, or a managed service. Match the store to your scale.
  3. Cache key. Include the model name and the settings that change the answer, such as temperature and system prompt version. Two prompts that look alike but run under different settings are not the same question.
  4. Similarity threshold. The cutoff score above which a stored answer is good enough. Tune it with real traffic, not guesses.
  5. Expiry and eviction. Set a time-to-live and a size limit, so old answers leave before they turn wrong.

Start small. A few hundred entries in an in-memory store with a daily expiry will show your hit rate on real traffic within a week. If the numbers justify it, move to a persistent store and add per-tenant scoping.

Picking a Similarity Threshold

The threshold is the one setting that decides whether the cache helps or hurts. Set it too low and the cache answers questions that only sound alike. “How do I cancel my subscription” and “how do I cancel my free trial” score high on similarity but need different answers. Set it too high and the cache almost never hits, so you paid for embeddings and got nothing back.

Start at a cosine similarity of 0.9 or above for short prompts, then adjust from evidence. Pull a sample of real prompts from your logs, run them against the cache, and read the matches yourself. Move the threshold until the wrong hits disappear while the correct ones stay. Re-check after any change to the embedding model or the prompt format, because both shift the scores. This tuning loop is also one of the cheapest ways to cut your AI bill without touching model quality.

What Can Go Wrong

Stale answers are the first failure. A cached answer about your pricing stays correct until pricing changes, then keeps answering with the old numbers until the entry expires. Keep time-to-live short for anything that changes, and flush entries when the underlying facts change.

Near-miss matches are the second. Two prompts can score 0.93 similar and still want different answers, as the cancel-subscription example shows. The fix is a higher threshold plus a weekly spot-check: read a sample of cache hits and confirm the answers still fit the prompts.

Privacy is the third. A cache that stores prompts and answers can leak one user’s data to another if the store is shared. Scope entries per tenant or per user where answers contain personal data, and treat cached answers with the same retention rules as your logs. Never cache answers that contain passwords, tokens, or account details.

Finally, a bad answer that gets cached keeps coming back. If the model hallucinates once and that answer is stored, every similar prompt receives the same hallucination until the entry expires. Log cache hits alongside model calls so you can trace a wrong answer to its source and remove it.

Semantic Cache vs Prompt Caching

The two techniques sound alike and stack well together. LLM prompt caching is provider-side: the API provider reuses computation for the exact prefix of a prompt you have already sent, which cuts cost on long repeated system prompts. A semantic cache is yours: it skips the model call entirely when a new prompt means the same as a stored one.

Prompt caching helps when the start of your prompts repeats, which is common with long system prompts and few-shot examples. Semantic caching helps when the whole question repeats in new words. Use prompt caching for the fixed parts of your requests and a semantic cache for the repeated questions; each saves money the other cannot reach. Our guide to llm prompt caching explains the provider side in more detail.

Should You Build One?

Build one if three things are true: much of your traffic repeats the same questions, the answers stay valid for hours or days, and model cost or latency is a real line item. Skip it if your prompts are mostly unique, your answers change by the minute, or your volume is so low that a week of traffic fits in a spreadsheet.

The cheapest way to decide is to measure first. Log a week of prompts, embed them, and count how many sit close to an earlier prompt. That share is your rough cache hit rate, and it tells you whether the build is worth an afternoon. If it is, start with the five-part setup above and watch the hit rate weekly.

Ditulis oleh

Firdaus Safari Putra

Founder, PT FirKaizen Digital Solutions

Firdaus Safari Putra adalah founder PT FirKaizen Digital Solutions. Ia membangun dan mengoperasikan infrastruktur server di balik panduan ini, dan menulis berdasarkan apa yang benar-benar ia jalankan, uji, dan ukur — bukan teori.