# Mac exercise for mission 5.1: Watch the history grow

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

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

In the mission you did the math; here you see it for yourself. A small Python script asks qwen3:1.7b three questions and sends the whole history so far every time, just like a chat application. After each request, Ollama reports how many tokens the model read.

**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

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

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

### 2. Save the script

Save these lines as `chat.py`, for example with `nano chat.py` in the terminal. The list `msgs` is the whole history: after each question, the script appends the answer and sends everything together the next time. It needs only Python’s standard library. The prompts are in German, as in the measurement: the system prompt says „Du bist ein knapper Assistent. Antworte in einem Satz.“ (You are a concise assistant. Answer in one sentence.), and the three questions ask what a token is, what a context window is, and why a long chat costs more.

```
import json, urllib.request
URL = "http://localhost:11434/api/chat"
msgs = [{"role": "system",
         "content": "Du bist ein knapper Assistent. Antworte in einem Satz."}]
for frage in ["Was ist ein Token?", "Und was ist ein Kontextfenster?",
              "Warum kostet ein langer Chat mehr?"]:
    msgs.append({"role": "user", "content": frage})
    body = {"model": "qwen3:1.7b", "messages": msgs, "stream": False,
            "think": False,
            "options": {"num_predict": 40, "temperature": 0, "num_ctx": 4096}}
    req = urllib.request.Request(URL, data=json.dumps(body).encode(),
                                 headers={"Content-Type": "application/json"})
    r = json.load(urllib.request.urlopen(req))
    msgs.append({"role": "assistant", "content": r["message"]["content"]})
    print(r["prompt_eval_count"], r.get("prompt_eval_cached_count"))
```

### 3. Send three rounds

```
python3 chat.py
```

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

```
42 0
101 29
150 88
```

The first number is `prompt_eval_count`, the tokens the model read in this round: 42, 101, 150. The second is `prompt_eval_cached_count`, the part Ollama could reuse from the cache of the previous request. A second run right afterward gave 42 41, 101 88 and 150 88: the input stays the same, and the cache column depends on what ran before.

### 4. How much of it is the question?

Count the third question on its own. `"raw": true` sends the text without a chat template. Type the question into the field below, and it will already be in the command here.

```
curl -s http://localhost:11434/api/generate -d '{"model": "qwen3:1.7b", "prompt": "Warum kostet ein langer Chat mehr?", "raw": true, "stream": false, "options": {"num_predict": 1}}' | python3 -c 'import json, sys; print(json.load(sys.stdin)["prompt_eval_count"], "Tokens")'
```

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

```
10 Tokens
```

The question has 10 tokens; in the third round the model read 150. The rest is the history sent along: system prompt, the two earlier questions, the two answers and the control tokens of the chat template.

### 5. Let the page check your output

The check needs JavaScript. Without it: the question has 10 tokens, and the round read 150.

The page counts the question with the same Qwen3 tokenizer in the browser and compares. Subtract the 10 tokens from the 150, and what’s left is what you would pay only because of the history.

### 6. Let it keep growing

Add two or three questions of your own to the list in the script and run it again. The first number keeps rising with every round, even if your questions are short.

### 7. Clean up

Keep the model if you still want to do the exercise for 5.4. Otherwise:

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

#### On the DGX Spark

The playbook “Serve LLMs with SGLang” lists prefix caching with RadixAttention as a learning goal: several requests with the same beginning share the KV cache. With a model from the playbook’s list, such as Qwen3-8B, you can simulate a conversation over 20 rounds, use a long, fixed system prompt and measure the time to first token for each round. Compare two versions: one with a fixed system prompt and one with a timestamp in the first line. With the second, the prefix cache shouldn’t kick in. (Checked against the documentation, not run here)
