Ollama context length: the default, why your local model forgets, and what 128K costs in VRAM
On any GPU under 24 GB, Ollama gives a model a 4,096-token context by default. Longer chats lose their oldest messages, and a long paste is cut to about half the window, with nothing in the chat to tell you. How the default changed (2048, 4096, then by VRAM), five ways to change it, and the KV cache arithmetic for 4K to 128K.
Run a model in Ollama on a 16 GB graphics card, paste in a long document, ask about the first section, and the answer can sound oddly vague. The model isn’t being lazy. On any GPU with less than 24 GB of memory, current Ollama gives the model a 4,096-token context window by default, and anything that doesn’t fit is dropped before the model sees it. Your chat window doesn’t tell you.
This post covers what the default actually is (it changed three times), what gets dropped, the five ways to change it, and what a bigger window costs in memory. Everything below comes from Ollama’s docs and source code (version 0.40.18, November 2026) and from published model configs. The memory figures are arithmetic, not measurements. Nothing was benchmarked for this post.
The context window is a desk
The context window is everything the model can see while it writes: the system prompt, your messages, its own replies, and any document you pasted. Think of it as a desk. Pages on the desk can be read. Pages that fall off the far edge are gone, because the model has no memory beyond what’s on the desk.
4,096 tokens is smaller than it sounds. OpenAI’s rule of thumb for English is 100 tokens ≈ 75 words, so 4,096 tokens is about 3,000 words, roughly six printed pages. The system prompt and the model’s own answers come out of the same budget.
What Ollama drops, and where it tells you
There are two cases, both visible in the source:
| Situation | What happens (Ollama 0.40.18) | Where it’s reported |
|---|---|---|
| A chat grows past the window | Whole messages are removed from the front until it fits. System messages and the latest message are always kept (server/prompt.go). |
A DEBUG line in the server log, which you only see with OLLAMA_DEBUG=1 |
| One prompt is longer than the window | The first num_keep tokens (default 4, plus the start token) are kept, then the prompt is cut to about half the window, so there’s room left for the reply (llm/llama_server.go). |
A WARN line, “truncating input prompt”, in the server log |
Same, with "shift": false in the request |
Error 400: “the prompt is longer than the context length currently available to the model” | The API response |
The second row is the one that surprises people. Paste a 10,000-token document into a 4,096-token window and the arithmetic in the code is 4096 − (4096 − 5) / 2 = 2,051 tokens kept: the first 5 tokens and the last 2,046. About 80% of the document never reaches the model. The chat history rule is gentler, but it’s just as quiet: the start of a long conversation simply stops existing.
The default changed three times
| Ollama version | Default context | Source |
|---|---|---|
| up to 0.6.6 (April 2025) | 2,048 tokens | api.DefaultOptions, envconfig |
| 0.5.13 (March 2025) | OLLAMA_CONTEXT_LENGTH env var added |
commit “config: allow setting context length through env var” |
| 0.6.7 (April 2025) | 4,096 tokens | commit “config: update default context length to 4096” |
| 0.15.5 (February 2026) and later | 4K / 32K / 256K, by total VRAM | commit “server: use tiered VRAM-based default context length” |
The docs haven’t fully caught up. On the same docs site, the Modelfile reference still lists num_ctx with “(Default: 2048)”, the FAQ says “By default, Ollama uses a context window size of 4096 tokens”, and the Context length page gives the VRAM tiers. The source code agrees with the last one.
Some details from server/routes.go and server/sched.go:
- The thresholds in code are 23 and 47 GiB (“to account for small differences in the exact value”), measured as the sum of all GPUs’ memory minus
OLLAMA_GPU_OVERHEAD. No GPU counts as zero, so CPU-only machines get 4K. - The automatic default never goes above the model’s own trained context (for example 128K for
llama3.1:8b, 40K forqwen3:8b, per ollama.com/library). - If loading runs out of memory with the automatic value, the scheduler steps down: 256K, then 32K, then 4K.
- So a 16 GB card still gets 4K unless you change it.
On Macs, models running on the MLX engine skip the chat-history truncation step, so behaviour there can differ from the table above.
Five ways to change it
| Where | How | Scope |
|---|---|---|
| Ollama app | Settings → Context length slider (4k, 8k, 16k, 32k, 64k, 128k, 256k) | Server default (the app passes it as OLLAMA_CONTEXT_LENGTH) |
| Server | OLLAMA_CONTEXT_LENGTH=32768 ollama serve |
Server default |
ollama run |
/set parameter num_ctx 32768 |
This session |
| API | "options": {"num_ctx": 32768} in /api/chat or /api/generate |
This request |
| Modelfile | PARAMETER num_ctx 32768, then ollama create llama3.1-32k -f Modelfile |
Every use of that model name |
The most specific setting wins: the request’s options override the Modelfile, which overrides the environment variable (or the app slider), which overrides the VRAM tier.
One trap: the OpenAI-compatible endpoint (/v1/chat/completions) has no field for it. The compatibility docs say “The OpenAI API does not have a way of setting the context size for a model” and recommend a Modelfile with PARAMETER num_ctx, called by the new model name. Many tools talk to Ollama through that endpoint.
To see what you actually got, run ollama ps. The CONTEXT column shows the allocated context length and PROCESSOR shows where the model lives: 100% GPU, 100% CPU, or a split like 48%/52% CPU/GPU (FAQ). The Context length page’s advice: “use the maximum context length for a model, and avoid offloading the model to CPU.”
What a bigger desk costs
Every token on the desk gets an entry in the KV cache, the model’s stored keys and values for every layer. The size per token comes straight from the model’s config.json:
layers × KV heads × head dimension × 2 (K and V) × bytes per value
For Llama 3.1 8B Instruct that’s 32 layers, 8 KV heads and 128 dimensions per head. At f16 (2 bytes, Ollama’s default cache type), that comes to 131,072 bytes, or 128 KiB per token.
| Context | KV cache, f16 | KV cache, q8_0 | KV cache, q4_0 |
|---|---|---|---|
| 4K (4,096) | 0.5 GiB | 0.27 GiB | 0.14 GiB |
| 32K (32,768) | 4 GiB | 2.1 GiB | 1.1 GiB |
| 64K (65,536) | 8 GiB | 4.3 GiB | 2.3 GiB |
| 128K (131,072) | 16 GiB | 8.5 GiB | 4.5 GiB |
At the model’s full 128K, the cache alone is more than three times the 4.9 GB model file. Other models differ: Qwen3 8B has 36 layers (144 KiB per token), and Qwen2.5 7B has only 4 KV heads (56 KiB per token). Redo the multiplication for your model before you drag the slider.
Folding the notes: KV cache quantization
Ollama can store the cache at lower precision with OLLAMA_KV_CACHE_TYPE. The FAQ describes q8_0 as using “approximately 1/2 the memory of f16 with a very small loss in precision”, and q4_0 as “approximately 1/4” with a “small-medium loss in precision that may be more noticeable at higher context sizes”. In ggml, a q8_0 block stores 32 values in 34 bytes and q4_0 stores them in 18, which is where the 53% and 28% in the table come from (ggml-common.h). Three caveats from the docs: it only works with flash attention (Ollama turns it on automatically where the backend supports it; OLLAMA_FLASH_ATTENTION=1 forces it), it’s a global setting for every model, and models with a high GQA ratio can lose more quality.
Parallel requests also multiply the cache. The FAQ: “a 2K context with 4 parallel requests will result in an 8K context”. The default is 1 parallel request on most backends.
A 16 GB recipe (estimates)
Model file plus KV cache for llama3.1:8b (about 4.6 GiB of weights). Leave a gigabyte or two for compute buffers and your desktop:
| Setting | Estimated total | Fits 16 GB? |
|---|---|---|
| 32K, f16 cache | ≈ 8.6 GiB | yes |
| 64K, f16 cache | ≈ 12.6 GiB | yes, with less headroom |
| 128K, q8_0 cache | ≈ 13.1 GiB | yes, with less headroom |
| 128K, f16 cache | ≈ 20.6 GiB | no: part of the model moves to system RAM |
If ollama ps shows a CPU/GPU split after you raise the context, either step the context down or quantize the cache, and check again until it says 100% GPU.
Sources
- Ollama docs: Context length, FAQ, Modelfile, OpenAI compatibility
- Ollama source, v0.40.18: server/routes.go, server/prompt.go, llm/llama_server.go, server/sched.go, envconfig/config.go; version history from the repository’s git tags
- Model configs: Llama 3.1 8B Instruct, Qwen3 8B, Qwen2.5 7B Instruct; model sizes from ollama.com/library
- ggml-common.h (q8_0 and q4_0 block sizes)
- OpenAI Help Center, What are tokens and how to count them