The Open Model EcosystemLesson 25 of 71

Module 3 · Inside the files

Attention variants, and the cache that eats your memory

In this lesson

  • 10 min read
  • 7 reading sections
  • Knowledge check
  • Lesson 5 of 10 in this chapter

This chapter’s goalOpen a model repository you have never seen and predict, from config.json and the tokenizer alone, its parameter count, its memory footprint at a given context length, its per-token cost in your languages and the exact conversation format it requires; then verify each prediction with a command and diagnose a runaway or garbled generation from the files rather than by guessing.

This chapter’s media

The film explains chapter ideas. The audiobook narrates the complete teaching text.

FormatEnglishहिंदी
Chapter filmNot availableNot available
Full audiobookNot availableNot available
KV cache for one conversation, Llama-3.1-8B010208000128000Context length, tokensGigabytes of cache—— fp16 cache: 128 KB per token– – 8-bit cacheThe weights are 16 GB. At 128k of context the cache alone matches them, and this is one conversation:ten concurrent users at 8k each want 10 GB on top of the model. Quantising the cache halves it, at acost that shows up on long-context retrieval before it shows up on chat.
The weights are 16 GB. At 128k of context the cache alone matches them, and this is one conversation: ten concurrent users at 8k each want 10 GB on top of the model. Quantising the cache halves it, at a cost that shows up on long-context retrieval before it shows up on chat.

Figure

A little at a time

Explore an idea, try it, then check what stays.

Step 1 of 8Free · no account needed
Lesson steps

Why there is a cache at all

Generating token 500 requires attending to the keys and values of tokens 1 to 499. Recomputing them every step would be quadratic and absurd, so they are computed once and kept. That store is the KV cache, and on any serious deployment it is the thing that runs out, not the weights.

The size is exact and worth memorising:

Text
bytes per token = 2 * num_layers * num_kv_heads * head_dim * bytes_per_element

The leading 2 is for keys and values. For Llama-3.1-8B (32 layers, 8 key-value heads, head dimension 128, fp16) that is 2 × 32 × 8 × 128 × 2 = 131,072 bytes per token, or 128 KB.

  • 8,000 tokens of context: about 1.0 GB
  • 32,000 tokens: about 4.2 GB
  • 128,000 tokens: about 16.8 GB, which is as much as the weights

Your place and first choices stay on this browser. Classroom credit is recorded separately.

Every lesson here is open. Your progress is recorded once you pass the Maths You Actually Need exam.

Stuck on something?Ask Anjali, your AI tutor

How to cite: Addaly (2026) ‘Attention variants, and the cache that eats your memory’, in The Open Model Ecosystem. Edited by M. Mun. Addaly. Available at: https://addaly.com/learn/open-models/attention-and-the-kv-cache (Accessed: 27 November 2026).