Blog
4 min readbuild-in-publicperformancecaching

Cutting AI Latency and Cost with Semantic Caching

How semantic caching cuts AI latency and cost: reuse answers for questions that mean the same thing via embedding similarity, with the trade-offs.

Visitors ask the same things in different words, so we cache answers by meaning rather than by exact text: when a new question is semantically close enough to one we have already answered, we serve the cached response — cutting both latency and model cost on the most common queries. Semantic caching is one of the highest-leverage optimizations in a RAG system.

Why exact-match caching fails for chat

A traditional cache keys on the exact input, which works for URLs and API calls but barely helps for natural-language questions. "How do I reset my password" and "I forgot my password, what now" mean the same thing but share few words, so an exact-match cache treats them as different and recomputes both. Since real visitors phrase the same handful of common questions in endless variations, exact-match caching catches almost none of the repetition that actually exists. The repetition is semantic, not lexical, so the cache has to be too.

Caching by meaning

info

We embed each incoming question into the same vector space we use for retrieval, then compare it against the embeddings of recently answered questions. If the cosine similarity clears a high threshold, the questions mean the same thing, and we return the cached answer instead of calling the model.

Because we already compute query embeddings for retrieval, the marginal cost of the similarity check is tiny — one extra comparison before the expensive part of the pipeline. When it hits, we skip the model call entirely, which removes the largest source of latency in the request and the largest source of per-query cost. On the common questions that dominate real traffic, that adds up fast.

The threshold is everything

The whole design lives or dies on the similarity threshold. Set it too low and the cache returns a stored answer for a question that is merely related, not equivalent — a correctness bug that erodes trust. Set it too high and the cache almost never hits, giving back none of the benefit. So the threshold is deliberately conservative: we only reuse an answer when we are confident the two questions are genuinely the same, accepting a lower hit rate in exchange for never serving a subtly wrong cached answer. In a grounded system, correctness is the product, so we tune toward safety.

What it does and does not solve

Semantic caching shines on high-frequency, stable questions — the "how do I" and "what is your policy" queries that repeat all day and whose answers do not change minute to minute. It is less suited to questions whose answers are time-sensitive or visitor-specific, which we exclude from caching so freshness is never compromised. Used with that discipline, it is close to free performance: the common path gets dramatically faster and cheaper, the uncommon path is untouched, and correctness is preserved by a conservative threshold. It is the kind of optimization that improves the experience and the unit economics at the same time.

Getting started

Experience a fast, grounded agent on the AIML.chat free plan: index your content and ask the common questions twice. Read AI agent latency and semantic caching, what is RAG, and compare tiers on the pricing page.

How is semantic caching different from normal caching?+

It keys on meaning, not exact text. Two differently worded questions that mean the same thing hit the same cache entry, which exact-match caching misses entirely.

How do you avoid serving wrong cached answers?+

A conservative similarity threshold — we only reuse an answer when we are confident the questions are genuinely equivalent, accepting fewer hits to protect correctness.

What questions should not be cached?+

Time-sensitive or visitor-specific ones, whose answers change. Those are excluded so freshness is never compromised; caching targets stable, high-frequency queries.

Get the AIML.chat newsletter

Practical tips on AI docs assistants, RAG, and growing with content — a couple of emails a month. No spam, unsubscribe anytime.

Double opt-in — we'll email you to confirm.

Add AI chat to your site

Free plan · No credit card · 5 minutes

Get started free