Services

Resources

Company

/

KV cache

KV cache (key-value cache)

A KV cache (key-value cache) is GPU memory where a transformer model stores the attention keys and values it has already computed for earlier tokens. Each new token reuses those stored values instead of recomputing them for the whole sequence. This makes generation faster, but the memory it uses grows with context length.

How it works

Inference has two phases. Prefill processes the whole prompt and fills the cache, and decode generates one token at a time while reading from it. Cache size per token is 2 × layers × KV heads × head dimension × bytes per value. For a 7B model with 32 layers, 32 KV heads, a head dimension of 128 and 16-bit values, that is 0.5 MiB per token, or 2 GiB for a 4,096-token context.

Why it matters in production

KV cache memory often limits how many concurrent requests a GPU can serve. Prompt caching reuses the cached prefix of a repeated prompt across requests, which cuts prefill work, lowers time to first token and is usually billed at a lower rate. Keeping prompt prefixes stable is what makes those cache hits possible.

See also

On the One2N blog

References

  1. Efficient memory management for LLM serving with PagedAttention (Kwon et al., 2023)

  2. Attention is all you need (Vaswani et al., 2017)

KV cache sizing worked for a 7B model: 2 x 32 layers x 32 KV heads x 128 head dim x 2 bytes = 0.5 MiB per token, and x 4,096 tokens = 2 GiB per request. Below, rows of key and value cells: prefill fills them for the whole prompt and decode adds one pair per new token. At the bottom, GPU memory holds the weights and three 2 GiB caches, and a fourth red 2 GiB cache has no room.

Putting this into practice?

Talk to One2N about our AI engineering services

Talk to One2N about our AI engineering services