# Mission 10.2: OLLAMA_NUM_CTX is set, but nothing changes

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

Belongs to [Chapter 10: Local models in practice](https://llm.yoga/en/10-lokale-modelle.md).

Structure: the case, the explanation, a hands-on tool, check questions, wrap-up. The answers to all questions are collected at the end.

## The case

**Max**, sales engineer:

> “A video said a context length of 4,096 tokens is almost nothing, so set `OLLAMA_NUM_CTX=8192`. I did that and restarted Ollama. Ollama still shows 4,096 for the loaded model, and it forgets the beginning of long documents. Is my MacBook too small?”

Before judging the hardware, Max needs to check whether the setting reached the server. Ollama is a program that runs language models on your own computer. The context length says how many tokens, meaning words and word pieces, the model can see at once. Max has a MacBook with 24 GB of memory.

**Question 1 (prediction):** Why doesn’t anything change?

- A) Ollama doesn’t know the name and skips it without a message.
- B) The MacBook lacks the memory for 8,192 tokens, so Ollama sticks with 4,096.
- C) Ollama only picks up new settings after a restart of the whole Mac.

## What’s behind it

A program only reads environment variable names that its source code recognizes. An extra, unknown name can therefore have no effect without triggering an error.

**Environment variables** are settings a program receives at startup. Ollama looks for fixed names. For the context length, it’s `OLLAMA_CONTEXT_LENGTH`. `num_ctx` is the parameter name in a request or Modelfile. The mix of both from the video doesn’t exist; Ollama skips it without a warning.

Without explicit settings, the default depends on GPU memory. The docs list 4,096 tokens under 24 GiB and about 32,000 up to but not including 48 GiB. The source uses slightly lower thresholds. The test Mac has 24 GB, but Metal, Apple’s graphics interface, reported a recommended GPU working set of 17.8 GiB. This left the test at 4,096.

A second trap: an Ollama Mac app already running or launched through Finder does not automatically inherit variables later set in the terminal. According to the docs, you set them separately for the app and then restart the app.

For Max, this means: get the name right, set it in the right place, then check. Ollama shows the context length for every loaded model. More context costs memory for the KV cache, the model’s cache for everything it has read. On the test Mac, qwen3:1.7b, a small model of 1.4 GB, used 1.5 GB at 4,096 tokens and 2.0 GB at 8,192 tokens.

### In more detail: where to set the context length

Your own server in the terminal: `OLLAMA_CONTEXT_LENGTH=8192 ollama serve`. Mac app: according to the FAQ, `launchctl setenv OLLAMA_CONTEXT_LENGTH 8192`, then restart the app. A single request: `"options": {"num_ctx": 8192}`. In the chat of `ollama run`: `/set parameter num_ctx 8192`. A custom model: the line `PARAMETER num_ctx 8192` in the Modelfile, then `ollama create`.

On the test Mac, a second, short-lived Ollama server ran on port 11555 for this. With `OLLAMA_NUM_CTX=8192`, its startup line said `OLLAMA_CONTEXT_LENGTH:0`, and the model got 4,096 tokens. With `OLLAMA_CONTEXT_LENGTH=8192`, `ollama ps` showed 8192 and 2.0 GB.

The docs on context length recommend at least 64,000 tokens for agents and tools. For the 8B model considered here, that costs a lot of memory: 32,768 tokens alone need about 4.8 GB of KV cache.

## Try it

**Sorting: what reaches Ollama?**

**Task:** Max found seven ways in forums to set the context length to 8,192 tokens. Sort each one: does it take effect, or does Ollama skip it without a message?

Categories: takes effect · silently ignored

1. `OLLAMA_NUM_CTX=8192` when starting your own Ollama server in the terminal
2. `OLLAMA_CONTEXT_LENGTH=8192` when starting your own Ollama server in the terminal
3. `OLLAMA_CONTEXT_LENGTH=8192` set in the terminal while the Mac app provides the server
4. `num_ctx` 8192 in the options of a single request
5. `OLLAMA_NUM_CTX` set for the Mac app as the FAQ describes, and the app restarted
6. In Ollama’s terminal chat, setting the `num_ctx` parameter to 8,192 for this session
7. Creating a custom model whose blueprint (Modelfile) contains the line `PARAMETER num_ctx 8192`

## Check

**Question 2 (check question):** Max has now set the right name. How can he be sure it works?

- A) Ollama reports an error at startup if a setting doesn’t fit.
- B) By the context length Ollama shows for the loaded model.
- C) By the answers getting longer.

**Question 3 (check question):** A colleague wants to run the open model qwen3:8b with 8 billion parameters (5.2 GB) at 32,768 tokens on her MacBook with 16 GB. What do you tell her?

