Context Length – explained simply
The context length determines how many tokens a language model can consider at once, meaning how much of the conversation it keeps in view.
Also: Context Window, Context Length, Context Window, num_ctx
Everything the model should consider right now has to fit in the context: the system prompt, the prior chat history, inserted documents, and the response. Think of the context as a desk that can only hold a certain amount of paper. Whatever falls off, the model forgets.
More context needs more memory. That’s why Ollama adjusts the default value to your VRAM: below 24 GB it is 4,096 tokens, above that it is more. You can change it in the app settings, via the OLLAMA_CONTEXT_LENGTH environment variable, or in the Modelfile with PARAMETER num_ctx. For agents, web search, and programming tools, Ollama recommends at least 64,000 tokens.
A large context can cause parts of the model to be swapped out to RAM and make everything slower.
What does that mean for you? If your model forgets long texts or earlier parts of the conversation, increase the context length and then check with ollama ps whether everything is still running on the GPU.
Related terms
Errors where that matters
- KoboldCpp closes immediately while loading
- llama-server: the request exceeds the available context size, try increasing it (request longer than the context length)
- llama.cpp: CUDA error: out of memory
- LM Studio: Error loading model. (Exit code: …) Unknown error (model won’t load)
- LM Studio: Model loading was stopped due to insufficient system resources
- LM Studio: This message contains no content. The AI has nothing to say. (Reply remains empty)
- LM Studio: Trying to keep the first … tokens when context the overflows (Input is longer than the configured context length)
- Model forgets the beginning or ignores long texts (context length too small)
- Ollama: llama runner process has terminated: exit status 0xc0000409 (model process crashed)
- Ollama: model requires more system memory
- Open WebUI: Ollama: 500, message='Internal Server Error' (Ollama reached, but model cannot respond)
- Very slow responses because the model does not fit in VRAM
- vLLM: The model's max seq len is larger than the maximum number of tokens that can be stored in KV cache / No available memory for the cache blocks (context does not fit in the VRAM)