Ollama keep_alive: How to Control Model Load and Unload

Ollama unloads models from memory after 5 minutes of inactivity by default. The next request after that timeout triggers a cold start — the model has to reload from disk before inference can begin, adding 5–30 seconds depending on model size and storage speed. The keep_alive setting is one of the most impactful Ollama configuration changes for anyone using models repeatedly throughout the day.

Why Ollama Unloads Models

The default 5-minute unload timeout exists for good reason: language models are large and occupy significant RAM or VRAM. On a machine dedicated to one model queried frequently — a coding assistant running in VS Code, a local assistant you use all day — the default is unnecessarily conservative and repeated cold starts add meaningful friction. On a shared machine running multiple models, automatic unloading prevents resource exhaustion.

How keep_alive Works

The keep_alive parameter controls how long a model stays loaded after its last request. It accepts three formats: a duration string like “5m”, “1h”, or “24h”; the integer -1 to keep the model loaded indefinitely; and the integer 0 to unload immediately after each response. The default is “5m”. Control it at two levels: per-request (overrides the server default for that call) and server-wide via the OLLAMA_KEEP_ALIVE environment variable.

Figure 1 — keep_alive values and when to use each

Value Behaviour Use when -1Never unloadDedicated machine, one model used constantly 24h / 1hUnload after durationDaily driver, don’t need overnight 0Unload immediatelyBatch jobs, free VRAM after each run

Setting keep_alive Per Request

Add keep_alive to any API request body alongside your model and messages. Set it to -1 to pin the model for the session, “30m” to extend beyond the default, or 0 to free memory immediately after the response. You can also preload a model without a prompt by posting an empty generate request with keep_alive: -1 — this warms the model so the first real request gets instant response, useful for applications that want to ensure the model is ready before showing a UI.

Setting OLLAMA_KEEP_ALIVE Server-Wide

Set the OLLAMA_KEEP_ALIVE environment variable before starting Ollama to change the default for all models. On macOS and Linux, export it in the shell or set it in a systemd service override file and restart the service. On Windows, set it as a system environment variable via System Properties. Common values: “24h” for a daily driver machine; “-1” to keep models loaded until Ollama restarts; “0” for maximum memory efficiency on shared machines.

keep_alive in Docker

Pass OLLAMA_KEEP_ALIVE as an -e flag in docker run or as an environment entry in docker-compose.yml. For setups running both Ollama and Open WebUI via Compose, setting OLLAMA_KEEP_ALIVE on the Ollama service ensures models stay warm between Open WebUI sessions without the 5-minute cold start.

Checking Load Status with ollama ps

The ollama ps command shows all loaded models with their memory usage, processor allocation, and scheduled unload time. The UNTIL column shows a timestamp for duration values and “Forever” for -1. If a model you expected to be loaded is missing from ps, it was either unloaded by timeout or evicted under memory pressure. The ps output also shows whether each model uses GPU or CPU inference.

keep_alive and Multiple Models

With keep_alive set to -1, all loaded models stay in memory simultaneously. Ollama still evicts the least recently used model when a new one needs to load and memory is insufficient — keep_alive is a maximum, not a hard guarantee. Use OLLAMA_MAX_LOADED_MODELS to set an explicit limit on how many models stay in memory at once, preventing a large model from crowding out smaller ones you also need available.

Frequently Asked Questions

Why is my model slow to respond after a break?
The model unloaded after the keep_alive timeout and is reloading from disk. Set OLLAMA_KEEP_ALIVE to “24h” or “-1” to eliminate cold starts on your primary model.
Does keep_alive affect output quality?
No. It only controls memory residency. Inference produces identical results regardless of the keep_alive value once the model is loaded.
Can I unload a specific model without restarting Ollama?
Yes. Send a request to that model with keep_alive set to 0 — this forces an immediate unload without affecting other loaded models.
What if I run out of memory with keep_alive set to -1?
Ollama unloads models under memory pressure even with -1 set. If you need multiple models simultaneously, ensure hardware has sufficient RAM or VRAM for all of them.

Last updated: October 2026

