New Tool Radar

AI Concepts

How to Set a Longer Context Window in Ollama with num_ctx

Give an Ollama model a longer context window with num_ctx when a long conversation or document gets cut off, using /set parameter for one session or a Modelfile for every session.

Schematic diagram with three ways to set num_ctx 8192: /set parameter for one session, options for one request, and PARAMETER in a Modelfile for every session.

The context window is how much text a loaded model can hold in memory at once. Ollama’s FAQ says that by default Ollama uses a context window size of 4096 tokens. If your conversation or document is longer than that, the earliest part drops out of reach and the model starts forgetting what you said at the start. The fix is to raise the window with num_ctx — but only as far as your computer’s memory allows.

Three places to set num_ctx

Ollama’s FAQ gives three ways to change the context window, and they last for different lengths of time.

Where you set it How How long it lasts
One chat session Inside ollama run, type /set parameter num_ctx 8192 That session only
One API request Send num_ctx in the request options, for example "options": {"num_ctx": 8192} That request only
Every session with a model In a Modelfile, write PARAMETER num_ctx 8192, then build the model with ollama create Every run of that model
Server default for all models Set the OLLAMA_CONTEXT_LENGTH environment variable, for example to 8192 Until the server setting is changed

The Modelfile reference lists num_ctx as the parameter that sets the size of the context window used to generate the next token, with the example num_ctx 4096. A Modelfile that bakes in a longer window looks like this:

FROM llama3.2
PARAMETER num_ctx 8192

Build it with ollama create llama3.2-8k -f ./Modelfile and run it with ollama run llama3.2-8k.

Try it: double the window for one session

Start a chat with ollama run llama3.2, type /set parameter num_ctx 8192, and then paste in a long fictional note — for example a practice letter from a lighthouse keeper named Mara, about the lamp, the supply boat, and the winter rota — and ask a question about a detail from the start of the note. If the model answers from the start of the note, the longer window is in use for that session. Expected result. We checked the Ollama FAQ and Modelfile reference. We did not run Ollama or set num_ctx on this machine.

Limits

A longer context window uses more memory, so doubling the window does not come free: on a computer that is already short of memory, a large window can slow the model down or stop it loading. The window also cannot give a model more than the model itself supports — the gemma3 tags page, for example, lists a 128K context window for the 4B, 12B, and 27B models and 32K for the 1B — so check the model’s own listed window before raising num_ctx. We did not measure memory use ourselves.

Sources

See all