LLM Algorithms · 8 of 10

KV Cache tutorial

Reuses earlier key and value vectors instead of recomputing the whole prompt every step. Learn the concept, implement it from scratch, and apply it with Hugging Face Transformers.

Category
Inference optimization
Core idea
K₁:ₜ ← Concat(K₁:ₜ₋₁,Kₜ); V₁:ₜ ← Concat(V₁:ₜ₋₁,Vₜ)
Typical use
Accelerating autoregressive LLM chat and long-sequence generation.
Practical tool
Hugging Face Transformers

What is KV Cache?

KV Cache is a inference optimization technique. Reuses earlier key and value vectors instead of recomputing the whole prompt every step. Its central idea is summarized by K₁:ₜ ← Concat(K₁:ₜ₋₁,Kₜ); V₁:ₜ ← Concat(V₁:ₜ₋₁,Vₜ).

This guide connects theory to practice. You will trace the input, intermediate process, and output; decode the notation; run a dependency-light implementation; then repeat the workflow with Hugging Face Transformers and evaluate the result.

By the end of this tutorial, you will be able to

  • Explain when KV Cache is appropriate and what assumptions it makes.
  • Read its mathematical notation or complexity statement without guessing what the symbols mean.
  • Follow and modify a from-scratch Python implementation.
  • Build a practical workflow with Hugging Face Transformers and choose useful evaluation checks.

Prerequisites and tools

You do not need an advanced software stack. Start with a recent Python environment and the fundamentals below, then install only the packages used by the practical example.

  • Comfort with Python, tensor shapes, matrix multiplication, and probability distributions.
  • A working understanding of token sequences, embeddings, batches, and attention masks.
  • Enough memory to run the small tensor example; pretrained-model tutorials may also download model weights.
LanguagePython 3
Primary libraryHugging Face Transformers
Learning modeFrom scratch + library

How KV Cache works

  1. 1

    Input

    Cached key-value tensors and the newest token representation.

  2. 2

    Prepare and configure

    Check shapes, value ranges, ordering assumptions, missing values, and the parameters that control the algorithm’s behavior.

  3. 3

    Algorithm process

    Project only the new token, append its keys and values, then attend over the cache.

  4. 4

    Output

    The next-token context with much less repeated computation.

  5. 5

    Validate

    Compare cached and uncached next-token logits for numerical agreement.

Formula and intuition

K₁:ₜ ← Concat(K₁:ₜ₋₁,Kₜ); V₁:ₜ ← Concat(V₁:ₜ₋₁,Vₜ)

At decoding step t, only the new key Kₜ and value Vₜ are appended; earlier keys and values are reused.

The notation captures the main operation or complexity statement behind KV Cache. Read it together with the symbol key above and the step-by-step process in this guide.

When to use KV Cache

Accelerating autoregressive LLM chat and long-sequence generation.

Learning tip

Change one parameter at a time in the interactive lesson, replay the animation, and connect the visible change to the input, process, and output described above.

KV Cache from scratch in Python

This dependency-light example emphasizes the algorithm’s mechanics so each important step remains visible.

PythonEducational implementation
import numpy as np

key_cache = np.empty((0, head_dim))
value_cache = np.empty((0, head_dim))

for token in generated_tokens:
    query = token @ W_query
    new_key = token @ W_key
    new_value = token @ W_value
    key_cache = np.vstack([key_cache, new_key])
    value_cache = np.vstack([value_cache, new_value])

    scores = query @ key_cache.T / np.sqrt(head_dim)
    weights = np.exp(scores - scores.max())
    weights /= weights.sum()
    context = weights @ value_cache
How to use this example

Run it once unchanged, inspect the output, and then alter one input or parameter. The compact implementation is designed for learning; use the tested library workflow below for real projects.

Build KV Cache with Hugging Face Transformers

Transformers exposes past_key_values so autoregressive decoding can reuse attention keys and values from earlier tokens.

Recommended environment

JupyterLab, Google Colab, or VS Code

Create an isolated virtual environment for a local project, or paste the cells into a hosted notebook. Pin package versions before deploying a reproducible application.

Install the required package python -m pip install torch transformers
  1. Prepare the data.Cached key-value tensors and the newest token representation. Validate its shape, type, range, and ordering before training or execution.
  2. Configure the algorithm.Begin with explicit, conservative parameters and a fixed random seed whenever the library supports one.
  3. Fit or execute.Project only the new token, append its keys and values, then attend over the cache.
  4. Inspect the result.The next-token context with much less repeated computation. Then apply the evaluation checks in the next section.
PythonHugging Face Transformers workflow
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer

model_name = "distilbert/distilgpt2"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name).eval()
inputs = tokenizer("Algorithms become clear when", return_tensors="pt")

with torch.no_grad():
    first = model(**inputs, use_cache=True)
    next_id = first.logits[:, -1].argmax(dim=-1, keepdim=True)
    second = model(
        input_ids=next_id,
        past_key_values=first.past_key_values,
        use_cache=True,
    )
print("cached layers:", len(first.past_key_values))
print("next-step logits:", second.logits.shape)

API details and version-specific options: official Hugging Face Transformers reference →

How to evaluate the result

A successful run is not enough. Evaluate the output against the intended use, compare it with a simple baseline, and preserve a genuinely unseen test case whenever the task involves learned parameters.

  • Compare cached and uncached next-token logits for numerical agreement.
  • Measure per-token latency and cache memory as sequence length grows.
Reproducibility check

Record the data version, package versions, parameters, random seeds, and evaluation procedure. Re-run the same workflow before publishing a benchmark or deploying a model.

Pitfalls and how to avoid them

These failure modes are common in tutorials and production systems. Treat them as review questions, not just after-the-fact debugging advice.

  • The cache is for inference; reusing detached history is generally inappropriate for full-sequence training.
  • Attention masks and position IDs must account for cached length.
  • Cache memory grows linearly with generated sequence length and layer count.

Before using KV Cache in a project

  • Write down every tensor shape and mask convention before composing layers.
  • Test a tiny deterministic case against a simple reference implementation.
  • Separate training behavior from autoregressive inference and disable dropout for evaluation.
  • Measure quality, latency, peak memory, and sequence-length scaling together.
  • Pin model, tokenizer, framework, and generation-configuration versions.

Official documentation and next steps

Primary software reference Hugging Face Transformers

Use the official documentation to confirm supported parameters, current defaults, input requirements, and version changes.

Read official documentation →

This guide is an educational introduction, not a substitute for domain validation. For consequential applications, review the source documentation, test against representative data, and involve a subject-matter expert.

See KV Cache in motion

Open the interactive lesson to adjust parameters, scrub through the process, replay the animation, and compare the explanation with the Python code.

Launch visualization →