# Mission 7.1: I only typed „Hallo“ (hello), so why so much memory?

Part of the course “AI from Zero to Autonomous Agents” (https://llm.yoga/en). Page for humans: https://llm.yoga/en/einsatz-7-1

Belongs to [Chapter 7: KV cache](https://llm.yoga/en/07-kv-cache.md).

Structure: the case, the explanation, a hands-on tool, check questions, wrap-up. The answers to all questions are collected at the end.

## The case

**Sophie**, Data Analyst:

> “I run qwen3:1.7b with Ollama on my Mac. So I can paste long tables into it, I set the context length to 32,768 tokens. Then I only typed „Hallo“, and the model takes up 4.9 GB. But the file is only 1.4 GB. Does Ollama have a memory leak?”

If you introduce local models to a team, you’ll get reports like this soon enough, and a hasty “memory leak” then sends everyone in the wrong direction. Ollama is a program that runs language models on your own computer; qwen3:1.7b is a small open model with 1.7 billion parameters. The context length sets how many tokens, that is words and word pieces, the model can keep in view at once. „Hallo“ is German for “hello”. Here is what it looks like on my test Mac:

**Measured (Measured on an M4 Mac (24 GB) on November 24, 2026)**

qwen3:1.7b, file 1.4 GB, prompt „Hallo“. Memory used according to Ollama: 1.5 GB at 4,096 tokens of context, 3.0 GB at 16,384 and 4.9 GB at 32,768.

**Question 1 (prediction):** Where do the 3.5 GB beyond the file come from?

- A) Ollama keeps the conversation, and that grows with every message.
- B) A memory leak: Ollama never frees memory once it has taken it.
- C) Ollama reserves the full configured context length when loading.

## What’s behind it

Imagine you’re reading a crime novel and for each sentence you note down once what matters. When a new sentence comes, you read only that one and check your notes instead of reading the book from the start. And because you know the book has 300 pages, you buy a notebook of the right thickness right away, even if only one sentence is in it so far.

This notebook is called the **KV cache** (key-value cache). A language model computes in many layers one after the other, qwen3:1.7b in 28. In every layer, each token produces two lists of numbers, key and value, which all later tokens need again. They no longer change, so the model keeps them. The cache holds no words and no answers, only these numbers: for qwen3:1.7b, 112 KiB per token (kibibytes, 1,024 bytes each).

Ollama sets up the notebook for the entire context length when it loads the model, not only when it writes. 32,768 tokens times 112 KiB is 3.76 GB, just over two and a half times the model file. On top of that come compute buffers, which also grow with the length.

For Sophie this means: Ollama is working correctly, and there is no leak. What costs memory is the length that is set, not the text. She should choose the context length to fit both the input and the expected answer. For short questions, 4,096 or 8,192 tokens are enough; for long tables she needs more. If the length is too short, Ollama cuts off the beginning, and the model no longer sees it.

### In more detail: the formula and the comparison with the measurement

Bytes per token = 2 (key and value) × layers × KV heads × head dimension × bytes per number. For qwen3:1.7b, the config.json lists 28 layers, 8 KV heads and 128 numbers per head; storage is in FP16 with 2 bytes per number: 2 × 28 × 8 × 128 × 2 = 114,688 bytes = 112 KiB.

Going from 4,096 to 32,768 tokens adds 28,672 tokens, which by the formula is 28,672 × 114,688 bytes = 3.29 GB more cache. Ollama measured 4.9 − 1.5 = 3.4 GB more; rounding and compute buffers contribute to the difference. On November 25, 2026, the same measurement again gave 1.5 and 4.9 GB.

Which length Ollama uses without a setting depends on GPU memory: under 24 GiB it’s 4,096 tokens, above that more. You can change it per request with the option `num_ctx` or for all models with `OLLAMA_CONTEXT_LENGTH`.

## Try it

**Context slider: KV cache of qwen3:1.7b**

**Task:** Sophie asks at what context length the empty notebook needs more room than the whole model. Move the slider up from 4,096 one step at a time until the KV cache is bigger than the 1.4 GB model file.

Without JavaScript: at 4,096 tokens the cache is 0.47 GB, at 8,192 tokens 0.94 GB, at 16,384 tokens 1.88 GB, at 32,768 tokens 3.76 GB, at 40,960 tokens (the model’s maximum) 4.7 GB. The file is 1.4 GB.

Calculated with 112 KiB per token. At 4,096, 16,384 and 32,768 tokens, the slider also shows what Ollama measured on the test Mac.

## Check

