# Mac exercise for mission 5.4: Let the code word get cut off

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

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

In the mission you moved the slider; here Ollama does the cutting itself. A small script sends qwen3:1.7b three messages: a code word, 79 sentences of filler text and the question about the code word. You choose the size of the context window and see how many tokens the model actually 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

If you still have qwen3:1.7b from the exercise for 5.1, skip this step. It also runs with 8 GB.

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

### 2. Save the script

Save these lines as `tulpe.py`. The number after the command becomes `num_ctx`, the size of the context window. With `ohne-fuell` (German for “without filler”), the script leaves out the filler text. It prints `prompt_eval_count`, the tokens the model actually read, and the answer. The messages are in German, as in the measurement: „Merke dir das Codewort Tulpe.“ (Remember the code word Tulpe; Tulpe means tulip), a filler sentence („Dieser Satz ist nur Fülltext ohne Inhalt.“, This sentence is just filler without content) and „Wie lautet das Codewort? Antworte mit einem Wort.“ (What is the code word? Answer with one word.)

```
import json, sys, urllib.request
URL = "http://localhost:11434/api/chat"
ctx = int(sys.argv[1])
fuell = " ".join(["Dieser Satz ist nur Fülltext ohne Inhalt."] * 79)
msgs = [{"role": "user", "content": "Merke dir das Codewort Tulpe."},
        {"role": "user", "content": fuell},
        {"role": "user", "content": "Wie lautet das Codewort? Antworte mit einem Wort."}]
if "ohne-fuell" in sys.argv:
    del msgs[1]
body = {"model": "qwen3:1.7b", "messages": msgs, "stream": False,
        "think": False,
        "options": {"num_predict": 20, "temperature": 0, "num_ctx": ctx}}
req = urllib.request.Request(URL, data=json.dumps(body).encode(),
                             headers={"Content-Type": "application/json"})
r = json.load(urllib.request.urlopen(req))
print(r["prompt_eval_count"], "Tokens:", r["message"]["content"])
```

### 3. With a window of 4,096 tokens

That’s Ollama’s default on computers with less than 24 GiB of GPU memory.

```
python3 tulpe.py 4096
```

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

```
1147 Tokens: Tulpe
```

### 4. With a window of 256 tokens

```
python3 tulpe.py 256
```

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

```
31 Tokens: Kryptogramm
```

No error message, no warning. Ollama left out the first two messages; the model read only the question and guessed („Kryptogramm“ is German for “cryptogram”). Your guessed word may be different; in the chapter it was „Code“.

### 5. Leave out the filler, keep the window small

```
python3 tulpe.py 256 ohne-fuell
```

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

```
41 Tokens: Tulpe
```

Code word and question fit into 41 tokens together. Leaving things out brings the code word back without the window growing.

### 6. How big is one sentence of filler?

Count a single sentence, without a chat template. Type it 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": "Dieser Satz ist nur Fülltext ohne Inhalt.", "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)**

```
14 Tokens
```

79 such sentences are 79 × 14 = 1,106 tokens, more than four times the window of 256. The message couldn’t fit, and everything before it dropped out along with it.

### 7. Let the page check your output

The check needs JavaScript. Without it: one sentence has 14 tokens, 79 sentences have 1,106.

The page counts the sentence with the same Qwen3 tokenizer in the browser and compares it with Ollama.

### 8. Cross-check: code word in the system prompt

In `tulpe.py`, change `"user"` to `"system"` in the first message, nothing else. Then run it with 256 again:

```
python3 tulpe.py 256
```

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

```
46 Tokens: Tulpe
```

Ollama kept the system prompt when trimming and left out only the filler text. So whatever must always apply belongs there. Change the line back afterward if you keep experimenting.

### 9. Clean up

Take the model out of memory:

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

If you don’t need it anymore, delete it from disk too:

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

#### On the DGX Spark

Ollama also runs on the DGX Spark; the playbook “Chat with LLMs Using Open WebUI and Ollama” sets it up together with a web interface. The script works the same way there, as long as Ollama is reachable at `localhost:11434`; otherwise you adjust the address in the second line.

According to the docs, Ollama’s default window depends on the GPU memory; with 48 GiB and up, it’s about 256,000 tokens. How Ollama classifies the Spark’s shared memory hasn’t been checked here. Because the script sets `num_ctx` explicitly, the result with 256 holds regardless. (Checked against the documentation, not run here)
