AI
Response Caching Strategy Cuts LLM Costs by Over Half—Here's How
A two-tier caching approach combining exact-match and semantic search can dramatically reduce token spending on repeated LLM queries without sacrificing accuracy.
The New Stack
Billing happens per token in most LLM deployments, which means identical or near-identical requests trigger duplicate charges. A straightforward but often-overlooked solution lies in response caching—storing answers already computed and reusing them when similar questions arrive—borrowed from techniques refined in data engineering over decades.
The economic incentive is substantial. For a hypothetical workload processing 1 million monthly API calls at $0.006 per call, a hybrid caching approach delivering a 60% hit rate could cut monthly spend from $6,000 to roughly $2,550 while also slashing response latency.
Two-Tier Caching Architecture#
The simplest layer is exact-match caching. Normalize the request (model settings, context, documents, user permissions), hash it with SHA-256, then look it up in a fast store like Redis. An exact match means returning the cached answer instantly, skipping model inference entirely. This works best for bounded, predictable requests: batch jobs, CI pipeline runs, boilerplate summarization.
The second layer addresses cases where queries aren't identical but are semantically similar. Embed the incoming query, search a vector database for close neighbors, and return a match if it clears a similarity threshold. The cosine similarity bar matters enormously: code-like queries typically need thresholds around 0.95+ (small wording shifts change meaning), while conversational queries tolerate looser thresholds (0.85–0.90). These numbers are starting points; real tuning depends on your embedding model and actual traffic.
A hybrid approach checks exact-match first, falls back to semantic search, and promotes semantic hits back into the exact store so future identical paraphrases hit instantly.
Freshness, Scope, and When to Skip#
Both cache tiers must key on more than query text alone: context and documents in the prompt, model version, caller permissions, and any versioned sources. Two identical questions about different documents or by users with different access must never share a cache entry.
Time-to-live (TTL) should track how quickly answers go stale. Internal HR policies may cache for weeks; market data for minutes or seconds. Live inventory and stock prices during active trading may be unsafe to cache at all. Shadow-mode testing (logging what you'd return without changing behavior) helps validate cached answers before trusting them in production.
Skip caching entirely for requests with personal or account-specific data (leakage risk), creative tasks (where variation is desired), and genuinely real-time data where staleness of even a minute matters.

