Add Environment Variable to Override API Parameter keep_alive Value
This is a feature request to add a new environment variable to Ollama, "OLLAMA_KEEP_ALIVE_OVERRIDE".
This proposed environment variable would allow the value of the existing environment variable "OLLAMA_KEEP_ALIVE" to override the API parameter "keep_alive" value, falling back to the default value from envconfig.KeepAlive() when not set.
Explain the problem you are trying to solve, not what you are trying to do
I want a way to prevent individual users from changing the keep alive duration with their API calls. When I set the environment variable OLLAMA_KEEP_ALIVE, I.E.: to the value -1 to keep models loaded 'forever', I want this setting to be honored over user API requests. The absence of such a feature allows models to be unloaded inadvertently when they are expected to be always available.
I am trying to solve this problem within the context of a multi-user enviornment where multiple users have API access, both directly via API requests and via UI layers calling the API on their behalf. Some users provide the keep_alive API parameter in their Ollama API requests. Some UI tools supply the keep_alive API parameter by default using the default value provided in Ollama's API docs.
I would prefer to have control over this functionality directly in Ollama as opposed to educating users and encouraging changes for separate UI providers.
Explain why the change is important
Providing administrators with the ability to enforce a consistent keep alive policy across all API usage is essential for maintaining high availability of specific models in shared environments. This is particularly important in production contexts where predictability and stability are priorities.
Explain how the change will be used
Users would be allowed to supply a new environment variable "OLLAMA_KEEP_ALIVE_OVERRIDE". This would be defaulted to false to prevent breaking changes to existing deployments.
When the proposed OLLAMA_KEEP_ALIVE_OVERRIDE is set to true:
- If OLLAMA_KEEP_ALIVE is set, then the value for OLLAMA_KEEP_ALIVE overrides any keep_alive value included in an API request.
- Else if OLLAMA_KEEP_ALIVE is not set, then the default value from envconfig.KeepAlive() would override any keep_alive value provided.
Explain how the change will be tested
A test would be added to envconfig/config_test.go similar to envconfig.TestKeepAlive().
A manual test procedure would look something like this:
- Set OLLAMA_KEEP_ALIVE to -1:
export OLLAMA_KEEP_ALIVE=-1
- Set OLLAMA_KEEP_ALIVE_OVERRIDE to true:
export OLLAMA_KEEP_ALIVE_OVERRIDE=true
- Run Ollama:
- Load a small model, I.E. gemma3:1b:
- Verify the model is loaded 'Forever':
- Run the following request with
keep_alive parameter:
curl http://localhost:11434/api/generate -d '{
"model": "gemma3:1b",
"prompt": "Why is the sky blue?",
"keep_alive": 0
}'
- Verify the model is still loaded 'Forever':
Add Environment Variable to Override API Parameter keep_alive Value
This is a feature request to add a new environment variable to Ollama, "OLLAMA_KEEP_ALIVE_OVERRIDE".
This proposed environment variable would allow the value of the existing environment variable "OLLAMA_KEEP_ALIVE" to override the API parameter "keep_alive" value, falling back to the default value from envconfig.KeepAlive() when not set.
Explain the problem you are trying to solve, not what you are trying to do
I want a way to prevent individual users from changing the keep alive duration with their API calls. When I set the environment variable OLLAMA_KEEP_ALIVE, I.E.: to the value -1 to keep models loaded 'forever', I want this setting to be honored over user API requests. The absence of such a feature allows models to be unloaded inadvertently when they are expected to be always available.
I am trying to solve this problem within the context of a multi-user enviornment where multiple users have API access, both directly via API requests and via UI layers calling the API on their behalf. Some users provide the keep_alive API parameter in their Ollama API requests. Some UI tools supply the keep_alive API parameter by default using the default value provided in Ollama's API docs.
I would prefer to have control over this functionality directly in Ollama as opposed to educating users and encouraging changes for separate UI providers.
Explain why the change is important
Providing administrators with the ability to enforce a consistent keep alive policy across all API usage is essential for maintaining high availability of specific models in shared environments. This is particularly important in production contexts where predictability and stability are priorities.
Explain how the change will be used
Users would be allowed to supply a new environment variable "OLLAMA_KEEP_ALIVE_OVERRIDE". This would be defaulted to
falseto prevent breaking changes to existing deployments.When the proposed OLLAMA_KEEP_ALIVE_OVERRIDE is set to
true:Explain how the change will be tested
A test would be added to envconfig/config_test.go similar to envconfig.TestKeepAlive().
A manual test procedure would look something like this:
export OLLAMA_KEEP_ALIVE=-1export OLLAMA_KEEP_ALIVE_OVERRIDE=truekeep_aliveparameter: