Semantic Cache for AI Apps: Serving Repeat Questions From Memory

A semantic cache matches repeat questions by meaning and serves stored answers in milliseconds. This guide covers GPTCache, thresholds and setup.

By Galactic News Editorial · Published 2026-11-22T01:53:36+00:00 · Updated 2026-11-22T01:53:36+00:00

Semantic cache AI apps repeat questions memory guide

Semantic Cache for AI Apps: Serving Repeat Questions From Memory

Most production AI apps answer the same questions again and again. FAQ bots hear near-identical questions from hundreds of users. Support chatbots repeat the same policy answers daily. A semantic cache catches those repeats. It matches a new question to a stored answer by meaning, not by exact wording. When it hits, the app returns the stored response in milliseconds and never calls the model. This guide explains how semantic caching works, when it pays off, and how to build one.

What a Semantic Cache Is

A semantic cache sits between your app and the language model. Every incoming query is converted into a vector embedding. That embedding is compared against embeddings stored from earlier queries. If the similarity score clears a threshold, the cached response comes back. If not, the query goes to the model and the new response is stored for next time.

That is different from a normal cache. A normal cache matches exact strings only. "How do I reset my password?" and "I forgot my password, what do I do?" are different strings. A semantic cache sees that they mean the same thing. That is why it catches far more repeats than an exact-match cache.

It is also different from prefix caching. Prefix caching stores the computed tensors for repeated prompt prefixes inside the inference engine. Semantic caching works at the application layer and catches repeated intent across users. The two complement each other. A well-built system uses both.

How Much It Saves

Industry guides put 30 to 40 percent of production LLM requests as semantically similar to earlier ones. A research paper on GPT semantic caching reported API call reductions of up to 68.8 percent across query categories, with cache hit rates from 61.6 to 68.8 percent. Treat those numbers as a best case from a research setup, not a promise for your traffic. Your hit rate depends on how repetitive your users are.

Latency drops too. A cache hit can return in a few milliseconds. A model call takes hundreds of milliseconds to seconds. For FAQ traffic and agent workflows, guides report typical hit rates of 30 to 70 percent. The savings are real when traffic is repetitive. They are small when every question is unique.

GPTCache: The Standard Starting Point

GPTCache is the most widely used open-source semantic caching library for language models. It is a Python package from Zilliz. Install it with pip install gptcache. It offers the same interface as LLM APIs, so existing OpenAI calls can run through it with minimal code changes. It integrates with LangChain and LlamaIndex.

GPTCache has a modular design. Six components make up the system: an adapter, a pre-processor, an embedding generator, a cache manager, a similarity evaluator and a post-processor. You can swap each part. It supports SQLite by default and other stores for scale. Vector stores include Milvus, Zilliz Cloud and FAISS. It also supports distributed caching through Redis or memcached.

The basic setup looks like this:

from gptcache import cache
from gptcache.adapter import openai
from gptcache.embedding import Onnx
from gptcache.manager import CacheBase, VectorBase, get_data_manager
from gptcache.similarity_evaluation.distance import SearchDistanceEvaluation

cache.init(
    embedding_func=onnx.to_embeddings,
    data_manager=data_manager,
    similarity_evaluation=SearchDistanceEvaluation(),
)
cache.set_openai_key()

After that, OpenAI calls run through the cache automatically. GPTCache measures three things: false positives on hits, false negatives on misses, and hit latency. Watch all three. A fast cache that returns wrong answers is worse than no cache.

Choosing a Similarity Threshold

The threshold is the most important setting. It decides when two questions are close enough to share an answer. Set it too low and the cache returns wrong answers. Set it too high and it rarely hits. Guides suggest starting FAQ bots at 0.90 to 0.93 cosine similarity. GPTCache uses distance instead. Its default evaluation uses cosine distance, where 0 is identical and 1 is unrelated. Most teams start around 0.15 to 0.25 distance and tune from there.

Tune with real data. Take a sample of your traffic. Label pairs of questions as same-answer or different-answer. Test thresholds against those labels. Measure the false positive rate. A false positive in a support bot is a wrong answer to a real user. That costs more than a cache miss.

Three Caching Layers, Not One

LayerWhere it runsWhat it storesWho manages it
Semantic cacheApplication layerFull responses, indexed by query embeddingYour code or GPTCache
KV cacheGPU memoryAttention key-value tensorsInference framework
Prompt cacheInference frameworkComputed tensors for shared prefixesvLLM or SGLang

These layers stack. The semantic cache catches repeated intent first. The prompt cache cuts cost on everything that gets through. The KV cache speeds up generation. Teams with a consistent system prompt report 20 to 40 percent compute reduction from prefix caching alone. Run all three where you can.

When a Semantic Cache Is the Wrong Tool

Semantic caching fits stable, repeated questions. It fits badly where answers change fast. A cache that serves yesterday's price or policy is a liability. It also fits badly where answers must be personalised. A response written for one user's account should never be served to another user. Keep personalised queries out of the cache entirely.

Time-sensitive data needs expiry. Set a time-to-live on cached entries. Short TTLs for prices and stock levels. Longer TTLs for stable policy answers. Invalidate entries when the underlying facts change. A cache without invalidation is a stale-answer machine.

Privacy matters too. Cached queries and responses may contain user data. Store them under the same access controls as your logs. Do not cache queries with personal information unless your policy allows it.

A Practical Build Plan

  1. Measure first. Log a week of queries. Count how many are near-duplicates of earlier ones.
  2. Pick the embedding model. A small local model keeps costs down. A hosted embedding API is simpler to start.
  3. Set the threshold high. Start at 0.90 cosine similarity or 0.15 distance. Loosen it only with evidence.
  4. Cache only safe queries. Exclude personalised, time-sensitive and regulated content.
  5. Log every hit and miss. Track hit rate, latency and user complaints.
  6. Review false positives weekly. Read a sample of cached answers served to users.
  7. Add TTLs and invalidation before scaling to more traffic.

Start with one endpoint. An FAQ bot is the easiest win. Expand to support chat once the numbers hold up. Do not cache across users until you have tested that personal data never leaks between them.

Bottom Line

A semantic cache serves repeat questions from memory instead of paying for the same answer twice. GPTCache is the standard open-source starting point, with Redis and Milvus as the scale-up path. Set the threshold high, cache only safe and stable content, and measure false positives before you celebrate the hit rate. For repetitive traffic, it is one of the cheapest wins in AI app engineering. For unique or fast-changing traffic, skip it and spend the effort elsewhere.