# Mac exercise for mission 7.1: Context length and memory

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

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

In the mission, Sophie’s model took up 4.9 GB at 32,768 tokens of context, even though the file is 1.4 GB. Here you load the same model twice with the prompt „Hallo“ (German for “hello”, as in the measurement), once with a short and once with a long context length, and compare the increase with the formula. qwen3:1.7b needs 112 KiB of KV cache 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. Download a small model

qwen3:1.7b is 1.4 GB and runs on any Mac with Apple silicon. If you already have it from another exercise, skip this step.

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

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

The command sends „Hallo“ to Ollama’s API. `num_ctx` sets the context length, `num_predict` 1 makes the model write only one token, and `keep_alive` keeps it in memory for three minutes.

```
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 25, 2026)**

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

The `SIZE` column shows how much memory the loaded model takes up: 1.5 GB, hardly more than the file.

### 3. With Sophie’s 32,768 tokens

The same command, with only `num_ctx` changed. Ollama reloads the model for this. With 8 GB of RAM, use 16,384 instead of 32,768.

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

```
ollama ps
```

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

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

The same prompt, 3.4 GB more. Almost nothing is in memory yet, but the room is reserved anyway.

### 4. Let the page check it

Paste both lines from `ollama ps`. The page checks the increase against 112 KiB per token.

The check needs JavaScript. Without it: 28,672 more tokens times 112 KiB is 3.3 GB of cache; the measurement was 4.9 − 1.5 = 3.4 GB.

Rounding and compute buffers, which also grow with context length, contribute to the difference from the formula. The page filters for qwen3:1.7b, so lines from other models don’t get in the way.

### 5. Do the math yourself

What length do you really need? Multiply tokens by 114,688 bytes. For 8,192 tokens:

```
python3 -c "print(8192 * 114688 / 1e9, 'GB')"
```

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

```
0.939524096 GB
```

### 6. Clean up

Keep the model if you still want to do the exercise for 7.3. Otherwise take it out of memory and, if you don’t need it anymore, off the disk:

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

```
ollama rm qwen3:1.7b
```

#### On the DGX Spark

On a Spark with 128 GB you can run the same measurement with a large model and real long context, for example with gpt-oss:120b or a 30B model at 131,072 tokens. The playbook “Open WebUI with Ollama” sets up Ollama on the Spark. The interesting comparison is between a model with a sliding window in some of its layers (gpt-oss) and one without (Qwen3): which one needs less cache at the same context length? Work it out beforehand with the config.json, then check with `ollama ps`. (Checked against the documentation, not run here)
