Guides · 3 min read

Why long context costs memory (the KV cache)

A model that fits at 8K tokens can run out of room at 64K. Here is why, and how to estimate it.

While a model reads and writes, it keeps a KV cache: the attention keys and values for every token so far, in every layer. It lets the model avoid recomputing the past, and it grows in step with the conversation.

KV cache ≈ 2 × layers × KV heads × head size × bytes × tokens

Because it scales with tokens, doubling the context doubles this part of the memory bill. The weights stay the same. For a long document, a long chat or a coding agent that keeps many files in view, the KV cache can rival the model itself.

What to do about it

See the exact number for any model and context length in the memory calculator.

Estimates come from published hardware specs and our open formulas, not from running the model on your machine. Real speed varies with runtime, drivers and settings. Numbers marked “Measured” come from a linked benchmark.