Every time you call an LLM API, a hidden data structure grows silently in the background: the Key-Value (KV) cache. It stores the intermediate representations of every token in the context, so the model doesn't have to recompute them for each new token. But this cache is expensive—it consumes memory proportional to the square of the context length. And that cost, largely invisible to the user, is the single biggest factor in inference pricing. Understanding the KV cache is the first step to understanding why your API bills look the way they do.
The Invisible Cost Driver in Every LLM API Call
The transformer's self-attention mechanism computes a weighted sum over all previous tokens for each new token. To avoid recomputing these keys and values for every step, the model caches them. The cache grows linearly with the number of tokens and the number of attention heads, and quadratically with the hidden dimension. For a model like Llama 3 70B with 128K context, the KV cache can consume roughly 15 GB of VRAM per sequence.
Providers like OpenAI, Anthropic, and Google all face the same constraint: memory is the bottleneck. They bill by token, but the cache cost is hidden behind a single price per token. When you send a long prompt, the provider must allocate a large cache for your request. If the cache is evicted due to memory pressure, the next token generation suffers a cache miss, forcing a recomputation that increases latency and cost.
The cache miss penalty is significant. Some estimates put the cost of a cache miss at 2–5 times that of a cache hit, depending on model size and hardware. On an A100, a single cache miss might add roughly $0.002 to the inference cost—small per request, but devastating at scale. For applications with long context windows, the cache can dominate the total inference budget.
To illustrate, consider a customer support chatbot that processes 10 million requests per month with an average prompt length of 4,000 tokens. If the cache hit rate is 80%, the cost of cache misses alone could be around $4,000 per month on a mid-range GPU cluster. Improving the hit rate to 90% would save approximately $2,000 monthly. This is not hypothetical—companies like Intercom and Zendesk have reported similar savings after implementing prefix caching strategies.
How KV Cache Size Dictates Batch Throughput
Batch processing is the primary lever for GPU utilization. Providers pack multiple requests into a single forward pass to amortize the cost of model weights. But batch size is limited by the per-request cache footprint. An H100 with 80 GB of VRAM can hold only so many sequences before the cache reaches capacity. For a 128K context model, the batch size might drop to single digits.
vLLM's PagedAttention addresses this fragmentation by managing the KV cache in fixed-size blocks, similar to virtual memory paging in operating systems. This reduces wasted memory and allows larger batch sizes. FlashAttention takes a different approach, trading off memory for recomputation by recomputing parts of the attention matrix on the fly, reducing cache size but increasing compute.
There is a trade-off between memory savings and computational overhead. FlashAttention reduces the cache footprint by up to 50% but increases total FLOPs by about 10–20%. For latency-sensitive applications, the recomputation cost may offset the memory benefit. PagedAttention, on the other hand, can increase batch size by 2–4x without extra compute, but it introduces complexity in cache management and potential fragmentation overhead. The choice between these techniques depends on the workload: batch-heavy applications benefit from PagedAttention, while single-request low-latency scenarios may prefer FlashAttention.
The throughput cliff occurs when the aggregate cache footprint exceeds available VRAM. Beyond that point, the provider must either queue requests or spill cache to CPU memory, which is orders of magnitude slower. Some inference engines implement cache eviction policies, but these introduce unpredictable latency spikes. The net effect is that the cache size directly determines the maximum throughput a given GPU can sustain.
For example, a deployment serving Llama 3 70B on an A100 (80 GB) with 128K context can handle at most 5 concurrent sequences before hitting memory limits. With PagedAttention, this can increase to 12–15 sequences, improving throughput by 2–3x. This directly translates to cost savings—fewer GPUs needed to serve the same load.
Inference Providers Exploit Cache Sharing Gaps
One of the most effective cost-saving techniques is prefix caching: when multiple requests share a common prefix (e.g., a system prompt), the cache for that prefix can be reused. Anthropic's prompt caching feature explicitly exposes this, allowing developers to mark cache breakpoints. OpenAI's cached tokens are discounted by 50% as of late 2024, making prompt caching a key lever for reducing API costs.
But cache hit rates vary wildly by workload. A chatbot with a fixed system prompt may achieve a 90% hit rate, while a code completion service with diverse prompts may see only 30%. Providers do not expose cache hit metrics, so developers must estimate based on prompt patterns. There is no standard cache interface across providers—each has its own API headers, caching granularity, and pricing model.
This lack of standardization means that optimizing for one provider's cache often comes at the expense of another's. A prompt engineered for OpenAI's prefix cache may not benefit from Anthropic's prompt caching headers. The industry is still in an early stage where cache-aware API design is a competitive advantage, not a commodity.
Consider the case of a multi-provider application that routes requests based on latency. If the request is sent to a provider that does not have the prefix cached, the user pays the full price. A cache-aware router could check which provider has the prefix cached and send the request there, but this requires a shared cache key format—something that does not exist yet. This is an active area of research and development.
Prompt Engineering as Cache Optimization
Long system prompts waste cache on every call. By moving static instructions into the system prompt and reusing the same prefix across requests, developers can maximize cache locality. Some teams split prompts into a static prefix and a dynamic suffix, ensuring the prefix is always cached. Instruction tuning also helps: a model that follows instructions with fewer tokens reduces the overall cache footprint.
Few-shot examples, though useful for in-context learning, extend the KV cache linearly. For each example, the cache grows by the example's token count. Engineers increasingly optimize prompts not just for accuracy, but for cache efficiency—shorter examples, fewer shots, and careful ordering of tokens to maximize prefix reuse.
Anthropic's research suggests that prompt caching can reduce latency by up to 75% for long prompts, and OpenAI reports similar gains. The tradeoff is that cached prefixes are immutable: any change to the prefix invalidates the cache. This forces a deliberate design process where prompt changes are batched and deployed with cache warming in mind.
For instance, a team running an AI writing assistant might have a system prompt that includes instructions like "Write in a professional tone." If they need to update the tone to "friendly," the entire cache for that prefix is invalidated. To mitigate this, they could version the system prompt and gradually migrate users, warming the new cache over several hours. This approach minimizes the performance hit but requires careful planning.
Hedged Numbers: The Real Cost of a Cache Miss
Quantifying the exact cost of a cache miss is difficult because it depends on model architecture, hardware, and deployment configuration. For Llama 3 70B with 128K context, the KV cache occupies roughly 15 GB per sequence. On an A100 with 80 GB VRAM, a batch of five such sequences leaves only 5 GB for model weights and overhead. A cache miss forces recomputation of the full prefix, which can take several seconds and cost an additional $0.001–$0.003 in GPU time.
The H200, with HBM3e memory, reduces the miss penalty by providing higher bandwidth. But the fundamental economics remain: memory capacity is the bottleneck. Some estimates place the cost of serving a 128K token request at around $0.01–$0.03 per call, with the cache miss component accounting for 30–50% of that cost. These numbers are rough and vary by provider, but they illustrate the magnitude of the hidden cost.
For developers deploying LLMs at scale, the cache miss rate is a key metric to monitor. A 10% improvement in cache hit rate can translate to a 5–10% reduction in inference cost. This is why prompt engineering and cache-aware API design are not just performance optimizations—they are direct cost-saving measures.
To put this in perspective, a company serving 100 million tokens per day with a 20% cache miss rate might spend $1,000 per day on misses. Reducing the miss rate to 10% saves $500 daily, or $15,000 per month. These savings can fund additional model training or infrastructure improvements.
The Protocol Shift: Standardizing Cache-Aware APIs
In late 2024, OpenAI introduced a dedicated prompt caching API, allowing developers to specify cache keys and control cache invalidation. Anthropic followed with a similar feature using HTTP headers. Google's Gemini platform offers context caching for repeated prompts. Each of these APIs exposes caching as a first-class concern, but they are incompatible with each other.
The lack of a cross-provider cache key format means that developers must write provider-specific code to take advantage of caching. A future standard—perhaps based on content hashing or semantic similarity—could allow cache-aware routing, where a request is sent to the provider that already has its prefix cached. This would reduce costs and latency across the ecosystem.
Until such a standard emerges, the burden falls on developers to understand each provider's caching model and optimize accordingly. The VC fund that backs the npm package you already depend on might not care about cache hit rates, but for engineers building on LLM APIs, it's a daily concern. Similarly, the wire protocol that cut CDN latency shows how protocol-level changes can have outsized impact; cache-aware APIs could do the same for inference.
Practical Takeaways for Deploying LLMs at Scale
First, measure cache hit rate per endpoint. Most providers do not expose this directly, but you can estimate it by tracking request latency distributions—a bimodal pattern suggests cache misses. Second, design prompts with reusable prefixes. Move static instructions, persona descriptions, and formatting guidelines into a system prompt that is identical across requests.
Third, bulk requests with identical system prompts to maximize cache reuse. Batch processing not only improves throughput but also increases the likelihood that the cache for the common prefix is already warm. Fourth, choose a provider based on cache pricing. If your workload has a high prefix overlap, a provider that discounts cached tokens (like OpenAI's 50% discount) can be significantly cheaper.
Finally, monitor KV cache to forecast inference budget. As context windows grow, the cache footprint will dominate costs. Tools like vLLM's metrics dashboard can help track cache utilization. The index merge that cuts database write throughput by half is a reminder that hidden system details can have dramatic cost implications. The KV cache is the equivalent for LLM inference—understand it, or pay the price.
Counter-Arguments: When Cache Optimization Isn't Enough
Some argue that as hardware improves—with HBM3e, HBM4, and beyond—memory capacity will cease to be a bottleneck, making KV cache optimization less critical. However, context windows are also growing (e.g., 1M tokens in Gemini 1.5 Pro), which offsets memory gains. The ratio of cache size to memory capacity may remain roughly constant, so optimization will continue to matter.
Another counter-argument is that techniques like multi-query attention (MQA) and grouped-query attention (GQA) reduce KV cache size by sharing keys and values across heads. While this helps, it does not eliminate the cache cost entirely. For very long contexts, even a reduced cache can be large. For example, Llama 3 70B uses GQA with 8 key-value heads, but the cache still grows linearly with tokens.
Finally, some developers prefer to pay for compute rather than optimize prompts, arguing that engineer time is more expensive than GPU time. This is a valid trade-off for small-scale applications, but at scale, the math shifts. A team spending 10 hours per month on cache optimization might save $5,000 in GPU costs—a 50x return on investment. For large deployments, the optimization pays for itself many times over.
Future Directions: Semantic Caching and Beyond
Looking ahead, semantic caching could allow cache reuse even when prompts are not identical but semantically similar. For instance, a system prompt that says "Explain quantum computing" could share a cache with "Explain quantum mechanics" if the embeddings are close enough. This would dramatically increase hit rates for diverse workloads.
Another emerging idea is speculative caching, where the model predicts likely future prompts and precomputes their caches. This could reduce latency for interactive applications like chatbots. However, these techniques are still experimental and may introduce overhead that offsets their benefits.
In summary, the KV cache is the hidden engine of LLM inference economics. Understanding its behavior, measuring its impact, and optimizing for it are essential skills for any engineer deploying LLMs at scale. The providers are racing to offer better caching APIs, but until standards emerge, the advantage goes to those who master the cache.