ChangeGamer

← All resources

Application-Level Response Caching for AI Agents

Guide · updated 2026-06-25 · Markdown variant

How to implement exact-match and semantic caching in your agent application to eliminate redundant LLM calls, with threshold guidance, invalidation strategies, and a decision matrix for when semantic caching is unsafe.


Application-level response caching intercepts a request before it reaches the model provider and returns a stored response — eliminating the inference call entirely. This is distinct from provider-side prefix caching (see /resources/prompt-caching-for-agents), which only discounts repeated input tokens while still running inference. Application caching is code you own and deploy; it can cut cost and latency to near-zero for cache-eligible queries.

Exact-match caching

Hash the full request (model + messages + temperature + parameters) with a deterministic function such as SHA-256, and store the response under that key. A subsequent identical request hits the cache without a model call. Lookup is O(1) (~1–3 ms). False-positive risk: effectively zero. False-negative risk: any character change is a miss. Useful when your agent issues identical sub-task calls across sessions (e.g., the same review prompt on the same unchanged file).

Semantic caching

Embed the incoming query, run a vector similarity search against previously cached query embeddings, and if the nearest neighbour exceeds a cosine-similarity threshold, return the cached response. A miss calls the model and stores the response plus its embedding. Lookup is roughly single-digit milliseconds versus hundreds-to-thousands for a live call. For the embedding layer see /resources/embeddings-vector-search.

Cache type comparison

Cache type Mechanism Eliminates the call? False-positive risk
Exact-match SHA-256 hash of the full request Yes, for identical requests None
Semantic Embed query → similarity search → return if above threshold Yes, for near-duplicate queries Low–medium (threshold-dependent)
Provider prefix caching Provider reuses KV state on shared input prefix No — input-token discount only None (provider-managed)

Similarity threshold tuning

The threshold is the single most consequential parameter. Start high — around 0.95 cosine similarity — and only lower it after backtesting on thousands of real queries while precision stays high. A low threshold (e.g. 0.85) will serve answers about one topic to questions about a superficially similar but different one. Domain-specific terminology needs higher thresholds than general FAQ content. Example: "Show DDA revenue by channel" and "Show GA4 revenue by channel" can score ~0.96 yet reference different attribution models — a naive threshold would serve the wrong answer. Source: portkey.ai/blog/semantic-caching-thresholds/.

When NOT to semantic-cache

Invalidation and staleness

Tooling

Relationship to other caching layers

These layers stack and are complementary: (1) application exact-match cache, (2) application semantic cache, (3) provider prefix caching (/resources/prompt-caching-for-agents — discounts input tokens; inference still runs). For the broader cost stack see /resources/agent-cost-latency-optimization.

Verified sources

#caching #semantic-cache #cost #latency #optimization #agents #embeddings

Category: Guide

Like this? See pricing for the full corpus license, or preview the exact format free as NDJSON or JSON.