Why LLM Inference Costs Keep Falling: KV Cache, Memory Math, and Hit Rate
This article explains the technical mechanics behind falling large language model inference costs, focusing on prefix caching (KV Cache) rather than promotional discounts. It covers why autoregressive generation repeats computation over prior tokens, how incremental decoding reuses cached keys and values, a reusable formula for estimating KV Cache memory usage with a worked FP16 example, and why cache hit rate drives the bill. It closes with a practical optimization checklist covering prefix stabilization, context layering, capacity planning, observability, and precision trade-offs.
1. Behind the Price Drop: What Actually Deserves Attention
Discussions about LLM inference services being “permanently reduced to one quarter of the price” have been circulating in the industry. Price is obviously an important variable for developers, but focusing only on the discount means missing a more valuable technical signal: the reason per-token cost can keep dropping comes from higher reuse rates on the inference side and better GPU memory efficiency, not from commercial giveaways alone.
So this article is not about promotions. It is about mechanisms: why generative model inference contains so much removable repeated computation, how prefix caching (commonly called KV Cache in the industry) works, how its memory footprint is estimated, and how engineering teams turn a “high hit rate” into a reproducible optimization result. Where cloud capabilities are involved, only generic categories are used, such as container platforms, object storage, logging services, and monitoring and alerting.
2. The Nature of Autoregressive Generation: Every Step Recomputes History
Text generation is an autoregressive process: it produces one token at a time, then appends it back to the sequence to continue generating.
step1: input [today weather] -> output isstep2: input [today weather is] -> output goodstep3: input [today weather is good] -> output ...
In the attention mechanism, every token produces three sets of vectors: query Q, key K, and value V. The problem is that in step 2, the K and V for “today” and “weather” were already computed in step 1 and are exactly the same. The longer the sequence, the more history gets recomputed. Computational complexity grows quadratically with sequence length, and a substantial share of that work is pure waste.
3. KV Cache: Keeping Historical K/V in GPU Memory
The basic idea is simple: after each new token is computed, write its K and V into a memory cache. On the next step, compute Q/K/V only for the newest token, score that new Q against all cached K, then weight the cached V.
# Illustration: core loop of incremental decoding (pseudocode)cache_k, cache_v = None, Nonefor _ in range(max_new_tokens):q, k, v = model.forward(last_token) # compute only the newest tokenif cache_k is not None:k = concat(cache_k, k) # reuse historical K directlyv = concat(cache_v, v)cache_k, cache_v = k, vscores = softmax(q @ k.T / sqrt(d_head)) # new Q against all Kout = scores @ vlast_token = sample(model.head(out))
The benefit is that per-step work drops from “processing the whole sequence” to “processing one token.” The cost is that memory usage grows linearly with context length: trading space for time.
4. How to Calculate the Memory Footprint: A Reusable Formula
KV Cache ≈ 2 × layers × KV heads × dim per head × sequence length × bytes per value × concurrency
Take 32 layers, 32 heads, 128 dimensions per head, and FP16 (2 bytes) as an example: per token per layer is 2 × 32 × 128 × 2 = 16384 bytes (16KB); at a length of 4096 that is roughly 16KB × 32 × 4096 ≈ 2GB; with 10 concurrent requests, about 20GB. As you can see, with long contexts and high concurrency, the cache can even exceed the weights themselves and become the primary bottleneck. In engineering practice there is no need to compute this by hand: a simple spreadsheet or small script can estimate the capacity ceiling using the formula above.
5. Why “Cache Hit Rate” Becomes the Key Cost Factor
Mainstream inference services generally support prefix cache reuse: when multiple requests share the same prefix (system prompts, tool definitions, long documents, few-shot examples), the K/V of that prefix only needs to be computed once, and subsequent requests hit the cache directly. The higher the hit rate, the less repeated compute is needed, and the lower the bill.
It is worth stressing that a hit rate approaching the high end is not some exclusive feature of a single provider, but the combined result of stable prefix design, cache lifecycle management, and scheduling strategy. Any interpretation that attributes it to a single metric is not rigorous.
6. An Actionable Optimization Checklist
- Stabilize the prefix: put the system prompt, tool schema, and examples first and keep them byte-for-byte unchanged; place variable content after them.
- Reduce unintended differences: avoid concatenating volatile fields such as timestamps or random IDs into prompts.
- Layer the context: store long documents in object storage and retrieve and inject only the needed fragments, instead of stuffing in the entire document.
- Plan concurrency and capacity: estimate the cache ceiling with the formula above and scale elastically with a container platform.
- Build observability: use logging services and monitoring and alerting to track hit rate, time to first token, and memory watermarks.
- Weigh precision and structure: where quality is acceptable, evaluate general options such as lower-precision caching and more efficient attention structures.
7. Conclusion
A price cut to one quarter is a result, not a cause. Developers should focus on prefix reuse design, the memory capacity model, and the observability stack. These capabilities transfer to any compliant platform, such as Baidu AI Cloud, and produce a stable, reproducible cost advantage.
