---
title: "KV cache"
url: https://www.daytona.com/definitions/k/kv-cache
---

# KV cache

A KV cache stores the keys and values a language model computes for earlier tokens, so it does not recompute them for each new token it generates.

A KV cache is memory that a language model uses to avoid repeating work during inference. A transformer model computes a key and a value for every token in its input at every layer. To generate the next token, it needs the keys and values of all earlier tokens. The KV cache keeps them, so each new token only needs its own keys and values computed.

The cache makes generation much faster, but it takes a lot of memory. Its size grows with the number of tokens, the number of layers, and the number of requests served at once. On a GPU, it often takes more memory than the model weights for long contexts. Serving systems manage it carefully, for example by storing it in pages, compressing it, or moving parts of it out of GPU memory.

KV caches can also be reused across requests. When many requests share the same beginning, such as a long system prompt, a server can keep the cache for that part and skip computing it again, which is the basis of prompt caching. GPU memory for the cache is often what limits how many users one GPU can serve. Sandboxes that run models on their own GPUs face the same trade-off between context length and the number of parallel requests.

## Related terms

- [Inference](https://www.daytona.com/definitions/i/inference.md)
- [LLM (Large Language Model)](https://www.daytona.com/definitions/l/llm-large-language-model.md)
- [Context window](https://www.daytona.com/definitions/c/context-window.md)
- [GPU memory](https://www.daytona.com/definitions/g/gpu-memory.md)
- [Prompt caching](https://www.daytona.com/definitions/p/prompt-caching.md)
- [Quantization](https://www.daytona.com/definitions/q/quantization.md)
