Why LLM Inference Costs Keep Dropping: A Breakdown of KV Cache Optimization

    This article explains why the price of large language model inference keeps falling by examining the engineering behind it. It breaks down the real cost of a generation request, shows how autoregressive decoding repeatedly recomputes attention keys and values, and introduces KV Cache as the fix. It also quantifies KV Cache memory usage, explains how high prefix cache hit rates reduce compute and memory overhead, and offers practical tips for improving cache reuse and memory management.

    1. What the Price Drops Reveal About Inference Costs

    A recent round of steep price cuts for a class of large language model services has caught developers’ attention. On the surface, this looks like a commercial strategy. From an engineering perspective, however, the room for lower prices comes from a drop in the unit cost of inference—and the largest share of that cost is usually not parameter loading, but GPU memory usage and repeated computation.

    To see why, consider what a single text generation request actually costs. It breaks down into three parts: the static memory occupied by model weights, the compute consumed by the forward pass at each step, and the dynamic cache that holds the context history. The first two are relatively fixed. The third grows linearly—or even faster—with context length and concurrency. Whoever can bring that third part down holds the pricing advantage.

    2. The Repeated Computation Problem in Autoregressive Generation

    Large language models generate text through a strictly autoregressive process: each step produces a single token, appends it to the end of the sequence, and generates the next one.

    The flow looks like this:

    1. step 1: input [Today weather] -> output is
    2. step 2: input [Today weather is] -> output good
    3. step 3: input [Today weather is good] -> output ...

    The problem lies in the attention mechanism, where every token produces three vectors: Q (Query), K (Key), and V (Value). In step 2, the K and V vectors for the first three tokens were already computed in step 1. Their values are identical, yet they are computed again. The longer the sequence, the worse this waste becomes, and overall complexity degrades from an ideal linear scale to something close to quadratic.

    3. How KV Cache Works

    KV Cache takes a straightforward approach: after computing a new token, write its K and V into memory. On the next inference pass, read the K and V of historical tokens directly, run the full Q, K, V computation only for the newest token, compute attention scores using the new Q against all cached K, and finally apply the weights to the cached V.

    1. # Pseudocode: single-step decoding with cache
    2. if cache is None:
    3. k, v = layer.forward_kqv(new_tokens) # first pass: full computation
    4. else:
    5. k, v = layer.forward_kqv(new_tokens) # incremental: only the new token
    6. cache.append(k, v)
    7. attn = softmax(q @ cache.k.T) @ cache.v

    The benefit is that per-step computation drops from “processing the entire sequence” to “processing one token.” This is the key to the qualitative leap in inference throughput.

    4. Quantifying KV Cache Memory Usage

    The cache is not free, and its size can be estimated precisely:

    1. KV size = 2 × num_layers × num_heads × head_dim × sequence_length × bytes_per_value

    Take 32 layers, 32 heads, a head dimension of 128, a sequence length of 4096, and FP16 (2 bytes) as an example:

    • Per token, per layer: 2 × 32 × 128 × 2 = 16384 bytes ≈ 16 KB
    • Total cache: 16 KB × 32 layers × 4096 ≈ 2 GB
    • 10 concurrent requests: about 20 GB

    The conclusion is clear: for long contexts and high concurrency, the cache can even exceed the weights themselves and become the real bottleneck.

    5. Why a High Hit Rate Cuts Costs Significantly

    When a system can identify and reuse identical prefixes—such as a fixed system prompt, a long document, or multi-turn conversation history—the cache hit rate rises sharply. Repeated computation is eliminated, and the compute and memory overhead per request drops accordingly. This is the root of the “it turned out to be cheap after a few days of use” experience: the price advantage lies not in the listed rate, but in the actual resource savings delivered by cache hits.

    6. Practical Engineering Recommendations

    1. Stable prefixes: Place unchanging content at the very front of the prompt to improve reuse probability.
    2. Control context: Trim history promptly to keep the cache from growing without bound.
    3. Precision and quantization: Store the cache at a lower precision when quality remains acceptable.
    4. Layered and paged management: Adopt paged cache management approaches common in the industry to reduce fragmentation.
    5. Observability: Track hit rate, memory水位, and latency percentiles through logging services and monitoring alerts.
    6. Elastic architecture: Combine container platforms and message queues for queuing and autoscaling to smooth out peaks.

    In summary, the essence of lower inference prices is the engineering dividend from cache reuse and memory governance. For developers, understanding and actively optimizing KV Cache delivers more long-term value than simply watching the unit price.