Skip to main content
AI · LANGUAGE MODELS

What Is KV Cache?

The KV cache is the memory a language model uses to keep the key and value vectors of every token it has already processed, so each new token is computed without re-reading the whole conversation. It makes generation fast, but it grows with context length and users: Llama 3.1 8B needs about 1 GB for every 8,000 tokens at 16-bit.

01

What the KV cache is

The KV cache (key-value cache) is the block of memory where a transformer language model keeps the key and value vectors it has already calculated for every token in the current conversation. Because those vectors are stored, the model can produce each new token by working on that one token alone, instead of processing the whole text again from the start.

Keys and values come from the attention step at the heart of a large language model. In every layer, each token is turned into three vectors: a query, a key and a value. To decide what a new token should attend to, the model compares its query with the keys of all earlier tokens, then blends their values according to how well they match. The keys and values of earlier tokens never change once calculated, so recomputing them for every new word would be wasted work.

The cache trades that computation for memory: an entry for every token, in every layer, for as long as the conversation lasts.

02

How the cache fills during prefill and decode

A request passes through two phases, and the cache behaves differently in each.

Prefill processes the whole prompt in one pass and writes a key and a value for every prompt token into the cache. The wait before the first word appears, the time to first token, is mostly prefill, so it grows with the length of the prompt.

Decode then generates the reply one token at a time. Each step reads the full cache, adds one new entry per layer and moves on.

Runtimes can also keep the cache between turns, so only the new text of the next question needs prefilling; llama.cpp’s server can reuse matching chunks of a cached prompt.

03

How big the KV cache gets

The size follows directly from the model’s shape. For each token, every layer stores one key and one value for each key-value head:

Cache per token = 2 × layers × key-value heads × head size × bytes per number

Meta’s Llama 3 paper gives the 8B model 32 layers and 8 key-value heads, and its model dimension of 4,096 split across 32 attention heads gives a head size of 128. At 16-bit precision, 2 bytes per number, that is 2 × 32 × 8 × 128 × 2 = 131,072 bytes, or 128 KB for every token. From there the cache scales in a straight line with the context length and with the number of conversations held at once.

Tokens held Llama 3.1 8B, 16-bit cache Llama 3.1 8B, 8-bit cache Llama 3.1 70B, 16-bit cache
4,096 About 0.5 GB About 0.25 GB About 1.25 GB
8,192 About 1 GB About 0.5 GB About 2.5 GB
32,768 About 4 GB About 2 GB About 10 GB
131,072 (the full 128K window) About 16 GB About 8 GB About 40 GB

The figures are our arithmetic from Meta’s layer counts (the 70B model has 80 layers and the same 8 key-value heads), with 1 GB taken as 1,024 MB. llama.cpp’s documentation puts Llama 3.1 8B at 4.58 GB in its Q4_K_M format, so one user with a full 128K context needs more than three times as much memory for the cache as for the model, and eight users at 8,192 tokens need about 8 GB between them.

FAQ · 01
What hardware does a local LLM need?
Enough fast memory to hold the model’s weights plus its KV cache. At 4-bit, weights need roughly 0.5 to 0.6 bytes per parameter, so an 8 GB device suits models up to about 8 billion parameters. Once the model fits, memory bandwidth sets how many tokens per second it produces.
Read the full FAQ —
04

Ways to make the KV cache smaller

Model designers and runtime developers both work on the problem, and most current systems combine several of these techniques.

  • //
    Grouped-query attention. Several query heads share one key-value head. Llama 3 8B has 8 key-value heads for 32 query heads, chosen, Meta says, to reduce the size of key-value caches during decoding: a quarter of the cache that full multi-head attention would need.
  • //
    Sliding-window and hybrid layers. Some layers look back only a fixed distance, so their cache stops growing. OpenAI’s published configuration for gpt-oss-20b alternates full attention with layers that see only the last 128 tokens, and Qwen3.5 uses full attention in one layer in four.
  • //
    Cache quantization. Storing keys and values in 8 bits halves the cache. vLLM supports FP8 caches, and llama.cpp sets the key and value cache types separately, from the default 16-bit down to 8-bit and 4-bit types. Check output quality at lower settings.
  • //
    Paged attention. Introduced with vLLM at SOSP 2023, it stores the cache in fixed-size blocks, like pages of virtual memory. Its authors report near-zero wasted cache memory and 2 to 4 times the throughput of earlier serving systems.
  • //
    A sensible context limit. Runtimes reserve cache space for the context length they are given. Setting it to what the job needs, instead of the model’s maximum, frees memory straight away.
05

KV cache on a shared-memory Jetson

On a desktop or server GPU, the cache sits in the card’s own memory. On Jetson, NVIDIA’s CUDA documentation states that the CPU and the integrated GPU share the same physical DRAM, so the operating system, camera buffers, model weights and KV cache all draw on one pool.

That makes the cache part of the sizing from the start. On an 8 GB module, a 4-bit 8B model with an 8,192-token context leaves little room for anything else, so smaller models or shorter contexts are the norm. A 16 GB Orin NX gives the same model room for longer prompts or a second session. A 70B model at 4-bit needs about 40 GB for its weights, and the full 128K context adds about 40 GB more at 16-bit: a 128 GB Jetson T5000 holds both, a 64 GB module does not.

NVIDIA’s published Jetson LLM benchmarks use 2,048-token prompts, so they say little about long-context use. Our NVIDIA Jetson comparison and module selector lists the memory of every current module, and our Jetson Thor computers carry the 64 GB T4000 or the 128 GB T5000 for long contexts and several users. Where many people share one model, edge AI servers with discrete GPUs keep the cache in dedicated GPU memory.

06

Quick answers on KV cache

KV cache: common questions

What is KV cache in an LLM?

Stored attention data. It holds the key and value vectors of every token the model has processed, layer by layer, so each new token is computed without reprocessing the conversation.

How does KV cache work?

Write once, read every step. Prefill writes an entry for each prompt token. Each decode step reads the whole cache, adds one entry per layer for the new token and repeats until the answer is finished.

What is KV cache offloading?

Moving the cache out of GPU memory. When GPU memory runs short, some runtimes keep part of the cache in system RAM or on fast storage, trading speed for capacity. On Jetson the CPU and GPU already share one memory, so moving it to system RAM gains nothing.

Does the KV cache change the answers?

Not at full precision. It stores exactly what the model would otherwise recalculate. Quantizing the cache to 8 or 4 bits can change outputs slightly, so test those settings.

Why does the KV cache use so much memory?

It grows with every token and every user. Each token adds an entry in every layer, about 128 KB for Llama 3.1 8B at 16-bit, and each simultaneous conversation keeps a cache of its own.

We supply Jetson and GPU computers for language models integrated as standard, with the module or GPU and memory fitted and the operating system installed before they ship.