# Mac exercise for mission 10.2: Really changing the context length

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

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

In the mission, Max set `OLLAMA_NUM_CTX`, and nothing happened. Here you try both names without changing your Ollama app: in the terminal, you start a second, short-lived server on another port and shut it down again afterward. The CONTEXT column of `ollama ps` shows whether it worked.

**Before you start**

You need **Ollama** 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
```

For this exercise you need two terminal windows: one for the second server, one for the commands.

### 1. Look at the default

Load qwen3:1.7b through the app, as in the exercise for 10.1, and take a look. The German prompt just asks the model to say „Hallo“ (hello):

```
ollama run qwen3:1.7b --think=false "Sag nur: Hallo"
```

```
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    4 minutes from now
```

4,096 tokens, even though the test Mac has 24 GB of memory. Ollama goes by the GPU memory that Metal reports, and that’s under 24 GiB.

### 2. Second server with the name from the video

In the first terminal window. `OLLAMA_HOST` sets the port so the second server doesn’t get in the app’s way. It stays in the foreground and writes its log to the window.

```
OLLAMA_NUM_CTX=8192 OLLAMA_HOST=127.0.0.1:11555 ollama serve
```

Right at the start, the log has a long `server config` line with all the settings the server knows. It contains `OLLAMA_CONTEXT_LENGTH:0`; `OLLAMA_NUM_CTX` doesn’t appear. A bit further down:

**Excerpt from the log (Measured on an M4 Mac (24 GB) on November 25, 2026)**

```
msg="vram-based default context" total_vram="17.8 GiB" default_num_ctx=4096
```

In the second terminal window:

```
OLLAMA_HOST=127.0.0.1:11555 ollama run qwen3:1.7b --think=false "Sag nur: Hallo"
```

```
OLLAMA_HOST=127.0.0.1:11555 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    4 minutes from now
```

No error, no warning, and still 4,096. Unload the model and stop the server in the first window with Control+C:

```
OLLAMA_HOST=127.0.0.1:11555 ollama stop qwen3:1.7b
```

### 3. The same server with the right name

In the first window:

```
OLLAMA_CONTEXT_LENGTH=8192 OLLAMA_HOST=127.0.0.1:11555 ollama serve
```

In the second window, the same two commands as before:

```
OLLAMA_HOST=127.0.0.1:11555 ollama run qwen3:1.7b --think=false "Sag nur: Hallo"
```

```
OLLAMA_HOST=127.0.0.1:11555 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    2.0 GB    100% GPU     8192       llamacpp    4 minutes from now
```

Now it’s 8,192 tokens, and memory has grown from 1.5 to 2.0 GB: the KV cache for the extra tokens needs room. In the log, Ollama now starts its bundled `llama-server` with `-c 8192` instead of `-c 4096`.

### 4. Let the page check it

Paste the two lines from `ollama ps`, the one with 4096 and the one with 8192. The page recalculates the increase with the rule of thumb from chapter 9: 112 KiB per token for qwen3:1.7b.

The check needs JavaScript. Without it: 4,096 more tokens times 112 KiB is 0.47 GB; measured, 0.5 GB more.

### 5. Just for one request

Without a second server, it also works for a single request, with the `num_ctx` parameter. This one goes to the app on the usual port:

```
curl -s http://localhost:11434/api/generate -d '{"model": "qwen3:1.7b", "prompt": "Hallo", "stream": false, "think": false, "options": {"num_ctx": 8192, "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    2.0 GB    100% GPU     8192       llamacpp    4 minutes from now
```

This only applies as long as every request includes the value. Without that value, model settings or the server default apply again.

### 6. Set it for the app

If you want the Ollama app itself to start with 8,192 tokens, according to the FAQ you set the variable for the app and then restart it:

```
launchctl setenv OLLAMA_CONTEXT_LENGTH 8192
```

(Checked against the documentation, not run here) On the test Mac, the app was left unchanged. Afterward, run `ollama ps` again: only the CONTEXT column shows whether it got through.

### 7. Clean up

Stop the second server in the first window with Control+C if it’s still running. Then unload the model in the app:

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

If you set the variable for the app and want the default back:

```
launchctl unsetenv OLLAMA_CONTEXT_LENGTH
```

(Checked against the documentation, not run here) Then restart the app.

With 8 GB, the exercise is also possible if enough memory is available. Values larger than 8,192 cost more memory; the rule of thumb from chapter 9 tells you beforehand how much.

#### On the DGX Spark

If Ollama runs on the Spark in the container from the playbook “Chat with LLMs Using Open WebUI and Ollama”, you pass environment variables when starting the container (`-e OLLAMA_CONTEXT_LENGTH=…` in the `docker run` command), not in the Spark’s terminal. As on the Mac, the default length Ollama picks there depends on the detected GPU memory; the Spark has 64 or 128 GB of unified memory. So there, too, look at the CONTEXT column first before you change anything. (Checked against the documentation, not run here)
