Why LLM Inference Costs Keep Falling: A Deep Dive into KV Cache
This article explains how KV Cache reduces the cost of large language model inference by eliminating repeated attention computations during autoregressive generation. It covers how the cache works, how to estimate its GPU memory footprint, why high cache hit rates create a step-change in marginal cost, and the conditions and practical practices that help teams improve hit rates and control inference spending.
1. The Technical Logic Behind Falling Inference Prices
A major class of LLM inference services recently announced a permanent price cut to roughly one quarter of the previous level, and the community discussion heated up quickly. Treating this as nothing more than a marketing campaign, however, misses the more valuable technical signal: the room to lower the cost per token comes largely from better resource utilization on the inference side, and the single most important piece of that is the design and hit efficiency of KV Cache.
This article does not evaluate any specific vendor’s pricing strategy. Instead, it brings the discussion back to a general engineering question: why can cache hit rates approach saturation, what level are they actually at, and how should users and builders optimize their own architecture and cost model accordingly.
2. The Repeated-Computation Problem in Autoregressive Generation
Text generation is an autoregressive process: each step produces a single token, which is appended to the sequence before the next step is generated. Without caching, step N has to recompute the intermediate tensors required for attention across all N historical tokens.
An illustration of the process:
step1: [Today weather] -> isstep2: [Today weather is] -> nice # K/V for the first 3 tokens recomputedstep3: [Today weather is nice] -> , # K/V for the first 4 tokens recomputed again
In the attention mechanism, every token produces three vectors: Q (query), K (key), and V (value). The K and V of historical tokens never change in later steps, yet they are recomputed over and over. This is the largest source of redundant work during inference.
3. How KV Cache Works
The core idea is straightforward: after each new token is computed, write its K and V into GPU memory. On the next inference pass, the historical portion is read directly; only the newest token gets a full Q, K, and V computation. The new Q is then scored against all cached K, and multiplied by the cached V to produce the output.
As a result, the work per step drops from “process the entire sequence” to “process one new token.” Complexity shifts from the quadratic behavior tied to sequence length to a linear accumulation. This is also the fundamental reason long-context scenarios are viable at all.
4. Quantifying the GPU Memory Footprint
The cache is not free; it trades GPU memory for computation. The general estimation formula is:
KV Cache = 2 × layers × heads × head_dim × sequence_length × bytes_per_value
For 32 layers, 32 heads, a head dimension of 128, a sequence length of 4096, and FP16 (2 bytes):
Per token, per layer: 2 × 32 × 128 × 2 = 16384 bytes = 16 KBTotal: 16 KB × 32 layers × 4096 tokens ≈ 2 GBWith batch=10: ≈ 20 GB
The conclusion is direct: with long documents or large batches, the cache footprint can exceed the model parameters themselves and become the real bottleneck. If you would rather not do the math by hand, general GPU memory estimation tables and calculator tools are available in the industry.
5. Why a High Hit Rate Is a Qualitative Shift
A hit rate approaching saturation means the prefixes of the vast majority of requests are reused, and repeated computation is almost entirely eliminated. This is not a linear optimization but a change in magnitude that pushes marginal cost down to an extremely low level. It is the source of the “rounds down to free” experience, and the engineering basis for structurally lower prices.
6. Key Conditions That Support a High Hit Rate
- Prefix stability: system prompts, templates, and few-shot examples stay byte-for-byte identical.
- Cache capacity and eviction policy: sufficient GPU memory budget and sensible hot/cold tiering.
- Scheduling and routing: requests with the same prefix land on the same instance to avoid cache drift.
- Context organization: place variable content at the end to maximize the shared prefix length.
7. Practical Recommendations
- Fix the system prompt and put it first; move volatile fragments such as user input and timestamps to the end.
- Control the context length per request; segment and summarize long documents first.
- Set a reasonable batch size to balance throughput against cache footprint.
- Build a monitoring and alerting dashboard for hit rate, time to first token, and GPU memory levels.
- Use general-purpose logging and monitoring/alerting capabilities for root-cause analysis instead of tuning parameters by feel.
In general engineering practice on platforms such as Baidu AI Cloud, cache-friendly context organization, batch tuning, and observability are all routine means of controlling inference costs. Understanding KV Cache is, in essence, understanding how an LLM inference bill is composed.
