How KV Cache Hits Reshape LLM Inference Cost
KV cache stores computed keys and values so autoregressive generation does not recompute the full history at every token. This article explains why generation repeats work without cache, how cache uses GPU memory, what hit rate actually measures, a simple cost intuition for reusable prefixes, and how to raise hits on both the client and the server.
How KV Cache Hits Reshape LLM Inference Cost
When a large language model generates text token by token, history is not free. Attention must maintain queries, keys, and values for every token that has already appeared. Without a cache, each new token forces a full recomputation of the entire history. A key-value (KV) cache keeps already computed keys and values in GPU memory. The next step fully computes only the new token, then attends against the cached history. A high hit rate means large prefixes are not recomputed, so billed compute can be far smaller than the surface context length.
Why Autoregression Repeats Work
Text generation is autoregressive: each step produces one new token, then appends it as input for the next step. Without a cache, step n still recomputes attention projections and interactions for the first n tokens.
Take the phrase “the weather is nice today.” The first step might consume “the weather is” and produce “nice.” The second step’s input becomes “the weather is nice.” The keys and values for the earlier tokens were already produced and have not changed, yet they would be computed again. The longer the sequence, the worse the waste: work at step t grows with existing length, so total compute is closer to quadratic in length than linear in “new tokens only.”
In attention, each token has three vectors: a query (“what am I looking for”), keys (“how can I be matched”), and values (“what content to take after a match”). The new token needs its own query, key, and value. Historical tokens whose keys and values are unchanged have no reason to be recomputed. The KV cache’s core idea is to read historical keys and values directly, match the new query against all cached keys, then take a weighted sum of values. Each step drops from “process the whole sequence” to “process the latest token and interact with the cache,” cutting both latency and compute.
How Much GPU Memory Does the Cache Use
Cache is not a free memory decoration; it often becomes the bottleneck before model parameters. A common order-of-magnitude estimate is:
cache bytes ≈ 2 × layers × heads × head dim × sequence length × bytes per element
The 2 is one copy for keys and one for values. Example: 32 layers, 32 heads, head dim 128, sequence 4096, FP16 (2 bytes). Keys and values for one token in one layer are about 2 × 32 × 128 × 2 = 16 KB. Across 32 layers and 4096 tokens that is about 2 GB. Ten concurrent sequences of the same length with no sharing approach 20 GB. Long context, high concurrency, and higher precision scale this linearly.
Engineering therefore has to watch three things at once: whether parameters fit, whether cache fits, and whether bandwidth can keep up. Cache hits save compute; they do not free memory already occupied. Even with a high hit rate, very long sessions can still fill GPU memory. Common mitigations include capping max length, releasing unused sessions promptly, compressing or quantizing along layer or head dimensions, and sharing one cache for identical prefixes across requests instead of storing a copy per request.
What Hit Rate Actually Measures
Public pricing is often billed per input token and output token, but server-side cost is closer to “prefixes that missed and must be recomputed + newly generated tokens + bandwidth to interact with cache.” If the system can detect that a request’s beginning matches an existing cache exactly, it can skip recomputing that prefix’s keys and values and pay compute only for the new suffix.
When hit rate is near the limit, users feel it strongly: the same system prompt, the same long document, and the same multi-turn history are attached again and again. Surface input is long; newly computed work is only a few tokens at the tail. After rounding, the marginal cost of long input can be far below the intuition of “recompute the full length.” That is not a cheaper model by magic; it is prefix reuse removing repeated work.
High hit rate usually needs all of these at once:
- Prefixes that are stable at byte or token granularity—no random rewrites of system text, timestamps, or shuffled paragraph order on every call.
- Server indexing by prefix rather than the whole request, so identical beginnings can hang on the same cache chain.
- Cache that has not been evicted. With limited GPU or host memory, cold prefixes are swapped out and the next call falls back to full recompute.
- Batching and parallelism that do not split a shareable prefix into invisible copies. If sharing fails, hit rate can look high inside a single request but never amortize across users.
Conversely, changing one leading token, randomly chunking and concatenating a long document, or putting dynamic fields at the very front of the prompt breaks the cache chain and collapses hit rate. Discounts cannot offset repeated compute; long context becomes expensive and slow again.
A Checkable Order-of-Magnitude Intuition
You do not need any vendor’s list price to split the cost. Suppose a request is reusable prefix P plus new suffix S, then generates G new tokens. Without cache, attention related to P, S, and G is recomputed as length grows. With an ideal hit, P’s keys and values are read and not recomputed; the main new cost is S and G, plus reading a cache of length P.
If P is much larger than S+G—for example a fixed tens-of-thousands-of-words spec, with only a sentence or two appended each turn—compute after a hit can be more than an order of magnitude below full-length recompute. That is the technical reason people later feel that long input “almost does not hurt”: they compare against a mental account of full recompute, not the incremental account after a cache hit. Raising hit rate from 50% to 90% saves the longest, most expensive prefix; going from 90% to near-complete hits saves the remaining tail—still noticeable, but with less absolute room.
There is a ceiling. Reading a very long cache costs bandwidth; inconsistent cache layouts across a batch hurt kernel efficiency; time-to-first-token still depends on first packet, scheduling, and any missed prefix. Hit-rate work optimizes the repeated part, not the serial number of generation steps.
How to Raise Hit Rate
On the caller side, split the prompt into a stable layer and a volatile layer. Put system role, tool descriptions, and fixed knowledge first and keep the exact text. Put this-turn user questions, time, and random values at the end. In multi-turn chat, do not rewrite history every turn; append the new turn. When you must edit, prefer changing the suffix rather than inserting in the middle, or later cache keys become invalid.
On the server, prefix cache must be shareable across requests, with an explicit eviction policy: recency or prefix heat, not only single-request lifetime. Observe separately: prefix tokens hit, tokens recomputed on miss, generated tokens, cache occupancy, and eviction count—not only end-to-end latency. Latency can drop because of a smaller model or shorter output; that alone does not prove the cache is working.
Illustrative control flow (not a specific platform API):
key = hash(stable_prefix_tokens)if cache.has(key) and not evicted:kv = cache.load(key) # read historical KVlogits = attend(new_tokens, kv)else:kv = compute_kv(stable_prefix_tokens)cache.store(key, kv)logits = attend(new_tokens, kv)
The hash must match tokenization: an extra space or a different normalization changes the key and the hit fails. “Looks the same” is not the same as “same token sequence.”
What to Check When Inference “Gets Cheaper”
When inference prices drop, or long input suddenly looks very cheap, split the cause instead of remembering a single multiplier.
- Did the unit price actually fall, or did billing change from “all input” to “missed input + output”?
- Is cache shared across sessions? Reuse only inside a single generation does little for independent request bills.
- Are hits stable? Peak eviction, version switches, and tiny prompt changes make the same call expensive then cheap.
- Are memory and concurrency pinned by cache? Hits save compute, but hot prefixes occupy resources for a long time; in extremes, throughput is limited by memory bandwidth.
- Was quality sacrificed? Lower precision, shorter default length, and aggressive cache compression should be validated on their own; price is not a substitute for quality.
For teams, useful actions are: freeze prompt templates, put dynamic fields last, keep a stable chunk order for long documents, and in test compare cold full recompute versus a hot prefix on latency and token counts before treating long context as the default path. If prefixes change often, shorten input or retrieve excerpts rather than expecting infinite cache hits.
Summary
KV cache turns repeated key-value computation in autoregression into write-once, read-many. Memory grows with layers, head dim, length, concurrency, and precision, and often becomes the bottleneck before parameters on long text. Cache hit rate measures how much prefix need not be recomputed. When prefixes are stable, shareable on the server, and not evicted, the marginal cost of long input can approach “process only new tokens.” When judging a price cut or a sudden cheapness, look at billing definition, hit stability, memory occupancy, and generation quality together, and write stable prefixes with dynamic suffixes so the reusable part actually reaches the cache.