- A) No problem, a longer context length only costs a bit more compute time for reading.
- B) That won’t work; Ollama allows at most 8,192 tokens, regardless of the model.
- C) That’ll be tight: 5.2 GB of model plus about 4.8 GB of cache, plus a buffer.

## Done

**What you can do now**

- You recognize that Ollama skips unknown environment variables without a message, and you know the right name for the context length.
- You know that a setting also has to arrive in the right place: with your own server, in the request, or separately for the Mac app.
- When a customer says they copied a setting from a video and nothing changes, you first have them check which context length Ollama shows for the loaded model.

### Two cards for review

**Card 1:** Which environment variable sets how many tokens of context the Ollama server loads models with by default?

- A) OLLAMA_NUM_CTX
- B) OLLAMA_CONTEXT_LENGTH
- C) OLLAMA_KV_CACHE_TYPE

**Card 2:** True or false? “An unknown environment variable makes Ollama abort startup with an error.”

- [Next mission: Switching a script from the cloud to a local model](https://llm.yoga/en/einsatz-10-3.md)
- [Try it on your Mac](https://llm.yoga/en/mac-10-2.md)

## Read more in the chapter

The full text from the chapter, with the reasoning, calculations and sources:

- [Environment variables that matter](https://llm.yoga/en/10-lokale-modelle-1.md)
- [The GPU memory budget on a Mac](https://llm.yoga/en/10-lokale-modelle-2.md)
- [Common misconceptions](https://llm.yoga/en/10-lokale-modelle-4.md)

## Answers: only reveal them after the learner has answered or explicitly asks

**Question 1:** The correct answer is A.
- A, correct: `OLLAMA_NUM_CTX` doesn’t appear anywhere in Ollama’s source code. The right name is `OLLAMA_CONTEXT_LENGTH`. An unknown name doesn’t cause an error; it simply isn’t read.
- B: There was enough memory in the measured example: on the test Mac with 24 GB, a small model with 8,192 tokens used 2.0 instead of 1.5 GB. Ollama doesn’t even notice the setting.
- C: Restarting the Mac doesn’t help here: afterward, Ollama still looks for `OLLAMA_CONTEXT_LENGTH` and doesn’t find it. With the right name, restarting Ollama itself is enough.

- Card 1: **silently ignored:** Measured on the test Mac: the server started without errors, its settings line said `OLLAMA_CONTEXT_LENGTH:0`, and the model got 4,096 tokens.
- Card 2: **takes effect:** Measured on the test Mac: context 8,192, memory 2.0 instead of 1.5 GB. That’s the name Ollama looks for.
- Card 3: **silently ignored:** Right name, wrong place. The running app does not receive variables later set in the terminal. The FAQ describes a separate way for it, followed by restarting the app.
- Card 4: **takes effect:** Measured on the test Mac: the app loaded the model with 8,192 tokens for this request. This only applies as long as each request includes the value.
- Card 5: **silently ignored:** The place is right now, the name isn’t. The app, too, looks for `OLLAMA_CONTEXT_LENGTH`.
- Card 6: **takes effect:** The FAQ names exactly this way for the chat. It applies to the current session.
- Card 7: **takes effect:** In the Modelfile, the parameter is called `num_ctx`. This sets the model default to 8,192 tokens. A request can override it again.

**Question 2:** The correct answer is B.
- A: That does not hold for unknown environment variables: the wrong name was ignored without a message on the test Mac.
- B, correct: The list of loaded models has a column for the context. If it says 8192, the setting works. The memory value next to it goes up too, because the cache gets bigger.
- C: Context includes the input and output generated so far. Answer length also depends on the output limit and content, so it does not establish this setting.

**Question 3:** The correct answer is C.
- A: Above all, it costs memory. The cache grows with every token; for this model, it reaches about 4.8 GB at 32,768 tokens.
- B: Ollama has no such limit. According to its model card, qwen3:8b can handle about 40,000 tokens; the limit is set by memory.
- C, correct: With 16 GB, under the chapter’s cautious assumption, about 10.5 GB is available for the GPU. If Ollama keeps the cache in 8 bits (setting q8_0), it drops to about 2.4 GB, putting the estimate within the budget. The runtime must support cache quantization with Flash Attention.

**Card 1:** Correct: B, OLLAMA_CONTEXT_LENGTH OLLAMA_CONTEXT_LENGTH. OLLAMA_NUM_CTX doesn’t exist; Ollama skips it without a message. num_ctx is only the name of the parameter in a request or in the Modelfile. OLLAMA_KV_CACHE_TYPE sets the format the KV cache is stored in.

**Card 2:** **False.** Ollama does not read this unknown environment variable; it does not prevent startup. On the test Mac, a server with OLLAMA_NUM_CTX=8192 ran without a warning and loaded the model with 4,096 tokens. The context length shown for the loaded model tells you whether a setting works.
