Ollama's context window — the silent 4096 wall
Ollama's default context window is 4096 tokens. A 15,437-token paste came back 200 with a fluent answer built from 2,050 read tokens — the head died, and only prompt_eval_count tells you.
Ollama's default context window is 4096 tokens, and it never announces itself. Paste longer: the server trims input, journals one WARN, and answers anyway. A 15,437-token paste came back 200, built from the last ~2,000 tokens — the input cap was 2,050. Only prompt_eval_count tells you.
The API returns 200. The CLI answers. prompt_eval_count is the only witness you can read.
What is ollama's default context window?
$ curl -s localhost:11434/api/ps | jq '.models[0] | {name, context_length}'
{
"name": "qwen3:4b",
"context_length": 4096
}
msg="vram-based default context" total_vram="0 B" default_num_ctx=40964096 is printed in the wrong places — not at load, not in the CLI banner, not in an error. ollama ps has a CONTEXT column; /api/ps calls it context_length, readable only after load. Fresh serve, zero options; the GPU-less box logs "no VRAM found, so 4096".
Why does ollama forget the middle of my document?
The trim precedes evaluation — server journal, not client:
Nov 07 ollama[638]: level=WARN source=llama_server.go:344 msg="truncating input prompt" limit=2,050 prompt=15437 keep=4 new=2,050 POST /api/generate → 200 … a fluent answer built from the kept tail
An 800-line doc, a unique safe word per line. Asked about line 0080, the model named its surviving strip: KILO-0696 to KILO-0800 — line 0080 was never read. At num_ctx 16384 the whole paste evaluated: 25 minutes on 4 threads.
| ask | truth (the doc) | window | prompt_eval_count | what came back |
|---|---|---|---|---|
| line 0080 | KILO-0080 | default 4096 | 2,050 — clamped | answered from the surviving strip, naming its own edges |
| line 0750 | PAPA-0750 | default 4096 | 2,050 — clamped | quoted kept line 0696 verbatim, walked toward 0750 until the 256-token answer bound cut it |
| line 0750 | PAPA-0750 | num_ctx 16384 | no clamp — whole paste evaluated | no answer printed: 25 minutes of prompt evaluation before the client was cut loose |
Witness rule: if the paste matters, read prompt_eval_count in every /api/generate and /api/chat response. Send 15,437 tokens, see 2,050 evaluated — that gap is the truncation.
How do I set the context window in ollama?
Three doors, all executed. Precedence: request option beats environment beats Modelfile beats 4096. The CLI has no context flag at ask time — terminal users get the env var or nothing.
| door | scope | where it lives | measured |
|---|---|---|---|
"options": {"num_ctx": N} | this request | request body | yes — ps flip + ask verified |
OLLAMA_CONTEXT_LENGTH | whole server | service environment | yes — ps flip after restart |
PARAMETER num_ctx N | one model, baked | Modelfile → ollama create | yes — ps flip on load |
| (nothing set) | everything | — | yes — 4096 observed |
Does a bigger context window cost memory?
Linearly — the KV cache reserves RAM per token of window whether you paste or not.
| window | result | RAM used | per-token arithmetic |
|---|---|---|---|
| 4096 | loaded | 3,599 MiB | baseline (weights 3.1 GiB every row) |
| 16384 | loaded | 5,397 MiB | +1,798 MiB ≈ 144 KiB/token |
| 32768 | loaded (24.6 s) | 7,531 MiB | +3,932 MiB ÷ 28,672 tokens ≈ 140 KiB/tok |
| 65536 | refused — HTTP 500 | — | attempted 9,663,672,576 B of cache; box said no |
of 15,437 pasted tokens were actually read — the window said 4096; the reserve ate half.
- read the window you got:
ollama ps→ CONTEXT column - halve it in your head: the input cap is ~half, the rest is saved for the reply
- pasting a document? one ask:
"options": {"num_ctx": …} - terminal-only workflow?
OLLAMA_CONTEXT_LENGTHon the service - after the ask, compare
prompt_eval_countto your paste size — a clamp means part of the doc was never read - before raising it far, price the RAM: ~144 KiB of cache per token, linear
- last tested 2026-11-07 · ollama 0.40.15 (Arch package) · 4 threads · 8 GiB VM · CPU-only bench — RAM numbers, not VRAM claims
- 800-line doc, unique safe word per line · 15,437-token paste · default window 4096, input cap 2,050
- all four window doors and all four RAM rungs executed on the bench; 65536 refused with HTTP 500