KV Cache Optimization for LLM Inference: The Key to Lower Costs
This article explains why LLM inference prices keep falling and identifies KV Cache optimization as the underlying technical driver. It covers how KV Cache works in autoregressive generation, how to estimate its memory footprint, five mainstream optimization approaches, and practical engineering recommendations for improving cache hit rates and reducing serving costs.
1. Behind the Trend: Why Inference Prices Keep Dropping
A major model provider recently announced a permanent price cut to one quarter of the previous rate for its flagship model, sparking broad discussion in the developer community. While much of the attention focuses on the price reduction itself, the more important question is: what technical foundation makes such sustained price cuts possible?
From an engineering perspective, LLM inference cost is primarily composed of three parts: GPU memory usage (which determines the concurrency limit), compute consumption (which determines single-request latency), and data movement overhead (which determines throughput). Among these, memory and compute are heavily influenced by a seemingly simple technique: KV Cache. When the cache hit rate is pushed to a very high level, the same hardware can serve multiple times more requests, and the marginal cost per token drops significantly. Price is only the result; cache reuse rate is the cause.
2. The Basics: Redundant Computation in Autoregressive Generation
Text generation is an autoregressive process. Each step produces a single token, which is then appended to the sequence for continued generation:
Step 1: Input [Today weather] -> Predict isStep 2: Input [Today weather is] -> Predict goodStep 3: Input [Today weather is good] -> Predict ...
In the attention mechanism, every token produces three vectors: Q (Query), K (Key), and V (Value). The problem is that when Step 2 computes the K and V for “Today” and “weather,” the results are identical to those from Step 1, which is purely redundant work. The longer the sequence, the more severe the waste, with overall complexity growing quadratically.
The core idea of KV Cache is to store the K and V of a token once it is computed. Subsequent steps only compute the Q, K, and V of the new token, use the new Q to score attention against all cached K, and then weight the cached V. This reduces the computation per step from “processing the entire sequence” to “processing a single token.”
3. A Quantitative View: How Much Memory Does KV Cache Use
The size of KV Cache can be estimated with the following formula:
KV = 2 × num_layers × num_heads × head_dim × sequence_length × bytes_per_value
For a model with 32 layers, 32 heads, a head dimension of 128, a sequence length of 4096, and FP16 precision: a single token in a single layer takes 2 × 32 × 128 × 2 = 16KB; the total is approximately 16KB × 32 × 4096 ≈ 2GB; with a batch size of 10, that is about 20GB. In long-context or high-concurrency scenarios, KV Cache can even exceed the model weights themselves, becoming the real bottleneck.
4. Mainstream Optimization Approaches
- Prefix caching and reuse: Persist the KV of shared prefixes such as system prompts, long documents, and multi-turn history. A cache hit skips redundant prefilling, which is key to improving the hit rate.
- KV quantization and compression: Store the cache at lower precision to trade memory for capacity, while watching for accuracy loss.
- Paged management: Borrowing the idea of paging from operating systems, split KV into blocks and allocate them on demand to reduce fragmentation and waste.
- Sparse and window attention: Keep only the KV of key tokens to control growth in long sequences.
- Scheduling and batching optimization: Use continuous batching to improve GPU utilization and amortize fixed overhead.
5. Engineering Recommendations
On platforms such as Baidu AI Cloud, you can deploy inference services with a container platform, use monitoring and alerting to track cache hit rate, memory usage, and P99 latency, and use a log service to identify long-tail requests. Key practices include: designing stable prefixes to improve hit rates, setting cache eviction policies by business tier, and running load tests to determine batch size and replica count. Understanding and optimizing KV Cache is the prerequisite for turning a price cut into a sustainable engineering capability.
