← All notes

KV cache in Transformers: why it exists and which Cache class

Answer: A key-value (KV) cache stores kv pairs from the attention layers of previously processed tokens so autoregressive generation reuses them instead of recomputing.

Why

Autoregressive generation predicts one token at a time, and each prediction depends on all the previous context. Once a token is processed, its keys and values never change with respect to future tokens, so they can be cached. At each step the new key and value vectors are appended to the past keys and values; caching is done per layer.

Caching should only be used for inference; enabled during training it may cause unexpected errors.

Cache classes

| Cache | torch.compile | Memory | |---|---|---| | DynamicCache (default) | No | Medium | | Static Cache | Yes | High | | Quantized Cache | No | Low |

DynamicCache grows as generation progresses. A fixed-size StaticCache pre-allocates a maximum cache size, which enables JIT optimizations like torch.compile at the expense of memory. Disable the cache with use_cache=False; choose a strategy with cache_implementation.

Artifacts

See also

Claims

Sources

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Hugging Face docs (huggingface/transformers, huggingface/huggingface_hub, huggingface/diffusers on GitHub), Apache License 2.0 (https://www.apache.org/licenses/LICENSE-2.0). Text was condensed and changed from the originals.

License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Hugging Face docs, How caching works, Apache-2.0 (huggingface docs repos); Hugging Face docs, Cache strategies, Apache-2.0 (huggingface docs repos).

Cite this

Permanent ID: iil:note:ac63ndsk

iinventedlight. 2026. "KV cache in Transformers: why it exists and which Cache class." iinventedlight Library, iil:note:ac63ndsk, version of 2026-11-09. https://iinventedlight.com/library/notes/hf-transformers-kv-cache/. License: CC-BY-4.0.

@misc{iil_note_ac63ndsk,
  author = {{iinventedlight}},
  title = {{KV cache in Transformers: why it exists and which Cache class}},
  howpublished = {iinventedlight Library},
  year = {2026},
  date = {2026-11-09},
  url = {https://iinventedlight.com/library/notes/hf-transformers-kv-cache/},
  note = {Permanent ID iil:note:ac63ndsk. License: CC-BY-4.0.},
}

Markdown

Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com.

For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/hf-transformers-kv-cache.md.