In Ollama, num_ctx is the option that sets the context length: the number of tokens a loaded model can hold in memory at once. You can set it for one request, in a Modelfile for one model, or server-wide with the OLLAMA_CONTEXT_LENGTH environment variable. A per-request setting always wins, so start there for a single long document.
Prerequisites
- Ollama running, with a model loaded.
- The document’s token count.
- A way to send an API request or use the interactive session.
Steps
Step 1: Check the current value
Run ollama ps and read the CONTEXT column for the loaded model. That is the window your document must fit, or the size you must raise.
Step 2: Set num_ctx for one request
Send the request with num_ctx in the options object, for example options with num_ctx 16384, to the generate or chat endpoint. Or, inside an interactive ollama run session, type /set parameter num_ctx 16384 for that session only.
Step 3: Confirm it loaded
Run ollama ps again and check the CONTEXT column shows the new value. If the value differs from what the loaded model is running with, the server reloads the model first, which you can see in the load_duration of the response.
Worked example: Bluebell Bakery 12,000-token supplier contract
The fictional Bluebell Bakery needs to ask about a 12,000-token supplier contract. The running context is 4,096 tokens.
| Step | num_ctx | CONTEXT column | Contract fits |
|---|---|---|---|
| Before | Default | 4096 | No |
| Set per request | 16384 | 16384 | Yes |
| Ask the question | 16384 | 16384 | Answered from the contract |
Setting num_ctx to 16384 raises the running context from 4,096 to 16,384 tokens, and the 12,000-token contract fits with room for the question and the answer.
Expected result. The CONTEXT column shows the new value after you set it, and the long document fits. UNTESTED: this guide was not run against a live model or API. The worked example below is fictional practice material, and its result is the expected result, not a measured one.
Limits
- A larger num_ctx needs more memory for the KV cache; check that the model still fits on the GPU.
- The Modelfile and server-wide options exist too; this guide covers the per-request setting.
- Older guides use OLLAMA_NUM_CTX; the current server variable is OLLAMA_CONTEXT_LENGTH.