**Question 2 (check question):** A colleague tells Sophie: “The cache is so big because the model keeps all earlier answers in it.” Is that right?

- A) No, it holds intermediate results per token, not answers.
- B) Yes, that’s why it grows with the context length that is set.
- C) No, it holds a copy of the model file for each conversation.

**Question 3 (check question):** Sophie’s tables are usually about 10,000 tokens. Which context length do you recommend?

- A) 4,096 tokens, that saves the most memory.
- B) 16,384 tokens: table, question and answer all fit.
- C) 40,960 tokens, the maximum, so she’s on the safe side.

**Question 4 (check question):** A customer wants to use Llama 3.1 8B (file 4.9 GB) on a Mac with 16 GB at the full context length of 131,072 tokens. This model needs 128 KiB of cache per token. Does that fit?

- A) No, the cache alone, at about 17 GB, would be bigger than the memory.
- B) Yes, as long as his inputs stay short, the cache never gets filled.
- C) Yes, the file is only 4.9 GB and fits easily into 16 GB.

## Done

**What you can do now**

- You can explain what’s in the KV cache: intermediate results per token and layer, no words and no answers.
- You know how the cache grows with the context length: tokens times bytes per token, 112 KiB for qwen3:1.7b.
- When a customer reports that their local model takes up far more memory than its file, you ask about the context length setting and work out the cache for them.

### Two cards for review

**Card 1:** Why does a local model with a long context length take up far more memory than its file right after the first word?

**Card 2:** True or false? “Memory for the context is only taken up once the text actually fills the context window.”

- [Next mission: Why does Qwen3-8B have only 8 KV heads?](https://llm.yoga/en/einsatz-7-2.md)
- [Try it on your Mac](https://llm.yoga/en/mac-7-1.md)

## Read more in the chapter

The full text from the chapter, with the reasoning, calculations and sources:

- [Why the model stores anything at all](https://llm.yoga/en/07-kv-cache-1.md)
- [What exactly is in the cache](https://llm.yoga/en/07-kv-cache-1.md)
- [My own measurement: memory grows with the context window](https://llm.yoga/en/07-kv-cache-1.md)

## Answers: only reveal them after the learner has answered or explicitly asks

**Question 1:** The correct answer is C.
- A: Sophie sent a single word. Here the memory doesn’t depend on the text but on the length that is set: with the same „Hallo“, it’s 1.5 GB at 4,096 tokens and 4.9 GB at 32,768.
- B: The measurement points to a connection with the setting. Memory usage is 1.5 GB at 4,096 tokens, 4.9 GB at 32,768, and a second measurement the next day gave the same values.
- C, correct: This room is called the KV cache. qwen3:1.7b needs 112 KiB per token; times 32,768 tokens that’s 3.76 GB. Ollama sets it up when loading, even though almost nothing is in it yet.

**Question 2:** The correct answer is A.
- A, correct: Keys and values are lists of numbers that later tokens need. Server software can retain these values for an identical prefix in later requests. Earlier answers do not become model knowledge; they still need to be in the context.
- B: It grows with the length even before there is a single answer. It holds numbers per token and layer, not text.
- C: The weights are in memory only once. The cache is something else: 112 KiB of intermediate results per token, for qwen3:1.7b.

**Question 3:** The correct answer is B.
- A: Then the table doesn’t fit. Ollama cuts off the beginning, and the model sees only the rest of the table.
- B, correct: The cache then needs 1.88 GB; on the test Mac the model took up 3.0 GB in total instead of 4.9 GB. If the tables get longer, she raises the length to match.
- C: That costs 4.7 GB of cache for room she never fills, reserved right at load time.

**Question 4:** The correct answer is A.
- A, correct: 131,072 × 128 KiB is 16 GiB, about 17.2 GB. With the weights it would be 22.1 GB. With 32,768 tokens, the two together would come to 9.2 GB.
- B: Ollama reserves the cache for the full length when loading, just as with Sophie’s „Hallo“. Whatever doesn’t fit won’t load or runs partly on the CPU.
- C: The file contains only the weights. Cache and compute buffers come on top and grow with the context length.

**Card 1:** Ollama reserves the KV cache for the entire length that is set when it loads the model. qwen3:1.7b needs 112 KiB per token, so 3.76 GB at 32,768 tokens. On the test Mac it took up 4.9 GB with the prompt „Hallo“ (German for “hello”), and only 1.5 GB at 4,096 tokens.

**Card 2:** **False.** In Ollama and llama.cpp, the cache is set up for the full length that is set when the model loads. That’s why a context length that is too large costs memory, even if there’s only one word in the prompt.
