The Core of LLM Inference Cost Reduction: How KV Cache Works

    This article explains why large language model inference costs can drop by orders of magnitude, focusing on KV Cache as the underlying mechanism. It covers the repeated computation problem in autoregressive generation, how KV Cache turns per-step work from O(sequence length) into roughly O(1), a concrete estimate of the GPU memory the cache consumes, four engineering approaches to achieving near-full cache hit rates, and a practical checklist covering structured prompts, cache compression, scheduling, observability, and autoscaling. It closes with three metrics to evaluate whether a model service is genuinely cheap: cache hit rate stability, long-context memory efficiency, and latency variance at peak.

    1. Behind the Price Drops: Why Inference Costs Shift by Orders of Magnitude

    Recent months have seen sharp cuts in the price of LLM inference services, and many developers’ first reaction is to assume providers are selling at a loss to grab market share. Explaining it purely as market strategy, however, misses a more fundamental fact: the cost structure of autoregressive inference contains enormous room for optimization, and how thoroughly engineering teams exploit that room directly determines how low the cost per call can go.

    To judge whether a price cut is sustainable, the key question is whether it lowers the “unit price” or the real resource consumption per token. The former depends on subsidies; the latter comes from technical progress. Once cache reuse rates, GPU memory management efficiency, and batching throughput are systematically improved, a provider can serve several times more requests on the same hardware, and the cost spread across each token falls accordingly. This kind of price cut is sustainable because it rests on higher resource utilization rather than financial concessions.

    2. The Base Mechanism: Repeated Computation in Autoregressive Generation

    An LLM generates text through an autoregressive process that advances one token at a time. Given the input “today weather”:

    1. Step 1: input [today, weather] -> predict "is"
    2. Step 2: input [today, weather, is] -> predict "nice"
    3. Step 3: input [today, weather, is, nice] -> predict ...

    In the attention mechanism, every token produces three vectors: a query vector Q, a key vector K, and a value vector V. The problem is that the K and V of the first three tokens in Step 2 are identical to Step 1, yet they are computed again. The longer the sequence, the worse this redundancy gets, and the overall cost degrades from “processing one token” to “processing the entire history.”

    The idea behind KV Cache is straightforward: once a token’s K and V are computed, they are written to GPU memory for reuse. Later steps only run the full Q/K/V projection for the newly added token, then compute attention scores between the new Q and all cached K, and weight the cached V to produce the output. The work per step drops from O(sequence length) to roughly O(1), which is the first-principles source of higher inference throughput.

    3. The Memory Ledger: How Much KV Cache Actually Costs

    Optimization is not free; the cache consumes GPU memory. Its size can be estimated as follows:

    1. KV Cache = 2 × layers × heads × head_dim × sequence_length × bytes_per_value

    With 32 layers, 32 heads, a head dimension of 128, and FP16:

    1. Per token, per layer: 2 × 32 × 128 × 2 = 16384 bytes = 16 KB
    2. 32 layers at 4096 length: 16 KB × 32 × 4096 ≈ 2 GB
    3. Batch size 10: about 20 GB

    The conclusion is clear: with long contexts and high concurrency, the cache can occupy more GPU memory than the weights themselves and become the real bottleneck. This also explains why longer contexts cost more; behind the pricing difference sit real memory and bandwidth overheads.

    4. Where a High Hit Rate Comes From

    A cache hit rate approaching saturation essentially means letting requests with an identical prefix reuse the same intermediate state. In engineering practice, this usually comes down to four directions:

    1. Prefix stabilization: place the system prompt, tool definitions, and few-shot examples at the very front and keep them byte-for-byte unchanged, so no timestamp or random ID contaminates the prefix.
    2. Cache tiering and eviction: keep hot data in GPU memory, let host memory or object storage hold warm and cold data, and pair this with eviction based on reference counting and LRU.
    3. Hit-aware routing: have the scheduling layer apply consistent hashing on the prefix fingerprint so requests sharing a prefix land on the same instance, avoiding recomputation across nodes.
    4. Lifecycle management: set a reasonable TTL for cache entries to balance hit rate against memory occupancy.

    5. Engineering Checklist

    • Structured prompts: strictly separate the invariant part from the variable part, and place variable content as late as possible.
    • Cache compression: use common industry techniques such as KV quantization, low-rank compression, and windowed retention to reduce the per-token cache overhead.
    • Scheduling optimization: use continuous batching to raise GPU utilization and keep long requests from blocking short ones.
    • Observability: collect cache hit rate, memory watermark, time to first token, and queueing time through logging and monitoring alerts to form a basis for capacity planning.
    • Elastic scaling: automatically scale on memory and concurrency metrics in a container platform to keep peak costs within a reasonable range.

    6. Selection Advice

    To assess whether a model service is “truly cheap,” do not look only at the listed price; look at three metrics: whether the cache hit rate is stable, memory efficiency under long contexts, and latency variance during peak hours. Build these three into a load-test script and replay real business traffic before drawing a reproducible cost conclusion. The general capabilities Baidu AI Cloud offers in model services and compute scheduling can serve as a reference foundation for enterprises building this kind of evaluation and capacity system.