← BlogAI Product Development

How Semantic Caching Cuts LLM API Costs: Embeddings, Cosine Similarity and Cache Thresholds Explained

By Aditya JhaAugust 4, 20269 min read

How Semantic Caching Cuts LLM API Costs: Embeddings, Cosine Similarity and Cache Thresholds Explained

A compliance team's AI assistant answers the same handful of regulatory questions a hundred different ways every week, 'what's our obligation under the new data localization rule', 'do we need to notify users under the localization update', 'is the new localization requirement mandatory for us', three phrasings, one actual question, and three full LLM calls billed at full price because the API only knows how to match exact strings, not meaning. By month three the bill has crept from a rounding error to a line item the founder has to explain, and the fix nobody reaches for first is the cheapest one: teach the cache to recognize a question it has already answered, even when the words are different.

Why exact-match caching misses most of the savings

A standard cache stores a response keyed to the exact request string, so it only ever fires when a byte-for-byte identical prompt comes in twice, which almost never happens in a real chatbot or agent, because two users, or the same user twice, rarely type a question the same way. Production LLM traffic is full of near-duplicates: semantic caching exists specifically because a meaningful share of that traffic is semantically identical queries phrased in different words, and exact-match caching structurally cannot see that overlap.

Semantic caching fixes this by keying the cache on meaning instead of text. Every incoming query is converted into an embedding, the same dense vector representation used in RAG retrieval, and compared against the embeddings of previously cached queries using cosine similarity. If a close-enough match exists, the cached response is returned instantly, no LLM call, no added token cost, and typically single-digit-millisecond latency instead of a multi-second round trip.

The actual mechanism: embeddings, cosine similarity and the threshold

  • Embed the incoming query with the same embedding model used to build the cache index, an inconsistent model here silently breaks similarity matching, since two different embedding models don't share a comparable vector space.
  • Compute cosine similarity between the new query's embedding and the vectors already stored in the cache, cosine similarity measures the angle between two vectors rather than their raw distance, which makes it robust to differences in phrasing length.
  • Compare that score against a configurable similarity threshold, typically set between 0.85 and 0.95, above the threshold, serve the cached response; below it, treat the query as new and call the LLM.
  • Store the new query's embedding alongside the fresh response so the next semantically similar question hits the cache too, the cache gets more effective over time as it accumulates coverage of your actual query distribution.
  • Attach a time-to-live or an explicit invalidation hook tied to the underlying data, a cached answer about a policy that just changed is worse than no cache at all, so TTL and invalidation are not optional polish, they're the difference between a cost saver and a source of wrong answers.

Why the threshold is the whole game

Set the similarity threshold too low and the cache becomes a hallucination amplifier: 'what's our refund policy for annual plans' and 'what's our refund policy for lifetime plans' are semantically close but factually different questions, and a threshold that's too permissive will confidently return the wrong answer instead of calling the model. Set it too high and the cache barely fires, and you've added embedding-lookup latency for negligible savings.

The practical approach is threshold tuning against your actual traffic, not a default left untouched: log every near-miss (queries that scored just under the threshold), review a sample weekly, and adjust based on whether those near-misses were true duplicates or genuinely different questions that happened to sit close in embedding space. Higher-stakes domains, medical, legal, financial, compliance, warrant a stricter threshold and a bias toward a fresh LLM call over a cheap-but-wrong cached one.

What it actually saves

The numbers here aren't theoretical. VentureBeat documented a production case where a company's monthly LLM API bill dropped from $47,000 to $12,700, a 73% reduction, after adding a semantic caching layer, with cache hit rate climbing from 18% (roughly what exact-match caching alone achieved) to 67% once similarity matching was in place. Latency improved alongside cost, since a cache hit skips the model call entirely.

That range holds up across the broader literature too: Percona's benchmarking of semantic caching for LLM apps reports cost reductions in the 40-80% band and response speedups up to 250x on cache hits, depending on how repetitive the underlying query traffic actually is, a support bot or an FAQ-style compliance assistant sees far higher hit rates than a genuinely open-ended research agent.

Monthly LLM API spend before and after adding a semantic caching layer, one documented production case. Source: VentureBeat, 'Why your LLM bill is exploding, and how semantic caching can cut it by 73%'.
Monthly LLM API spend before and after adding a semantic caching layer, one documented production case. Source: VentureBeat, 'Why your LLM bill is exploding, and how semantic caching can cut it by 73%'.

Where semantic caching earns its keep, and where it doesn't

  • High-repetition, FAQ-shaped traffic (support bots, compliance Q&A, onboarding assistants): this is where hit rates climb into the 50-70% range and the savings are largest, because the same handful of underlying questions genuinely repeat.
  • Low-repetition, open-ended traffic (research agents, creative generation, one-off analysis): hit rates stay low because few queries are true semantic duplicates, so the embedding-lookup overhead may not be worth adding.
  • Anything time-sensitive or personalized (account balances, live pricing, user-specific data): cache with a short TTL or skip caching entirely, a stale cached answer here isn't a minor inefficiency, it's a wrong answer served with full confidence.
  • Pair it, don't replace it: semantic caching and prompt caching solve different problems, prompt caching cuts the cost of a repeated prefix within a call that still executes, semantic caching skips the call entirely, and production systems that care about cost typically run both layers together.

How AIBOOTSTRAPPER solved this for a client

ComplyNexus, a Hong Kong compliance platform we built, runs a RAG-powered engine that answers regulatory questions against a constantly updated control library, exactly the FAQ-shaped, high-repetition query pattern where semantic caching pays off, the same handful of obligations get asked about in dozens of phrasings across a compliance team. The platform cut manual review time by 92% and turned a three-week regulatory change turnaround into two hours; a caching layer tuned to that query pattern is a direct extension of the same discipline, keep the system fast and cheap at scale without letting a stale cache slip an outdated regulatory answer past an auditor.

If your AI product's API bill is climbing faster than usage justifies, AIBOOTSTRAPPER's AI product development team audits your actual query traffic, model calls and cache hit potential, then builds the caching and cost-reduction layer around what your data actually shows, not a generic default. Book a call to get a real breakdown of where your spend is going.

Want this done for you?

Book a free strategy call and we'll show you how to build and market your business with AI.

FAQ

Questions, answered

Everything you might want to know before we hop on a call.

Prompt caching (offered natively by providers like Anthropic and OpenAI) reduces the cost of reprocessing a repeated prefix, like a long system prompt, within a call that still runs. Semantic caching skips the LLM call entirely when a new query is similar enough in meaning to one already answered. They solve different parts of the cost problem and are often used together.

Most production setups start between 0.85 and 0.95 cosine similarity and tune from there based on logged near-misses. Higher-stakes domains (medical, legal, financial, compliance) should lean stricter and favor a fresh LLM call over risking a wrong cached answer.

Yes, if the threshold is too permissive or the cache lacks a TTL and invalidation hook tied to the underlying data. A cached answer to a policy question is only safe if it's invalidated the moment that policy changes, treat cache invalidation as a required part of the design, not an afterthought.

It depends on how repetitive your queries actually are, not just your traffic volume. A low-volume but highly repetitive FAQ bot can still see meaningful hit rates; a low-volume, open-ended research tool likely won't see enough duplicate queries to justify the added embedding-lookup step.

Keep reading

Let's talk

Ready to build and sell with AI?

Book a free 30 minute strategy call. We'll map the highest ROI AI move for your business, no pitch, just value.