
Dan Patiño
AI Strategy & Innovation at Coderhouse
Artificial Intelligence
Semantic caching is a technique that stores LLM responses and reuses them when a new request means the same thing as a previous one, even if it is worded differently. Instead of matching the exact text of a prompt, a semantic cache converts each query into an embedding and looks for a stored query that is close enough in meaning. If it finds one, the application returns the cached answer without calling the model again.
For teams running AI assistants, support bots and internal copilots in production, this matters for two reasons: cost and latency. Many users ask the same handful of questions in slightly different ways. Paying for a fresh model call every time — and making the user wait several seconds for it — is wasteful when a correct answer already exists. That is why semantic caching has become a standard tool in the forward-deployed AI engineer’s production toolkit.
What is semantic caching?
A traditional cache works on exact keys: if the request string is identical, return the stored value. That works poorly for natural language. “How do I reset my password?”, “I forgot my password, what do I do?” and “password reset steps” are three different strings with the same intent.
A semantic cache solves this by comparing meaning instead of characters. The basic flow looks like this:
Embed the query: the incoming prompt is turned into a vector with an embedding model.
Search the cache: the vector is compared against stored query vectors, usually with cosine similarity or cosine distance.
Apply a threshold: if the closest match is within a configured distance, it counts as a cache hit.
Return or generate: on a hit, the stored response is returned immediately. On a miss, the LLM is called and the new prompt-response pair is written to the cache.
If embeddings are new to you, our explainer on what embeddings are and why AI search and RAG depend on them covers the foundation this technique is built on.
Semantic caching vs. prompt caching vs. exact caching
These three ideas are often confused, but they solve different problems:
Exact-match caching stores responses keyed by the literal prompt. It is cheap and safe, but only helps when requests repeat word for word — common in batch jobs and automated pipelines, rare in chat.
Prompt caching is a provider-side feature: the model provider reuses the computation for a repeated prompt prefix (a long system prompt or document) and charges less for those tokens. The model still generates a new answer each time. Our article on how prompt caching works goes deeper.
Semantic caching runs in your own stack and can skip the model call entirely when a similar enough question was already answered.
In practice, mature AI applications often combine them: exact caching first, semantic caching second and provider prompt caching on whatever still reaches the model.
How AI engineers implement a semantic cache
Pick a vector store
The cache needs fast nearest-neighbor search over embeddings. Teams typically reuse infrastructure they already run, such as Redis, PostgreSQL with a vector extension or a dedicated vector database. Redis, for example, ships a SemanticCache class in its RedisVL library; its LLM cache guide shows how to configure the index, an embedding model, a distance_threshold, time-to-live policies and filters for multi-user scenarios.
Use a library or build it
Open-source projects such as GPTCache package the embed-search-compare loop and integrate with frameworks like LangChain and LlamaIndex. Building a minimal version yourself is also straightforward: an embedding call, a vector search and a threshold check wrapped around your LLM client. Many teams place this logic inside an AI gateway so every application gets caching without reimplementing it.
Tune the similarity threshold
The threshold is the most important setting. Too loose, and the cache returns answers to questions that only look similar (“How do I cancel my order?” vs. “How do I cancel my subscription?”). Too strict, and the hit rate drops so low that the cache barely helps. The right value depends on the embedding model and the domain, so engineers tune it against a labeled set of real query pairs rather than guessing.
Scope the cache correctly
A cached answer must be valid for whoever receives it. Responses that depend on the user’s account, permissions, language or plan should be partitioned with metadata filters or tags, so one customer never receives an answer generated from another customer’s data.
Expire stale answers
Prices, policies and product features change. A time-to-live (TTL) on each entry, plus the ability to invalidate entries when source content is updated, keeps the cache from serving yesterday’s truth.
When semantic caching works well (and when it doesn’t)
It is a strong fit for:
Customer support and FAQ assistants where many users ask the same questions.
Internal knowledge assistants answering policy or how-to questions.
Classification or extraction tasks where similar inputs should produce the same output.
It is a poor fit for:
Personalized or time-sensitive answers (account balances, order status, live data).
Creative generation, where users expect variety.
Multi-turn conversations, where the meaning of a message depends on everything said before it — unless the cache key includes that context.
How to measure whether it is worth it
Treat the cache like any other production component and instrument it. Useful metrics include hit rate, latency on hits vs. misses, cost saved per day (avoided model calls minus embedding and storage costs) and, most importantly, the rate of incorrect hits found through sampling or user feedback. A cache that saves money but returns wrong answers is a liability, not an optimization.
FAQ
Does semantic caching replace RAG?
No. RAG retrieves documents to help the model write a new answer; semantic caching retrieves a previous answer to skip the model. They can work together: cache the final responses of a RAG pipeline for frequent questions.
Does it add latency on cache misses?
Slightly, because every request needs an embedding and a vector search before the model call. In most applications that overhead is small compared with LLM generation time, but it should be measured.
Is semantic caching safe for sensitive data?
It can be, if entries are scoped by user or tenant, sensitive fields are excluded or redacted before caching, and TTLs limit how long data is kept.
Keep learning with Coderhouse
Making AI applications fast, affordable and reliable in production is a skill that cuts across AI, data and go-to-market roles. Explore the three programs:
Forward Deployed AI Engineering Course — learn to build and deploy AI agents and applications that connect to real business tools, data and production constraints.
Data Analytics Course — learn SQL, Python and dashboards to track hit rates, costs and performance with data.
GTM Engineer Course (waitlist) — join the waitlist for early access to the program focused on automated go-to-market systems.
I'm Dan Patiño, head of AI Strategy & Innovation at Coderhouse. My day-to-day work involves merging the tactical management of e-commerce (CRO, Email Marketing and SEO) with the development of disruptive solutions. I specialize in building internal AI-powered apps to automate tasks and boost innovation within the team. I firmly believe that technology is strategy's best ally. To dive deeper into my professional journey, I'll be waiting for you on my LinkedIn profile.