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.

Response caching (exact & semantic)Diagram with these labels, in reading order: Submit Prompt; Exact Cache Lookup; Semantic Cache Lookup; Return Cached Response; User Prompt; (Exact/Semantic); Hash/Embed Prompt; Key-Value Store; (Redis); Similarity Search; (Threshold); Cached Response.Submit PromptExact Cache LookupSemantic Cache LookupReturn Cached ResponseReturn Cached ResponseUser Prompt(Exact/Semantic)Hash/Embed PromptKey-Value Store(Redis)Similarity Search(Threshold)Cached Response
Response Caching Mechanism

Exact key versus embedding lookup

The two modes differ in how a hit is decided.

ModeLookupHit condition
ExactHash of the normalized prompt plus parameters, looked up in a key-value store such as RedisSame hash
SemanticEmbed the query with a model such as OpenAI's embeddings or Sentence Transformers, then run a vector similarity searchSimilarity 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.

Tune the threshold A loose similarity threshold produces false positives. Set it deliberately and test it against your own prompts.
  • 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.

Response caching (exact & semantic)Diagram with these labels, in reading order: Response Cache Hit Rate; Exact Caching Issue; Semantic Caching Issue; Check Cache Key Generation; Review Semantic Similarity; Threshold; Implement Improved Key; Generation Logic; Adjust Similarity; Algorithm Parameters.Response Cache Hit RateExact Caching IssueSemantic Caching IssueCheck Cache Key GenerationReview Semantic SimilarityThresholdImplement Improved KeyGeneration LogicAdjust SimilarityAlgorithm Parameters
Debugging response caching failure

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.

Back to the knowledge baseRequest an assessment