Skip to content
AI Dev Toolkit.
Esc
  • Loading…

AI glossary · Local AI

What is the KV cache in an LLM?

Also called: key-value cache, attention cache

Definition

The KV cache is the memory where a language model keeps the attention keys and values of every token it has already processed, so each new token is computed without reprocessing the whole sequence.

Explained

How does the KV cache work, and how big is it?

Its size grows linearly with context length, which is why a long prompt can need more VRAM than the model weights. Our VRAM guide puts it in the full memory budget, next to the weights and runtime overhead.

Each attention layer turns every token into a query, a key and a value. To write the next token, the model compares its new query with the keys of all earlier tokens and mixes their values. Those keys and values don’t change, so runtimes keep them in memory instead of recomputing them at every step.

It saves computation, not memory: every new token still reads the whole cache, and the cache grows with every token. Its size is 2 (keys and values) × layers × KV heads × head size × bytes per value × tokens, multiplied by the batch size, because each sequence has its own. Grouped-query attention, sliding-window layers and compressed attention all shrink it, so the real figure depends on each model’s config.

Runtimes can store it at lower precision. llama.cpp has --cache-type-k and --cache-type-v, and Ollama has OLLAMA_KV_CACHE_TYPE; both accept q8_0 and q4_0. A quantised cache needs Flash Attention: llama.cpp refuses a quantised V cache without it, and Ollama applies cache quantisation only when Flash Attention is on, which it enables automatically where the hardware supports it. Ollama’s docs put q8_0 at about half the memory of f16, with a very small loss of precision.

Example

Llama 3.1 8B at 8K and 128K tokens

Llama 3.1 8B has 32 layers, each with 8 KV heads of 128 values. In FP16 every token adds 2 × 32 × 8 × 128 × 2 bytes = 128 KiB. At 8K tokens the cache is 1.0 GB. At the model’s full 128K context it is 16 GB, about 3.5 times the 4.6 GB of Q4_K_M weights.

Llama 3.1 8B: KV cache by context length and cache type
ContextKV cache (FP16)KV cache (q8_0)Total at Q4_K_M, FP16 cache
8,192 tokens1.0 GB0.5 GB6.6 GB
32,768 tokens4.0 GB2.1 GB9.6 GB
131,072 tokens16.0 GB8.5 GB22.6 GB

From our VRAM calculator: batch 1, Q4_K_M weights, overhead 10% (at least 1 GB). q8_0 stores 32 values in 34 bytes. Architecture from the model’s config.json, fetched 2026-10-08. GB = 1,024³ bytes.

Cost and quality

Why does the KV cache limit context length on your GPU?

On your own GPU, the KV cache is often what turns a model that fits into one that doesn’t. The weights are fixed, but the cache grows with the context length and with every parallel request, so a long context can need more memory than the model itself.

When memory is tight, set a shorter context than the maximum, quantise the cache to q8_0, or pick a model with fewer KV heads or sliding-window layers. Our VRAM calculator shows the cache separately for each model.

Don’t mix up

KV cache vs prompt caching: what’s the difference?

KV cache vs prompt caching
The KV cache is the working memory of whatever hardware runs the model; local servers such as llama.cpp’s can reuse it for a shared prefix in the next request. Prompt caching is the API version of that idea: the provider keeps a processed prefix between requests and bills it at a discount.
KV cache vs context window
The context window is the most tokens a model can handle. The KV cache is the memory the tokens you actually use take up, which is why local runtimes let you set a shorter context to save memory.

FAQ

Questions people ask

What happens when the KV cache is full?

The runtime cannot add more tokens to that sequence. Local runners stop at the context length you set; llama.cpp’s server can shift older tokens out with --context-shift, but that is off by default. Serving engines such as vLLM pause, or preempt, some requests to free cache space and recompute them later. Either way, a larger context setting reserves more memory up front.

How do you reduce KV cache memory?

Use a shorter context setting, pick a model with grouped-query attention (fewer KV heads), or quantise the cache to q8_0, which roughly halves it compared with 16-bit at a small cost in precision. Serving engines also allocate the cache in blocks as each sequence grows, rather than reserving the maximum length for every request. The VRAM calculator shows the effect of each change.

Is Redis a KV cache?

Not in this sense. Redis is a general key-value store: it maps keys to values for applications. An LLM’s KV cache holds the attention key and value tensors computed inside the model while it generates, lives in GPU or CPU memory next to the weights, and is managed by the inference engine, not by a database.

Where is the KV cache stored?

Normally in the same GPU memory as the model weights, because every new token reads it; llama.cpp offloads it to the GPU by default. When VRAM runs short, you can keep it in system RAM instead (--no-kv-offload in llama.cpp), which frees GPU memory but slows generation. Hosted APIs manage it for you; you see it only as context limits and prompt caching prices.

Go deeper

Try it and read more

Related

All 41 terms in the AI glossary

Keep exploring