Caching Strategies That Actually Reduce Inference Costs
Caching is the most powerful lever for reducing AI inference costs, but it only works when applied to the right workloads. Learn when caching helps, what to cache, and how to build a cache that pays for itself.
Caching is the most powerful lever for reducing AI inference costs, but it only works when applied to the right workloads. A well-designed cache can cut your inference bill by 70% or more. A poorly designed cache can add latency, complexity, and stale responses without saving a dime. The key is understanding which patterns repeat, what to cache, and when to invalidate.
When caching makes sense
Not all AI workloads benefit from caching. Caching is effective when the same or nearly identical requests are likely to recur, and when the cached response remains useful even if it becomes slightly stale.
The best candidates are deterministic or semi-deterministic queries: classification tasks, entity extraction, code completion for common patterns, FAQ answers, and data lookups.
note: Caching is not a silver bullet
If every request is unique (custom image generation, personalized chat with long context, real-time data analysis), caching will not help. Apply caching to the right workloads.
Types of AI caches
Different cache strategies serve different purposes. Choose the right one based on your workload characteristics.
- Exact match cache: Stores the full input and output. Returns the cached output when the input matches exactly. Best for repeated identical queries.
- Semantic cache: Uses vector similarity to match semantically equivalent queries. Handles paraphrased inputs but is more expensive to compute.
- Prefix cache: Caches the tokenization and attention for common prompt prefixes (system prompts, instructions). Saves compute without caching the full response.
- Result cache: Stores the final generated output for a given query. Useful for FAQ bots and common information requests.
tip: Start with exact match
Exact match caches are simple, fast, and cheap. Start there. Semantic caches add complexity and cost that may exceed the savings for many workloads.
The cache hit ratio equation
The economic viability of a cache depends on three variables: the hit ratio, the cost of a cache lookup, and the cost of an inference call. Even a 30% hit ratio can be profitable if the cache lookup is cheap.
function cacheIsProfitable({
hitRatio,
cacheLookupCost,
inferenceCost
}: {
hitRatio: number
cacheLookupCost: number
inferenceCost: number
}) {
const withCache =
hitRatio * cacheLookupCost +
(1 - hitRatio) * (cacheLookupCost + inferenceCost);
const withoutCache = inferenceCost;
return {
savings: withoutCache - withCache,
roi: (withoutCache - withCache) / cacheLookupCost,
isProfitable: withCache < withoutCache,
};
}
const result = cacheIsProfitable({
hitRatio: 0.42,
cacheLookupCost: 0.00001,
inferenceCost: 0.003,
});Cache invalidation strategies
A stale cache is worse than no cache at all. If a user receives outdated information, the cost of that mistake can exceed the savings from skipping an inference call.
- Time-based TTL: Expire entries after a fixed duration. Simple but can waste resources if the TTL is too long, or miss savings if too short.
- Content-based invalidation: Invalidate when source data changes. More precise but requires tracking data provenance.
- Version tags: Include a version in the cache key. When the underlying data or model changes, bump the version.
- Stale-while-revalidate: Serve stale data immediately while refreshing the cache in the background. Balances freshness with availability.
warning: Never serve stale data to the wrong context
If a cache entry contains user-specific information, include the user ID in the cache key. A common mistake is caching a response keyed only on the query, leaking one user's data to another.
Cache sizing and eviction
An unbounded cache eventually exhausts memory. The eviction policy determines which entries survive when the cache is full.
tip: LRU is a good default
LRU (Least Recently Used) eviction is simple, effective, and widely supported. Unless you have a specific access pattern that favors another policy, start with LRU.
start(Cache full)
policy:Eviction policy
lru:LRU
lfu:LFU
ttl:TTL
end(Cache has room)
start -> policy
policy -> lru -> end
policy -> lfu -> end
policy -> ttl -> endMonitoring cache performance
A cache that you cannot measure is a cache you cannot optimize. Track these metrics:
- Hit ratio: Percentage of requests served from cache. Target 40%+ for exact match caches.
- Latency split: Compare p95 latency for cached vs. uncached requests.
- Memory usage: Track cache size over time and set alerts for unexpected growth.
- Staleness incidents: Log and alert when a cached response is served that is older than the TTL threshold.
note: Cache hit ratio alone is not enough
A 90% hit ratio with a 500ms average cache lookup time is worse than a 40% hit ratio with a 1ms lookup. Always factor in the cost and latency of the cache itself.