keep_alive with the Ollama Python Library

When using the official Ollama Python library, pass keep_alive as a parameter to the chat or generate calls. Set it to -1 to keep the model loaded for the duration of your script, which is especially useful for batch processing jobs where you want to avoid repeated cold starts between calls. Set it to 0 in the final call of a batch job to immediately free the model from memory when you’re done. The Python library accepts the same values as the REST API: -1, 0, or a duration string. For long-running Python applications that query Ollama periodically, setting a keep_alive of “2h” or “-1” on each request ensures the model stays warm without requiring a separate preload step.

Cold Start Benchmarks: Why This Matters

The practical impact of cold starts varies significantly by model size and storage type. On an NVMe SSD, a 7B model in Q4 (approximately 4–5GB) takes around 3–5 seconds to load from disk into RAM or VRAM. A 14B model takes 8–12 seconds. A 70B model can take 30–60 seconds. On a mechanical hard drive, multiply these times by 3–5x. For an interactive assistant you use dozens of times per day, even a 5-second cold start adds up to several minutes of waiting over a working day. Setting OLLAMA_KEEP_ALIVE to “-1” or “24h” eliminates this entirely on your primary model at the cost of keeping 4–42GB of memory occupied. On a machine with 16–32GB RAM used primarily for local AI work, that tradeoff is almost always worth making.

keep_alive in Open WebUI and Other Frontends

Graphical frontends like Open WebUI, LM Studio, and LibreChat typically send requests to Ollama without a keep_alive parameter, meaning they rely on the server-wide OLLAMA_KEEP_ALIVE default. If you notice that your model is frequently unloading between Open WebUI sessions, the fix is to set OLLAMA_KEEP_ALIVE at the server level rather than trying to configure it per-request through the UI. Set it to “-1” or “24h” on the Ollama server, restart, and the model will stay loaded between sessions. Some frontends, including Open WebUI, allow you to configure the keep_alive value in their model settings panel, which overrides the server default for requests sent from that frontend — check the model settings or administration panel for a keep_alive or model timeout field if you want per-model control without changing the server-wide setting.

Troubleshooting keep_alive

If your keep_alive settings do not seem to be taking effect, check these common issues. Environment variable not picked up: the variable must be set before Ollama starts, not after. Verify with ollama ps immediately after a request — the UNTIL column shows the effective keep_alive value. systemd service not reloaded: after editing the service override file, always run sudo systemctl daemon-reload before restarting. Conflicting per-request values: if the application making requests explicitly sets keep_alive in its API calls, that overrides the server-wide OLLAMA_KEEP_ALIVE value. Check the application’s source or configuration for a keep_alive setting that may be forcing the 5-minute default.

The keep_alive parameter is one of the smallest configuration changes with one of the largest day-to-day quality-of-life impacts for regular Ollama users. Setting it once at the server level via OLLAMA_KEEP_ALIVE eliminates cold starts entirely on your primary model, and the memory cost is simply keeping a file you were going to use anyway resident in RAM. For anyone who has noticed that first response of the day taking noticeably longer, or who has felt friction switching between tasks and coming back to a chat that requires a reload wait, adjusting keep_alive is the single most direct fix.

keep_alive and Context Caching

A less-discussed benefit of keeping models loaded via keep_alive is that Ollama preserves the KV cache between requests as long as the model stays in memory. The KV cache stores computed attention states for any prompt prefix shared between consecutive requests. If you send a long system prompt with every request, Ollama can reuse the computed states for that prefix rather than recomputing them from scratch each time, provided the model was not unloaded between requests. This means keep_alive set to -1 or a long duration not only eliminates cold start latency but also enables prompt caching, which reduces the time-to-first-token for requests that share a common prefix. For applications with a long, fixed system prompt, this cache reuse can cut prefill time by 50-80% on subsequent requests within the same session.

For teams building production applications on top of Ollama, keep_alive should be part of the deployment configuration from day one rather than an afterthought. Document the chosen value alongside the model selection and hardware specifications so that anyone debugging latency issues or memory usage has the full picture of how the server is configured.

Leave a Comment