When you use a large language model (LLM), you're not just paying for computation—you're paying for a hidden memory called the KV cache. This unsung hero of AI inference determines how fast your chatbot responds, how much context it can remember, and ultimately, how much your bill comes to.
Why LLMs Need Memory
Every time you prompt an LLM, the model must process your input and generate a response. But it doesn't start from scratch each time. Instead, it keeps a scratchpad—the Key-Value (KV) cache—that stores intermediate calculations from previous tokens. This cache allows the model to reuse computation, speeding up generation. Without it, generating each new token would require reprocessing the entire conversation history, which would be prohibitively slow and expensive.
The Two Speeds: Prefill and Decode
The KV cache creates two distinct phases in AI interaction. The prefill phase processes your input prompt in parallel, quickly filling the cache. Then the decode phase generates tokens one by one, reading from the cache. This is why the first response might feel fast (prefill) but subsequent words trickle out (decode).
When the Cache Grows Bigger Than the Model
In a long conversation, the KV cache can balloon to enormous sizes—sometimes exceeding the model's own parameters in memory footprint. For a 70-billion-parameter model, a lengthy chat might require tens of gigabytes of cache. This memory demand is a primary driver of cost, as cloud providers charge for the GPU memory your session occupies.
How Prompt Caching Affects Your Bill
Many AI services now offer prompt caching, where the system reuses the KV cache from common prefixes. This reduces latency and cost for repeated queries, but the cache still consumes resources. When you're billed per token or per session, a large cache means higher costs—especially if your conversation is long or includes many shared system prompts.
Shrinking the Cache: Techniques to Save Money
Researchers and engineers have developed methods to tame the KV cache:
- Grouped Query Attention (GQA): reduces the number of key/value heads, shrinking cache size.
- Multi-Query Attention (MQA): shares keys and values across all heads.
- Multi-Head Latent Attention (MLA): compresses the cache into a smaller latent space.
- vLLM: a library that efficiently manages the cache using PagedAttention.
- Quantization: reduces the precision of cached values to lower memory.
These techniques can cut cache memory by 50–75%, leading to significant cost savings.
The Future: Sharing, Offload, and No-Cache Models
Innovations are on the horizon to further reduce costs:
- Shared caches allow multiple users to reuse portions of the cache from similar prompts.
- CPU offload moves less critical parts of the cache to system memory.
- No-cache models aim to eliminate the need for a large KV cache entirely, though they are experimental.
Understanding the KV cache is key to controlling your AI spending. By choosing models and services that optimize cache usage, you can get faster responses and lower bills.