Ollama context length limits how much text a model can keep in view. A systemd drop-in sets Ollama's persistent server context default, which must be checked on a newly loaded model.
A larger context needs more memory, even when the model weights stay the same size. The model's supported context is an upper limit, rather than a promise that the computer has enough memory to use it all.
Ollama must already be running as a systemd service, with a local model installed and sudo access available for service configuration. A model or request can override the server default, so the allocated context of a newly loaded model matters more than the saved setting alone.
Steps to set Ollama context length with systemd:
- Inspect the Ollama service unit and its existing overrides.
$ sudo systemctl cat ollama
The unit identifies the service running the server, while any drop-ins appear after it with their file paths. An existing OLLAMA_CONTEXT_LENGTH assignment may already control the default.
- Check the supported context of the installed model.
$ ollama show qwen2.5:0.5b Model architecture qwen2 parameters 494.03M context length 32768 ##### snipped #####qwen2.5:0.5b is a small model suitable for a 4096-token setting. A larger setting must fit both the chosen model's supported context and available memory.
- Create the Ollama service drop-in directory.
$ sudo mkdir -p /etc/systemd/system/ollama.service.d
- Write a 4096-token server default to a dedicated context-length drop-in.
This replaces only context-length.conf. If that file already exists, the following backup preserves its previous settings before replacement.
$ sudo cp -a /etc/systemd/system/ollama.service.d/context-length.conf /etc/systemd/system/ollama.service.d/context-length.conf.bak
Larger contexts increase memory use and can cause model loading to fail.
$ sudo tee /etc/systemd/system/ollama.service.d/context-length.conf > /dev/null <<'EOF' [Service] Environment="OLLAMA_CONTEXT_LENGTH=4096" EOF
OLLAMA_CONTEXT_LENGTH sets the server default in tokens. If another drop-in assigns the same variable, the assignment in the file sorted last takes precedence.
- Reload systemd's service configuration.
$ sudo systemctl daemon-reload
- Restart Ollama to apply the context default.
Restarting interrupts active requests and unloads models, so this needs a quiet period on a shared server. If the new setting causes a failure, the previous default can be restored by recovering context-length.conf.bak or, for a newly created file, removing only this drop-in.
$ sudo rm /etc/systemd/system/ollama.service.d/context-length.conf
The reload and restart actions apply that recovery without changing other overrides.
$ sudo systemctl restart ollama
- Confirm the restarted API is responding.
$ curl -fsS --retry 5 --retry-connrefused --retry-delay 1 http://127.0.0.1:11434/api/version {"version":"0.40.12"}
A brief connection-refused message can appear while the service starts. A returned version confirms API availability; the version number varies with the installed release.
- Load the model with a short inference request.
$ ollama run qwen2.5:0.5b 'What is 17 plus 25? Answer in one short sentence.' 17 plus 25 equals 42.
The answer's wording can vary. This request uses the server default without a client context override.
- Confirm the loaded model has a 4096-token context.
$ ollama ps NAME ID SIZE PROCESSOR CONTEXT RUNNER UNTIL qwen2.5:0.5b a8b0c5157701 630 MB 100% CPU 4096 llamacpp 4 minutes from now
CONTEXT must show 4096 for the model that answered the request. A different value can indicate another service override or a model-specific context parameter. Processor placement, memory size, and unload time depend on the computer and model.