Response caching stores prior LLM outputs for reuse. Exact caching keys on the literal prompt. Semantic caching returns a stored answer when a new prompt is close enough in meaning to a past one.
Exact key versus embedding lookup
The two modes differ in how a hit is decided.
| Mode | Lookup | Hit condition |
|---|---|---|
| Exact | Hash of the normalized prompt plus parameters, looked up in a key-value store such as Redis | Same hash |
| Semantic | Embed the query with a model such as OpenAI's embeddings or Sentence Transformers, then run a vector similarity search | Similarity above a set threshold |
An exact hit is very fast and cheap, and it hits only when the normalized prompt and parameters are identical. A semantic hit widens the set of queries that benefit, because the new prompt need not match a past one word for word.
Both modes need a TTL tuned to usage patterns, so that stored answers expire when they should.
False hits from loose similarity thresholds
Semantic caching matches on approximate meaning, so a hit is a similarity judgment. A hash cache has no equivalent risk. A threshold set too loose can return a stored answer that is subtly wrong for the new question.
- Use exact caching for deterministic, repeated prompts, such as document Q and A or classification.
- Use semantic caching for paraphrased repeat questions, and test the threshold first. Both modes can cut cost and latency on repeated traffic.
- For personalized, stateful or high-stakes queries, skip the cache or scope it per user.
A stored answer matched to a different prompt may not fit the person asking, so keep personalized prompts out of a shared cache.
Prompt caching of shared prefixes
Prompt caching is a related technique. It targets cost and time to first token when many prompts share a long prefix. Instead of storing whole responses, it stores the model's key-value state after processing the shared prefix. A continuation can then build on that state and skip reprocessing the prefix.
The software analogue is HTTP or CDN response caching, and memoization of expensive function calls. Without any caching, identical and similar prompts are regenerated each time, adding cost and delay for frequent questions.
Cache hit rate drop
Example scenario: the response cache hit rate falls to 45%. The figure is illustrative.
A lower hit rate means more requests are generated from scratch, so responses get slower. Likely causes are a keying strategy that produces inconsistent keys, stale entries, or a cache too small for the workload.
- Check the hit rate over the past hour.
- Review the key generation logic for inconsistencies.
- Review cache size and eviction policy.
Fixing key generation or raising cache size may trade memory against lookup speed. Monitor hit rate and average response time to confirm the change.