New Tool Radar

Local AI

Set a Larger Context Window for an Ollama Model with num_ctx

Raise the context window of an Ollama model with PARAMETER num_ctx in a Modelfile, and check the setting with ollama show.

Schematic with three steps: Add num_ctx to the Modelfile; Create the model; Verify the setting.

The context window is how many tokens a model can hold at once, and Ollama’s Modelfile default is 2048 tokens. Setting PARAMETER num_ctx in a Modelfile raises it for that model, at the cost of more memory for the KV cache.

What you need

  • Ollama installed and a base model pulled
  • A Modelfile, or a new one to create
  • Enough RAM or VRAM for the larger KV cache

Step 1: Add num_ctx to the Modelfile

In the Modelfile, under the FROM line, add PARAMETER num_ctx 8192. The documented default is 2048, so 8192 quadruples the window. Larger values such as 16384 or 32768 are possible if memory allows.

Step 2: Create the model

Run ollama create my-long-context -f Modelfile. The new model carries the larger context setting, while the base model keeps its default.

Step 3: Verify the setting

Run ollama show my-long-context and check the parameters list for num_ctx 8192. Then run the model and confirm long prompts no longer truncate early. ollama ps also shows the CONTEXT actually allocated for a running model.

Worked example: num_ctx 8192 for a long document chat

The table below is a synthetic example. The default of 2048 comes from the Modelfile docs; the 8192 value is the documented example size.

Setting Value Effect
Default num_ctx 2048 Short window, small KV cache
Raised num_ctx 8192 Four times the window
Check ollama show my-long-context Parameters list num_ctx 8192

Expected result (UNTESTED). ollama show lists num_ctx 8192, and a long prompt that was cut off at 2048 tokens now fits. This check was not run live; the default and the parameter name come from the cited Modelfile docs.

Limits

  • KV cache memory grows with context length, so a large num_ctx can push a model out of VRAM.
  • The setting applies per model; each model needs its own Modelfile entry.
  • A context window larger than the model was trained for can hurt answer quality.
  • The base model default stays 2048 unless a Modelfile changes it.

Sources

See all