Skip to content
AI & Machine Learning Rich #kv-cache#inference#autoregressive

KV Cache: Why Generation Speeds Up

During generation, past K and V are cached. Each new token only computes its own.

A free, animated kv cache: why generation speeds up you can read here or embed on any website, from Scrollchart.

KV Cache: Why Generation Speeds Up

KV Cache: Why Generation Speeds UpCaching past K and V tensors eliminates redundant recomputation: FLOPs per step stay O(1) not O(n)No cacheKV cacherecompute allnew token onlyp0Q,K,Vp1Q,K,Vp2Q,K,Vp3Q,K,Vgen1Q,K,Vgen2Q,K,Vgen3Q,K,Vgen4Q,K,Vp0cachedp1cachedp2cachedp3cachedgen1cachedgen2cachedgen3Q,K,Vgen4cachedmemory grows linearly with seq len (2 x H x d_head x n_layers x bytes)Attention FLOPs per generation step (relative)12345678generation stepLegendNo cache: O(n) FLOPsCache: O(1) FLOPsMemory cost:2 x H x d x L x nn = seq len, L = layersKV cache trades memory for compute: at 32k context, 32 layers, fp16, the cache alone can reach 67 GB (MHA). GQA, MQA, and MLA compress this.

Autoregressive generation. At step t, only the new token computes its Q, K, V. Past K and V are read from cache. Memory grows linearly in sequence length and quadratically in batch x heads x d. Memory and FLOPs without cache vs with cache plotted side by side.

Good for

  • LLM serving articles
  • vLLM/TensorRT-LLM tutorials
  • Inference optimization content

Source & accuracy

This kv cache: why generation speeds up is an editorial illustration built to represent the concept accurately. Where it shows figures, they are typical or representative values chosen to make the relationship clear, not a single underlying dataset. The diagram and its explainer are reviewed and maintained centrally, and updated over time as understanding improves.

Why caching K and V speeds up generation

During generation, the model produces one token at a time. Each new token attends to all previous tokens (the context). Computing attention from scratch each time is wasteful: you recompute K and V for all previous tokens even though they haven't changed. KV caching stores the K and V matrices from previous steps, reusing them for new tokens.

The cache grows with each generation step. After generating 100 tokens, the cache contains 100 steps worth of K and V. Each new token only computes its own K and V, attending to the full cached context.

Speedup and memory cost

With KV caching, generation time per token drops from O(n^2) to O(n) (where n is context length). For a 4K context, this is a massive speedup: no KV cache requires 16M attention operations per token, while with cache it's 4K lookups. The trade-off is memory: you must store all K and V from all positions.

For a 70B parameter model with 4K context, KV cache consumes roughly 32-64 GB per sequence. This limits batch size: few sequences fit if KV cache is large.

Cache size management strategies

Sliding window attention discards old cache entries, keeping only the most recent (e.g., last 1K tokens). This trades off context for memory. Sparse attention patterns also reduce cache size by selecting which positions to attend to. PagedAttention (used in vLLM) manages cache like virtual memory, reusing pages and paging out unused cache to disk.

Embed this diagram

Add this animated kv cache: why generation speeds up to your own site. Copy one line of HTML, or use the embed builder for theme and sizing options.

Reference

What this is
A free, embeddable, animated kv cache: why generation speeds up for any website.
Who uses it
AI/ML blogs, DevOps / SRE sites.
How to embed
Copy one line of HTML. No signup. No watermark. Works in WordPress, Webflow, Ghost, Substack, plain HTML.
File size
iframe embed, ~80 KB gzipped (loads on demand, does not block your page paint).
License
Free forever. Editorial explainer text included; updated centrally over time.

Embed format options

Copy the universal HTML snippet, the WordPress shortcode, or an iframe fallback - see the WordPress plugin page for details. Any format keeps the same Core Web Vitals profile and the same explainer text.

Embed snippet
<div data-scrollchart="kv-cache" data-scrollchart-v="1"></div>
<script src="https://scrollchart.com/embed.js" async></script>

Frequently asked questions

Where can I get a free animated "KV Cache: Why Generation Speeds Up" for my website?
Scrollchart provides "KV Cache: Why Generation Speeds Up" as a free, embeddable animated diagram you can add to any website with one line of HTML. No signup is required and there is no watermark. The diagram and its explainer text are served from scrollchart.com, so the embed stays current without any maintenance on your end.
How do I embed a kv cache: why generation speeds up in a developer or tech blog?
Copy the one-line snippet from the Scrollchart diagram page and paste it into your post HTML. It works in any static site generator, CMS, or hand-coded HTML page. The embed is a thin loader, not an iframe, so the content is fully in your DOM.