# Mac exercise for mission 9.1: Watching the KV cache fill up memory

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

About 10 minutes at your computer · Type this address: **llm.yoga/en/mac-9-1**

The rule-of-thumb formula says: on top of the weights comes the KV cache, and it grows with the context length. Here you see that for yourself on a small model. For qwen3:1.7b with full attention and an unquantized cache, the formula gives 112 KiB per token.

**Before you start**

You need **Ollama** and Python 3 in the terminal. Ollama comes as an app: download it from ollama.com/download and drag it into your Applications folder. On first launch, the app asks permission, if needed, to set up the `ollama` terminal command. It requires macOS 14 or newer. More on this in [chapter 10](https://llm.yoga/en/10-lokale-modelle-4.md). Check:

```
ollama --version
```

Python 3 may need to be installed. On the first `python3` call, macOS may offer to install developer tools; alternatively, Python is available from python.org.

### 1. Load a small model

```
ollama pull qwen3:1.7b
```

### 2. Start with 4,096 tokens of context

`num_ctx` sets the context length, `keep_alive` keeps the model in memory for three minutes. The prompt „Hallo“ (hello) is only there to load the model.

```
curl -s http://localhost:11434/api/generate -d '{"model": "qwen3:1.7b", "prompt": "Hallo", "stream": false, "think": false, "keep_alive": "3m", "options": {"num_ctx": 4096, "num_predict": 1}}' > /dev/null
```

```
ollama ps
```

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

```
NAME          ID              SIZE      PROCESSOR    CONTEXT    RUNNER      UNTIL
qwen3:1.7b    8f68893c685c    1.5 GB    100% GPU     4096       llamacpp    2 minutes from now
```

### 3. With 16,384 tokens of context

The same command, only `num_ctx` changed. Ollama reloads the model.

```
curl -s http://localhost:11434/api/generate -d '{"model": "qwen3:1.7b", "prompt": "Hallo", "stream": false, "think": false, "keep_alive": "3m", "options": {"num_ctx": 16384, "num_predict": 1}}' > /dev/null
```

```
ollama ps
```

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

```
NAME          ID              SIZE      PROCESSOR    CONTEXT    RUNNER      UNTIL
qwen3:1.7b    8f68893c685c    3.0 GB    100% GPU     16384      llamacpp    2 minutes from now
```

### 4. Compare with the formula

Paste both lines from `ollama ps`. The page calculates: 12,288 more tokens times 112 KiB is 1.4 GB of extra KV cache.

On the test Mac, memory grew from 1.5 to 3.0 GB, so by 1.5 GB. The difference from the estimated 1.4 GB can involve buffers and rounded displays. The exact breakdown was not measured.

### 5. Clean up

```
ollama stop qwen3:1.7b
```

For the 70B model from the mission it’s 320 KiB per token, almost three times as much. With 8 GB the exercise works the same way; stick to 16,384 tokens as the largest value and check that enough memory is free.
