In Windows, the Ollama context window length (also known as context size or maximum sequence length) is controlled via the model's configuration or by setting environment variables when running Ollama. Here's how you can manage it and why you wouldn't always make it as large as possible.
✅ How to Set the Ollama Context Window Length in Windows
There are two main ways to adjust the context window:
1. Using the OLLAMA_NUM_CTX Environment Variable
Ollama uses environment variables to control model parameters. To change the context window length:
- Open System Properties:
- Press
Win + R, type sysdm.cpl, and press Enter.
- Go to the Advanced tab → Environment Variables.
- Under System Variables (or User Variables), click New.
- Add:
- Variable name:
OLLAMA_NUM_CTX
- Variable value: Desired context size (e.g.,
4096, 8192, 32768 — depending on model support)
- Save and restart any command prompts or Ollama services.
Alternatively, set it temporarily in a terminal:
set OLLAMA_NUM_CTX=8192
ollama run llama3
Or in PowerShell:
$env:OLLAMA_NUM_CTX = 8192
ollama run llama3
⚠️ Note: This setting applies to new model sessions. You may need to re-run the model after setting the variable.
2. Modify the Model Modelfile (Custom Model)
If you're using a custom model, you can bake in the context size via a Modelfile:
FROM llama3
PARAMETER num_ctx 8192
Then create and run your model:
ollama create mymodel -f Modelfile
ollama run mymodel
❌ Why Not Make the Context Window as Large as Possible?
While a larger context window allows the model to "remember" more input (e.g., longer documents or conversations), there are important trade-offs:
1. Increased Memory (RAM/VRAM) Usage
- The memory required grows quadratically with context length due to attention mechanisms.
- Large contexts can exhaust GPU or system memory, especially on consumer hardware.
2. Slower Inference Speed
- More tokens = more computation per response.
- Longer context increases latency, especially during generation.
3. Diminishing Returns
- Models may struggle to effectively use extremely long contexts (e.g., 32k+ tokens).
- Relevant information can get "lost" in the middle (a known issue in transformer models).
4. Model Limitations
- Not all models support arbitrarily large contexts. For example:
- Llama 3 typically supports up to 8192 tokens by default.
- Some fine-tuned variants (e.g.,
llama3:70b-instruct) may support 32k, but only if the model was trained for it.
- Setting
num_ctx beyond what the model supports can lead to instability or poor performance.
5. Higher Disk & Bandwidth Usage
- Larger context may require more data transfer and caching, especially when using APIs or remote clients.
✅ Best Practices
- Match context size to your use case:
- Chat/conversation: 4k–8k is usually sufficient.
- Long document analysis: 16k–32k (if supported and hardware allows).
- Monitor memory usage in Task Manager or via
ollama serve logs.
- Test performance with realistic inputs before scaling up.
🔍 Check Current Context Settings
You can inspect model details with:
ollama show llama3 --modelfile --parameters
Look for num_ctx in the output.
Summary
- ✅ Set context length via
OLLAMA_NUM_CTX or PARAMETER num_ctx in a Modelfile.
- ❌ Don’t max it out unnecessarily — balance performance, memory, and utility.
- 💡 Use only as much context as your application truly needs.