Ollama Context Length (num_ctx) Guide
Fix Truncated Text and Forgotten Chats, Plus VRAM Math
Ollama cutting off long text or forgetting chats? Check CONTEXT in ollama ps, then set num_ctx by env var, Modelfile or API. Includes KV cache VRAM math.
When Ollama seems to ignore the second half of a long document, or a long chat forgets what you said earlier, the cause is usually not the model but the context length (num_ctx) being too small. Context length is the upper limit of text the model can keep in view at once, and Ollama lets you change it per server, per model or per request. A larger value, however, uses more VRAM, and once it no longer fits, part of the model spills to the CPU and slows down. This article covers how to recognize truncation, the current default according to Ollama's official docs, how to check the effective value with ollama ps, how to set it via the CLI, Modelfile, API and environment variables, approximate KV cache VRAM figures and KV cache quantization, and the OpenAI-compatible endpoint caveat. It sticks to what the official documentation confirms.
The short answer
- Check the CONTEXT column of ollama ps first — it shows the context length actually allocated. If it is smaller than you expected, that is your cause
- Five ways to change it — OLLAMA_CONTEXT_LENGTH for the whole server, PARAMETER num_ctx in a Modelfile for one model, /set parameter num_ctx in an interactive session, options.num_ctx in the API, and the slider in the app settings
- Bigger costs more VRAM — the KV cache grows in proportion to context length. Pick the largest value that keeps PROCESSOR at 100% GPU
- The OpenAI-compatible /v1 cannot set it — the official docs say the OpenAI API has no way of setting the context size, so use a model rebuilt from a Modelfile
What context length is
Context length (the context window) is the maximum number of tokens the model can reference in one inference. Your prompt, the chat history, the system prompt and even the answer being generated all share this window. Tokens are sub-word units, and Japanese tends to use more tokens per character than English, so the same amount of text fills the window faster.
In Ollama, the maximum context the model supports and the value Ollama actually allocates (num_ctx) are different things. A 128K model run at 4K only sees 4K.
Symptoms of truncation and forgetting
- You paste a long document and the summary never mentions the latter part
- Over a long chat, the model stops following the conditions or role you gave at the start
- You ask about row 3 of a table from earlier and get an invented answer
- You hand over a whole codebase and the answer only reflects the first file
- No error appears, so it is easy to miss
That last point is the tricky one. Text beyond the window is dropped silently, oldest first. A warning about truncation may appear in the server log, but the exact wording is not documented in the official docs, so we do not rely on it here. The reliable check is to look at the real allocation with ollama ps, below.
What is the current default?
The default has changed across versions, and different official pages state different numbers. As of December 1, 2026, this is what we found.
| Official page | What it says |
|---|---|
| Context length (docs.ollama.com/context-length) | The default depends on VRAM: 4k tokens under 24 GiB, 32k for 24-48 GiB, 256k for 48 GiB or more |
| FAQ (docs.ollama.com/faq) | The default context window is 4096 tokens; change it with OLLAMA_CONTEXT_LENGTH |
| Modelfile reference (docs.ollama.com/modelfile) | The num_ctx entry says Default: 2048 |
The mismatch most likely means some pages have not caught up. The Context length page describes the newest behavior: the default is chosen from VRAM. On a typical consumer GPU (under 24 GiB) you get 4k tokens, and machines with more VRAM get larger values automatically. The real value can still vary with version and model, so always confirm with ollama ps.
The same page recommends at least 64,000 tokens for web search, agents and coding tools.
How to check the effective context length
The CONTEXT column of ollama ps
With a model loaded, run this from another terminal. The official docs explain that the CONTEXT column shows the allocated context length and the PROCESSOR column shows whether the model runs on the GPU or is split with the CPU.
ollama run llama3.2 "hello" # run any model once to load it
ollama ps| Column | Meaning | What to check |
|---|---|---|
| CONTEXT | Allocated context length | Is it large enough for your task? |
| PROCESSOR | 100% GPU, 100% CPU, 48%/52% CPU/GPU, etc. | Has it left 100% GPU? |
ollama show and the logs
ollama show <model> prints model details and can help you see what context the model itself supports. The official CLI page we checked does not list its fields, so use ollama ps for the value actually in effect. The official troubleshooting page gives these log locations.
Log locations: ~/.ollama/logs/server.log on macOS, journalctl -u ollama --no-pager --follow --pager-end on Linux (systemd), docker logs <container-name> for Docker, and server.log in %LOCALAPPDATA%Ollama on Windows. OLLAMA_DEBUG=1 adds extra information.
How to set the context length
There are five ways, with different scopes. Pick by what you need.
| Method | Scope | Best for |
|---|---|---|
OLLAMA_CONTEXT_LENGTH (env var) | Server-wide default | Raising the floor for every model and client |
| App settings slider | Default in the desktop app | Changing it from a GUI on Mac or Windows |
PARAMETER num_ctx in a Modelfile | That model only | A model that should always run at the same length |
/set parameter num_ctx | The interactive session | Trying a value quickly in the CLI |
options.num_ctx in the API | That request | Choosing per call from code |
The OLLAMA_CONTEXT_LENGTH environment variable
The official FAQ shows setting the variable when starting the server, and the Context length page gives OLLAMA_CONTEXT_LENGTH=64000 ollama serve as an example.
# Starting manually
OLLAMA_CONTEXT_LENGTH=8192 ollama serve
# For context-heavy work
OLLAMA_CONTEXT_LENGTH=64000 ollama serveWhere to set environment variables differs for systemd, the Mac app and Windows. See the environment variable section of Ollama Not Using Your GPU or Running Slow? for the steps. On Linux with systemd, add Environment="OLLAMA_CONTEXT_LENGTH=32768" via systemctl edit ollama.service, then run daemon-reload and restart.
/set parameter num_ctx in the CLI
During ollama run, you can change it with /set parameter num_ctx, as the official FAQ describes. It applies only to that session.
ollama run llama3.2
>>> /set parameter num_ctx 8192PARAMETER num_ctx in a Modelfile
The Modelfile reference shows this form. The model you create then always runs at that length.
FROM llama3.2
PARAMETER num_ctx 8192ollama create llama3.2-8k -f ./Modelfile
ollama run llama3.2-8kWhen you use Open WebUI or other clients, selecting this rebuilt model is the most dependable approach. It also works around the OpenAI-compatible endpoint limitation described next.
options.num_ctx in the API
Per the official FAQ, put num_ctx inside the options object. This works with the native /api/chat and /api/generate endpoints.
curl http://localhost:11434/api/chat -d '{
"model": "llama3.2",
"messages": [{"role": "user", "content": "Summarize this long document"}],
"options": { "num_ctx": 8192 }
}'The OpenAI-compatible endpoint (/v1) caveat
The OpenAI-compatible /v1/chat/completions cannot take num_ctx. The official OpenAI compatibility page states that the OpenAI API has no way of setting the context size for a model. To change it, create a model with PARAMETER num_ctx in a Modelfile using ollama create, then pass that model name in the model field. The page's supported request fields do not include a context size setting either. Raising the server default with OLLAMA_CONTEXT_LENGTH also works.
If you call Ollama through an OpenAI-compatible client library such as LangChain or an agent tool and long inputs get cut, you are probably hitting this limit. Adding num_ctx on the library side does not reach Ollama through /v1.
Context length and VRAM (the KV cache)
Increasing the context length grows the KV cache (memory holding the computed state of earlier tokens), separate from the model weights. The KV cache scales in proportion to context length. The official Context length page also says a larger context length increases the memory needed to run a model.
A rough formula:
KV cache bytes ≈ 2 × layers × KV heads × head dimension × context length × bytes per element (2 for f16)
Here are estimates for a typical 7B-class model (32 layers, 8 KV heads, head dimension 128) and a 70B-class model (80 layers, 8 KV heads, head dimension 128). Architectures differ, so treat these as rough guides only.
| Context length | 7B f16 | 7B q8_0 | 7B q4_0 | 70B f16 |
|---|---|---|---|---|
| 4,096 | about 0.5 GB | about 0.3 GB | about 0.15 GB | about 1.3 GB |
| 8,192 | about 1.1 GB | about 0.5 GB | about 0.3 GB | about 2.7 GB |
| 32,768 | about 4.3 GB | about 2.1 GB | about 1.1 GB | about 10.7 GB |
| 131,072 | about 17 GB | about 8.6 GB | about 4.3 GB | about 43 GB |
These figures are approximate; model weights, compute buffers and parallel requests come on top. The ratios (q8_0 about half of f16, q4_0 about a quarter) follow the official FAQ. To estimate whether weights plus cache fit before you try, use the VRAM calculator. For how quantization shapes the weights themselves, see the GGUF quantization guide.
Saving VRAM with KV cache quantization
The official FAQ lets you choose the K/V cache quantization with OLLAMA_KV_CACHE_TYPE, and says it applies when Flash Attention is enabled.
| Value | Memory (per official docs) | Accuracy impact (per official docs) |
|---|---|---|
f16 (default) | Highest | Highest precision |
q8_0 | About half of f16 | Very small loss |
q4_0 | About a quarter of f16 | Small to medium loss, which can be more noticeable at larger contexts |
According to the FAQ, Flash Attention turns on automatically when supported; OLLAMA_FLASH_ATTENTION=1 forces it on and 0 turns it off. Note that the KV cache type applies globally to every model.
Try q8_0 first and move to q4_0 only if it still does not fit. Quality differences depend on your use, so test with your usual prompts.
Why too large a context slows things down
If model plus KV cache exceeds VRAM after you raise the context, Ollama places the overflow in system memory, and PROCESSOR in ollama ps shows a split such as 48%/52% CPU/GPU. The official Context length page also warns that CPU offload hurts performance and tells you to check the PROCESSOR column.
OLLAMA_NUM_PARALLEL matters too. The official FAQ says parallel processing increases the context size by the number of parallel requests; its example is a 2K context with 4 parallel requests becoming 8K. The default is 1 on most backends, but raising it multiplies memory use together with context length. For a full walkthrough of GPU and slowness problems, see Ollama Not Using Your GPU or Running Slow?.
Recommended settings by use case
| Use case | Context length guide | Notes |
|---|---|---|
| Short questions and chat | 4,096-8,192 | The default is enough and saves VRAM |
| Summarizing or translating documents | 16,384-32,768 | Japanese uses more tokens per character, so leave headroom |
| Long chats and role play | 16,384-32,768 | Grows with history. Start fresh chats now and then |
| RAG | 8,192-32,768 | Estimate chunks x length, plus question and answer |
| Coding and agents | 64,000 or more | The official recommendation. Pair with KV cache quantization if VRAM is short |
| Low VRAM (8-12 GB) | 4,096-8,192 | A smaller model helps more than stretching the context |
To pick a value, first find the largest context length that keeps ollama ps at 100% GPU, then tune within that range. If you cannot stretch it, adjust in this order: smaller model, lower-bit weight quantization, then q8_0 KV cache.
Troubleshooting table
| Symptom | Likely cause | Fix |
|---|---|---|
| Latter part of a long text is ignored | num_ctx too small (e.g. 4k default) | Check CONTEXT in ollama ps, then raise via OLLAMA_CONTEXT_LENGTH or a Modelfile |
| Early instructions forgotten | History exceeds the window and drops oldest first | Raise num_ctx, or start a new chat and restate the key points |
| Client setting has no effect | num_ctx does not reach Ollama via OpenAI-compatible /v1 | Build a model with num_ctx in a Modelfile, or set OLLAMA_CONTEXT_LENGTH |
| Suddenly slow after raising it | VRAM overflow, CPU split | Check PROCESSOR; quantize the KV cache, use a smaller model or lower the context |
| Out of memory with parallel requests | OLLAMA_NUM_PARALLEL multiplies context | Set it back to 1 |
Summary
When Ollama truncates long documents or forgets earlier turns: (1) read the real allocation in the CONTEXT column of ollama ps, (2) raise it with OLLAMA_CONTEXT_LENGTH, a Modelfile, /set parameter or options.num_ctx, and (3) confirm PROCESSOR stays at 100% GPU. If it does not fit, adjust with KV cache quantization (start with q8_0), a smaller model, or fewer parallel requests. num_ctx cannot be passed through the OpenAI-compatible /v1, so use a model built from a Modelfile. If you are still choosing a local tool, the Ollama vs LM Studio comparison may help.
References
- Ollama docs: Context length
- Ollama docs: FAQ
- Ollama docs: Modelfile reference
- Ollama docs: OpenAI compatibility
- Ollama docs: Troubleshooting
FAQ
What is Ollama's default context length?
The official Context length page says it depends on VRAM: 4k tokens under 24 GiB, 32k for 24-48 GiB and 256k for 48 GiB or more. The FAQ also states 4096 tokens, so pages differ. Confirm your real value in the CONTEXT column of ollama ps.
Does raising num_ctx always make Ollama slower?
Not while everything fits in VRAM. Once the KV cache pushes past VRAM and ollama ps shows a CPU/GPU split in PROCESSOR, speed drops sharply. Find the largest value that stays at 100% GPU.
Can I set num_ctx through the OpenAI-compatible endpoint?
No. The official OpenAI compatibility page says the OpenAI API has no way of setting the context size. Create a model with PARAMETER num_ctx in a Modelfile via ollama create and use that model name, or change the server default with OLLAMA_CONTEXT_LENGTH.
Does KV cache quantization hurt quality?
Per the official FAQ, q8_0 uses about half the memory of f16 with a very small precision loss, and q4_0 uses about a quarter with a small to medium loss that may be more noticeable at larger contexts. It applies when Flash Attention is enabled.
株式会社オブライト
Oflight Inc. is a Tokyo-based IT company providing software development, digital signage, web development, network systems, and IoT/AR/VR technology services.
Related free tools (no sign-up, instant results)
Feel free to contact us
Contact Us