Semantic Response Cache in Front of LLM Calls

Reuse prior answers for near-duplicate prompts using embedding similarity with a strict threshold and TTL.

cachingembeddingscostlatency
Setup time1-2 hours (estimate)
Estimated costOne embedding per request plus vector storage; saves generation cost on hits.
ToolsPython
VerificationNot yet independently verified

Steps

  1. Normalize the prompt (trim, lowercase for matching only) and include model + system prompt hash in the cache key scope.
  2. Exact-match lookup first (hash). On miss, embed the prompt.
  3. Nearest-neighbor search within the same scope; accept only if cosine similarity ≥ threshold.
  4. On miss, call the model and store (embedding, prompt, response, created_at).
  5. Expire entries by TTL; never cache responses that depend on time or user-specific data.

Config

semantic_cache:
  exact_match_first: true
  similarity_threshold: 0.95   # start strict; lower only after reviewing false hits
  ttl_hours: 24
  scope_by: [model, system_prompt_hash, tool_set_hash]
  skip_if: [has_tools_with_side_effects, temperature_gt_0_5, user_specific_context]

LiteLLM and GPTCache both offer built-in semantic caching if you prefer not to build it.

Failure modes

Markdown version: /blueprints/semantic-cache.md

Want a deeper build of this?

Vote for an extended, fully tested version (more tools, a full workflow file). Votes decide what we build next.

– votes

Get one fully built blueprint a week

A weekly email: one blueprint with config, cost, setup time and failure modes, plus a vote on what we build next.

Related blueprints