Ollama’s context length controls how many tokens a model can consider at once. The KV cache is the memory that stores the key-value pairs for those tokens. To configure context length, you can set the OLLAMA_CONTEXT_LENGTH environment variable, pass num_ctx in the API, or use /set parameter num_ctx in the CLI. The documented default is 4096 tokens, but Ollama may automatically choose larger context windows based on available VRAM. Increasing context length increases memory use, so understanding the KV cache tradeoff is essential for stable local inference.
Context Length and KV Cache in Plain Language
Context length is the maximum number of tokens the model has access to in memory [2]. A token can be a word, part of a word, or a punctuation mark. The KV cache (key-value cache) stores the internal representations for each token in that context window. As the context grows, the KV cache grows with it. Ollama manages the KV cache automatically; the documented configuration surface does not include a separate KV cache size parameter. Instead, you control it indirectly by setting the context length and choosing a model and quantization. The Ollama documentation confirms that setting a larger context length increases the amount of memory required to run a model [2].
Default Context Lengths and VRAM-Based Scaling
The Ollama FAQ states that the default context window size is 4096 tokens [1]. However, the context length documentation describes VRAM-based defaults: less than 24 GiB VRAM gets 4k context, 24–48 GiB gets 32k, and 48 GiB or more gets 256k [2]. These two statements may reflect different versions or configurations. In practice, check the allocated context with ollama ps after loading a model. If you need a specific context length, set it explicitly rather than relying on defaults.
How to Set Context Length
There are several ways to configure context length in Ollama. Choose the method that matches your workflow.
1. Server-wide environment variable
Set OLLAMA_CONTEXT_LENGTH before starting the Ollama server. For example, to set an 8K default:
OLLAMA_CONTEXT_LENGTH=8192 ollama serveFor agent, web search, or coding workflows that need long context, the documentation recommends at least 64000 tokens [2]:
OLLAMA_CONTEXT_LENGTH=64000 ollama serveThis variable sets the default for all models served by that Ollama instance. It does not override per-request settings. On Linux, Ollama is configured through environment variables, which can be set for the service in a drop-in file, then restart ollama.service [3].
2. Per-session CLI setting
Inside an ollama run session, use /set parameter:
/set parameter num_ctx 4096This changes the context length for the current session only [1].
3. API request parameter
When calling the Ollama API, include num_ctx in the options object:
curl http://localhost:11434/api/generate -d '{
"model": "llama3.2",
"prompt": "Why is the sky blue?",
"options": {
"num_ctx": 4096
}
}'This applies the context length to that specific request [1].
4. Ollama app slider
If you use the Ollama desktop app, you can change the slider under settings to your desired context length [2].
How to Verify Allocated Context and GPU Offloading
After loading a model, run:
ollama psThe output includes a CONTEXT column that shows the allocated context length. It also shows the PROCESSOR column, which indicates where the model and its KV cache are loaded [2]. Example output:
NAME ID SIZE PROCESSOR CONTEXT UNTIL
gemma4:latest c6eb396dbd59 9.6 GB 100% GPU 131072 2 minutes from nowThe PROCESSOR column can show 100% GPU (fully on GPU), 100% CPU (fully in system memory), or a split such as 48%/52% CPU/GPU (partially offloaded) [1]. For best performance, the documentation advises using the maximum context length for a model and avoiding CPU offloading [2]. If you see a CPU/GPU split, the context length or model size may exceed available VRAM.
Memory Tradeoffs: KV Cache and VRAM
The KV cache is a major consumer of VRAM as context length grows. Because Ollama does not expose a direct KV cache size setting in the documented sources, the practical levers are:
- Lower the context length (
num_ctxorOLLAMA_CONTEXT_LENGTH). - Use a smaller model or a more aggressive quantization to free VRAM for the cache.
- Add more VRAM if you need long context and fast inference.
For example, a workflow that needs 64k context may fit on a GPU with 24–48 GiB VRAM if the model is small enough, but a larger model may require offloading. The VRAM-based defaults (4k, 32k, 256k) are starting points, not guarantees [2]. Always verify with ollama ps.
Troubleshooting Common Context and KV Cache Problems
Context length setting has no effect
Check that you set the variable before starting the server. For ollama run, /set parameter only affects the current session. For API calls, ensure num_ctx is inside the options object, not at the top level. Restart the server after changing environment variables.
Out-of-memory errors or model fails to load
Reduce num_ctx or switch to a smaller model. If the model loads but runs slowly, check ollama ps for a CPU/GPU split. A split means the KV cache or model weights are partially in system memory, which increases latency. Lower the context length until you see 100% GPU.
Model ignores long context
Some models have a maximum context length defined in their architecture. Ollama may cap the context to the model’s limit. Consult the model card or try a model known to support long context. The documentation notes that tasks like web search, agents, and coding tools should use at least 64000 tokens [2], but only if the model supports it.
Practical Example: Configuring 64k Context for a Local Agent
This is an illustrative scenario, not a tested benchmark. Suppose you are building a local agent that summarizes long documents. You have a GPU with 48 GiB VRAM and a model that supports 128k context. You start Ollama with:
OLLAMA_CONTEXT_LENGTH=64000 ollama serveThen you load the model and run ollama ps. You expect to see CONTEXT 64000 and PROCESSOR 100% GPU. If you see a CPU/GPU split, reduce to 32k or use a smaller quantized model. This example shows the verification loop: set, load, check, adjust.
Best Practices and Next Steps
Start with the default context length and increase only when your task requires it. Use OLLAMA_CONTEXT_LENGTH for a consistent server-wide default, and num_ctx for per-request control. Monitor ollama ps to confirm that the context is allocated and the model is fully on GPU. Remember that KV cache memory scales with context length, so plan your VRAM budget accordingly. If you encounter a specific error or want to share your hardware and context settings, join the Ai-Forum.org community discussion.
FAQ
What is the default context length in Ollama?
The FAQ states 4096 tokens [1], but the context length documentation describes VRAM-based defaults: 4k for less than 24 GiB VRAM, 32k for 24-48 GiB, and 256k for 48 GiB or more [2]. Check ollama ps for the actual allocated context.
How do I change context length for a single request?
Use num_ctx in the API options object, or /set parameter num_ctx in an ollama run session [1].
Does Ollama have a separate KV cache size setting?
The documented sources do not expose a direct KV cache size parameter. KV cache size is derived from context length and model architecture. To reduce it, lower num_ctx or use a smaller model or quantization [2].
How can I tell if my model is using the GPU for the KV cache?
Run ollama ps. The PROCESSOR column shows 100% GPU, 100% CPU, or a split [1]. For best performance, aim for 100% GPU [2].