Why LLM Inference Is So Cheap: KV Cache and Cost Optimization
LLM API prices keep falling. This article explains autoregressive generation, how KV Cache avoids redundant attention work, GPU memory cost of the cache, and why prefix cache hits drive down per-token inference cost.
Why LLM Inference Is So Cheap: KV Cache and Cost Optimization
LLM API prices have hit new lows. After some inference services permanently cut input-token pricing, fees dropped to about a quarter of the previous rate. Many developers ask how providers still make money at that level. A large part of the answer is KV Cache and cache hits. This article starts from the inference loop, then unpacks KV Cache, its memory footprint, and why hit rate can dominate cost.
Autoregressive generation: where the repeated work comes from
Text generation in large language models is autoregressive: each step produces one token, appends it to the sequence, and continues. For the input “the weather today”:
- Step 1: input “the weather today”, the model outputs “is”;
- Step 2: input “the weather today is”, the model outputs “good”;
- Step 3: input “the weather today is good”, and so on.
Attention makes this expensive if implemented naively. Each token in each layer produces Q (query), K (key), and V (value). Attention scores come from the current Q dotted with every K in the sequence, then a weighted sum of V.
In step 2, the K and V for the earlier tokens were already computed in step 1 and are identical, but a naive pass recomputes them. Every new token repeats work over the full history, so cost grows quadratically with sequence length.
KV Cache: trading memory for compute
KV Cache is straightforward: after each new token, store that token’s K and V per layer in GPU memory. On the next step, reuse cached K and V for history. Only the newest token needs full Q, K, and V. The new Q attends to all cached K, then weights the cached V.
Per-step work drops from “the whole sequence” to “the latest token,” which is why interactive chat inference is practical. Without KV Cache, conversational serving would not look like it does today.
The tradeoff is GPU memory, which is already scarce for inference.
KV Cache memory: a formula you can compute
A common estimate:
KV Cache size = 2 (K and V) × layers × attention heads × head dim × sequence length × bytes per element
Example: 32 layers, 32 heads, head dim 128, sequence length 4096, FP16 (2 bytes):
- KV per token per layer: 2 × 32 × 128 × 2 = 16384 bytes = 16 KB; times 32 layers → 512 KB per token;
- 4096 tokens: 512 KB × 4096 ≈ 2 GB;
- 10 concurrent requests: about 20 GB.
For long context or high concurrency, KV Cache can exceed the weight footprint and become the real bottleneck. Industry responses include grouped-query attention (shared KV heads), quantized caches (lower precision for K and V), and sliding-window or local attention (limited cache span). All of them trade speed, memory, and quality.
Cache hits: the economics of cheaper inference
KV Cache removes repeated work inside one request. At serving scale, many requests share a prefix: a customer-support bot with a fixed system prompt, or batch jobs that share a long document or code context.
If the server keeps computed prefix KV (reliable hits), a new request only pays compute for tokens after the prefix. The shared prefix is almost free computationally, with modest storage and read cost. Higher hit rate means lower average cost per token.
That is one reason some providers can cut prices sharply: when traffic has stable prefixes, repeated instructions, or multi-turn shared context, marginal compute can be very low. Practical takeaways:
- Stable prefixes: Put system prompts, shared rules, and document background at the start of the prompt, byte-for-byte identical, so wording changes do not miss the prefix.
- Reuse multi-turn context: Carry history as-is to help prefix cache hits.
- Watch billing splits: Compare cached vs. uncached token prices and put reusable content first to cut the bill.
- Cost is more than list price: Hit rate, throughput, and latency jointly determine what you actually spend.
Summary
Falling inference prices come from stacked engineering: KV Cache, cache reuse, attention variants, and quantization. Knowing how KV Cache works and what it costs in memory helps you size deployments and design prompts that cooperate with caching, so the same budget buys more useful tokens. The gains go to people who understand the mechanism and use the rules.
