You ask your local model a question, wait 20 seconds for the first word, and then the next answer is instant. Come back after a coffee and the slow start is back. That pause is Ollama loading the model into memory again, and the setting that controls it is called keep_alive.
By default Ollama unloads a model 5 minutes after the last request. That is great for freeing VRAM, and annoying when you want instant replies all day.
This guide shows how to keep a model loaded forever, unload it on demand, preload it at startup, and set it per request, per run or for the whole server. It also covers how many models Ollama keeps in memory at once and how to fix a model that keeps unloading anyway.
- Default: a model stays in memory for
5mafter its last request. - Keep it loaded forever: set
OLLAMA_KEEP_ALIVE=-1and restart Ollama, or send"keep_alive": -1in an API call. - Unload now:
ollama stop <model>, or an API call with"keep_alive": 0. - One CLI session:
ollama run llama3.2 --keepalive 1h(needs a unit, so use-1m, not-1, for forever). - Check it:
ollama psshows each loaded model and theUNTILtime it will unload.
New to Ollama? My Ollama guide covers install, models, commands and fixes in one place.
What keep_alive Does
Before a model can answer, Ollama has to copy its weights from disk into VRAM (or system RAM). For a 4 GB model on a fast NVMe drive that takes a few seconds. For a 20 GB model, or any model on a slow HDD, it can take 30 seconds or more.
After a reply, Ollama keeps the model in memory for a while so the next request is instant. keep_alive is how long that “while” lasts. When the timer runs out with no new requests, the model is unloaded and the memory is free again.
Every new request resets the timer. So with the default 5 minutes, a model you use every couple of minutes never unloads.
Accepted Values
The same value types work everywhere: in the environment variable, the API and Open WebUI.
| Value | Meaning |
|---|---|
5m (default) |
Unload 5 minutes after the last request |
30m, 2h, 24h |
Any Go duration string (s, m, h) |
3600 |
A plain number is read as seconds (here, 1 hour) |
-1 or -1m |
Any negative value: keep the model loaded until you stop it or Ollama restarts |
0 |
Unload right after the reply |
One catch: the ollama run --keepalive flag only accepts duration strings with a unit. --keepalive -1 fails, --keepalive -1m works.
See What Is Loaded: ollama ps
Run this any time:
ollama ps
You get one line per loaded model. The useful columns are:
- NAME: the model tag.
- SIZE: how much memory it is using right now, including its context cache.
- PROCESSOR:
100% GPUis what you want. A split such as30%/70% CPU/GPUmeans it did not fit in VRAM. - CONTEXT: the context length it was loaded with.
- RUNNER: which inference engine is serving it.
- UNTIL: when it will unload, for example
4 minutes from now, orForeverif keep_alive is negative.
If ollama ps shows nothing, no model is loaded and your next prompt will have the cold-start delay.
Set It for All Models: OLLAMA_KEEP_ALIVE
This changes the default for every model and every app that talks to your Ollama server. It is the best choice if you want instant replies all day on your own PC.
Windows
- Quit Ollama from the system tray icon.
- Open the Start menu, search for Edit the system environment variables, then click Environment Variables.
- Under User variables, click New. Name:
OLLAMA_KEEP_ALIVE. Value:-1(or2h, etc.). - Click OK, then start Ollama again from the Start menu.
For a quick test without saving anything, run this in PowerShell instead (it lasts until you close the window):
$env:OLLAMA_KEEP_ALIVE="-1"
ollama serve
macOS
launchctl setenv OLLAMA_KEEP_ALIVE "-1"
Then quit and reopen the Ollama app.
Linux (systemd service)
sudo systemctl edit ollama.service
Add these lines in the editor, save, then reload:
[Service]
Environment="OLLAMA_KEEP_ALIVE=-1"
sudo systemctl daemon-reload
sudo systemctl restart ollama
Docker
docker run -d --gpus=all -e OLLAMA_KEEP_ALIVE=-1 -v ollama:/root/.ollama -p 11434:11434 --name ollama ollama/ollama
More on containers in my Ollama Docker guide.
To confirm it worked, run any prompt, then ollama ps. The UNTIL column should say Forever.
Set It for One Session: –keepalive
If you only want a longer timer while you chat in the terminal:
ollama run llama3.2 --keepalive 1h
Keep it loaded until you stop it:
ollama run llama3.2 --keepalive -1m
This only affects requests from that ollama run session. Other apps still use the server default.
Set It per Request: the API
The keep_alive field works on /api/generate, /api/chat and /api/embed. It overrides OLLAMA_KEEP_ALIVE for that model.
curl http://localhost:11434/api/chat -d '{
"model": "llama3.2",
"messages": [{"role": "user", "content": "Hi"}],
"keep_alive": "30m"
}'
In Python with the official ollama package:
import ollama
reply = ollama.chat(
model="llama3.2",
messages=[{"role": "user", "content": "Hi"}],
keep_alive=-1,
)
print(reply["message"]["content"])
Remember that the newest request wins. If a script sends keep_alive: 0, the model unloads after that reply even if your server default is forever. Full API details are in my Ollama API guide.
Preload a Model at Startup
An empty request loads a model without generating anything. Combine it with keep_alive: -1 and the model sits ready in VRAM before you type your first prompt:
curl http://localhost:11434/api/generate -d '{"model": "llama3.2", "keep_alive": -1}'
From the CLI, an empty prompt does the same job:
ollama run llama3.2 ""
To do this every time Windows starts, save the curl line in a .bat file and drop a shortcut to it in shell:startup. Add timeout /t 15 at the top so the Ollama server has time to start first.
On Linux, a simple user crontab line works:
@reboot sleep 20 && curl -s http://localhost:11434/api/generate -d '{"model":"llama3.2","keep_alive":-1}' > /dev/null
Unload a Model and Free VRAM
Need your GPU back for ComfyUI or a game? Unload the model straight away:
ollama stop llama3.2
Or through the API:
curl http://localhost:11434/api/generate -d '{"model": "llama3.2", "keep_alive": 0}'
Run ollama ps afterwards to confirm it is gone. Quitting Ollama from the tray (or systemctl stop ollama) unloads everything at once.
If you switch between Ollama and ComfyUI on one card a lot, a short keep_alive such as 1m is a good middle ground. My CUDA out of memory fix covers the other side of that problem.
How Many Models Stay Loaded
keep_alive decides how long a model stays. Three more server settings decide how many models fit and how busy each one can get:
| Variable | Default | What it does |
|---|---|---|
OLLAMA_MAX_LOADED_MODELS |
3 per GPU (3 for CPU) | Most models kept in memory at once, if they fit |
OLLAMA_NUM_PARALLEL |
1 | Parallel requests per model. Memory for context grows with this number |
OLLAMA_MAX_QUEUE |
512 | Requests waiting before Ollama returns a 503 “overloaded” error |
Even with keep_alive set to forever, Ollama will unload an idle model when a new one needs the memory. On a GPU, a new model must fit entirely in VRAM to load next to the others.
So on a 16 GB card, a 9 GB model plus a 6 GB model can share; two 9 GB models cannot, and the older idle one gets swapped out.
Not sure what fits? Check my VRAM guide. Also remember that a bigger context length makes each loaded model take more memory.
Open WebUI, OpenAI Clients and Other Apps
- Open WebUI: open a model’s settings or your chat’s Advanced Params and set Keep Alive (for example
-1or30m). Leave it on default to use the server value. Setup steps are in my Open WebUI guide. - OpenAI-compatible clients (
/v1/chat/completions, used by many coding tools): the standard OpenAI request has no keep_alive field, so rely onOLLAMA_KEEP_ALIVEon the server. - LangChain:
ChatOllamatakes akeep_aliveargument. See my LangChain + Ollama guide. - Embedding models: RAG apps call the embedding model on every upload and search. A long keep_alive for small embedders saves a lot of reload time.
Which Setting Should You Use?
| Your situation | Suggested keep_alive |
|---|---|
| One main model, used all day, GPU not needed for anything else | -1 via OLLAMA_KEEP_ALIVE plus a startup preload |
| Same GPU shared with ComfyUI or games | 1m to 5m, and ollama stop before heavy jobs |
| Several apps and models through the day | 30m to 2h |
| Batch script that runs once | 0 on the last request |
| Laptop on battery | Default 5m or shorter |
Troubleshooting
The model still unloads after 5 minutes
- The server never saw the variable. On Windows you must quit Ollama from the tray and start it again after adding it. On Linux, check with
systemctl show ollama | grep -i keep. - An app is sending its own value. Open WebUI, scripts or agents can send
keep_alivein each request, which wins over the server setting. - You set it in your shell, but Ollama runs as a service. A variable typed into a terminal does not reach the background service.
Error: time: missing unit in duration “-1”
That comes from ollama run --keepalive -1. Use -1m or -1h. The environment variable and the API accept a plain -1.
Model unloads when I open a second model
It did not fit next to the first one. Use smaller quants, a smaller context, or raise OLLAMA_MAX_LOADED_MODELS only if you really have the VRAM.
Model stays loaded but replies are still slow
Check the PROCESSOR column in ollama ps. If part of the model is on the CPU, it will be slow every time. My Ollama not using GPU fix walks through that.
VRAM stays full after I close the chat app
That is keep_alive doing its job. Run ollama stop <model> or wait for the UNTIL time.
FAQ
What is the default keep_alive in Ollama?
Five minutes (5m) after the last request to that model.
Does keep_alive -1 survive a reboot?
The setting does if you saved it as an environment variable, but the model itself is not loaded until something asks for it. Add a startup preload if you want it ready right away.
Does keeping a model loaded use power?
Weights sitting in VRAM use very little extra power while idle. The real cost is that the memory is not free for other programs.
Can I set keep_alive in a Modelfile?
No. keep_alive is a runtime setting, not a model parameter. Use the environment variable, the run flag or the API. Model parameters are covered in my Modelfile guide.
How do I unload all models at once?
Restart the Ollama server, or run ollama stop for each name listed by ollama ps.
Does LM Studio have the same setting?
LM Studio keeps models loaded until you eject them, and its server has its own auto-unload (TTL) option. See my LM Studio API guide.
Sources
- Ollama FAQ (preloading, keep_alive values, OLLAMA_KEEP_ALIVE, concurrency settings)
- Ollama source: envconfig/config.go (5m default, negative = forever, plain number = seconds)
- Ollama source: cmd/cmd.go (–keepalive flag parsing)




