Ollama keep alive explained: the 5m default, OLLAMA_KEEP_ALIVE vs per-request keep_alive, -1 and 0, preloading, max loaded models, and why it 'doesn't work'.
Ollama keeps a model in memory for 5 minutes after its last request, then unloads it. Set OLLAMA_KEEP_ALIVE on the server (for example 24h, or -1 for forever) to change the default, or send keep_alive on each request, which always wins. keep_alive 0 or ollama stop unloads immediately. Most 'not working' reports come from a client sending its own keep_alive, an env var the service never sees, or a value Ollama cannot parse.
- 5 minutes
- keep_alive: -1 / OLLAMA_KEEP_ALIVE=-1
- ollama stop <model> or keep_alive: 0
- 3 per GPU (3 on CPU)
Key takeaways
- Default is 5m. A request's keep_alive field overrides OLLAMA_KEEP_ALIVE, and every new request resets the timer with its own value.
- In the API, use a number (-1, 0, 3600) or a string with a unit ("24h", "-1m"). The string "-1" has no unit and makes the request fail.
- In OLLAMA_KEEP_ALIVE, a bare integer means seconds and -1 means forever. A value Go cannot parse, such as 1d, silently falls back to 5m.
- keep_alive -1 is not a lock: if a new model needs the memory or a slot, Ollama still evicts an idle model. Changing num_ctx between calls also forces a reload.
- Check the result with ollama ps: the UNTIL column shows the unload time, or Forever.
Ollama keep alive is the timer that decides how long a model stays in RAM or VRAM after its last request. The default is 5 minutes (Ollama FAQ). To keep a model loaded for good, set OLLAMA_KEEP_ALIVE=-1 on the server or send "keep_alive": -1 with your requests. To free memory now, run ollama stop <model>. The rest of this page covers the rules that make those settings appear to “not work”, checked against the Ollama v0.40.13 source.
How long does Ollama keep a model loaded by default?
Five minutes after the last request finishes, unless something tells it otherwise. The FAQ says so, and the code agrees: envconfig.KeepAlive() starts from 5 * time.Minute and the JSON parser for keep_alive uses the same fallback (config.go, types.go).
The timer restarts on every request, and each request brings its own value. So keep-alive is not a property of the model. It is the setting carried by the most recent request that touched it, with the server env var used only when that request carried none (sched.go).
You can see the current state with:
ollama psThe UNTIL column shows something like 4 minutes from now, or Forever for a negative keep-alive.
How do you change keep_alive per request and server-wide?
Per request, add keep_alive to the body of /api/generate, /api/chat or /api/embed. Server-wide, set the OLLAMA_KEEP_ALIVE environment variable on the process that runs ollama serve. The request value overrides the env var whenever it is present (Ollama FAQ).
The two are parsed by different code, and that difference is where most typos turn into bugs:
| Value | In the API (keep_alive) | In OLLAMA_KEEP_ALIVE |
|---|---|---|
-1 (number) | Keep loaded forever | Keep loaded forever (bare integer = seconds, negative = infinite) |
"-1" (string) | Request fails: Go’s duration parser rejects a number without a unit | n/a (env vars are strings, see row above) |
"-1m", "-24h" | Forever | Forever |
0 | Unload right after the response | Unload right after every response |
3600 (number) | 1 hour | 1 hour |
"3600" (string) | Request fails (missing unit) | 1 hour |
"10m", "24h", "1h30m" | That duration | That duration |
"1d", "forever" | Request fails (unknown unit) | Ignored, silently stays 5m |
The API behaviour comes from Duration.UnmarshalJSON in api/types.go: numbers are seconds, strings go through time.ParseDuration, which needs a unit and knows only ns, us, ms, s, m, h. The env var path in config.go tries time.ParseDuration first, then a plain integer as seconds, and keeps the 5-minute default if both fail, without logging an error. OLLAMA_KEEP_ALIVE=1d is the classic way to get 5 minutes while believing you set a day.
Setting the env var depends on how Ollama runs, per the FAQ:
- Linux (systemd):
systemctl edit ollama.service, addEnvironment="OLLAMA_KEEP_ALIVE=-1"under[Service], thensystemctl daemon-reload && systemctl restart ollama. - macOS app:
launchctl setenv OLLAMA_KEEP_ALIVE -1, then quit and reopen the Ollama app. - Windows: quit Ollama from the tray, add the variable under “Edit environment variables for your account”, start Ollama again.
- Docker: pass
-e OLLAMA_KEEP_ALIVE=-1to the container; details are in our Ollama Docker guide.
From the CLI, ollama run llama3.2 --keepalive 30m sends a keep-alive with that session’s requests. The flag goes through time.ParseDuration, so it needs a unit too.
How do you preload or unload a model on demand?
Send a request with no prompt to load, and a request with keep_alive: 0 to unload. Both are documented in the FAQ and API reference:
# load llama3.2 now and keep it loaded
curl http://localhost:11434/api/generate -d '{"model": "llama3.2", "keep_alive": -1}'
# unload it now
curl http://localhost:11434/api/generate -d '{"model": "llama3.2", "keep_alive": 0}'
# same thing from the CLI
ollama stop llama3.2/api/chat with an empty messages array works the same way. ollama stop is literally a load-or-unload request with keep_alive 0 in the CLI source (cmd.go). Preloading is useful at boot or before a demo, so the first real request does not pay the load time.
If you talk to Ollama through the OpenAI-compatible endpoint, v0.40.13 accepts keep_alive as an Ollama extension on /v1/chat/completions; the struct comment says “without it an OpenAI-API client cannot release a model” (openai.go). With the OpenAI SDK you pass it through extra_body. Older builds dropped it: a user on Ollama 0.9.6 reported that extra_body={"keep_alive": "24h"} was ignored and the model unloaded after 5 minutes (#11458). If you are pinned to an old version, set the env var instead. More on that endpoint in Ollama API.
We build CandorNo GPU? Use a hosted model
Uncensored AI, right in your browser.
Candor runs open models with refusals removed, so it answers the questions other assistants deflect. New accounts get free credits, no card needed.
Try Candor freeHow many models can stay loaded at once?
By default up to 3 per GPU, or 3 on CPU, and only as many as fit in memory. OLLAMA_MAX_LOADED_MODELS sets a fixed cap (FAQ; defaultModelsPerGPU = 3 in sched.go). Windows with Radeon GPUs is documented as defaulting to 1.
Keep-alive is a ceiling on idle time, not a reservation. When a request arrives for a model that is not loaded, the scheduler checks the cap and free memory. If either is short, it picks a model to evict, preferring idle ones, sets its keep-alive to zero and waits for it to unload (sched.go, findRunnerToUnload). That happens even when the victim was loaded with -1. On a GPU, a new model has to fit completely in VRAM to load next to the others; otherwise it waits in the queue until something is evicted (FAQ).
So if you alternate between a 14B coder and an 8B chat model on a 16 GB card, both with -1, you will still see reloads on every switch. Raising OLLAMA_MAX_LOADED_MODELS will not help when the problem is VRAM. Smaller quants, a smaller context, or a quantized KV cache will (see Ollama context length). How many requests one loaded model serves at the same time is a separate setting, covered in OLLAMA_NUM_PARALLEL.
Why is Ollama keep_alive not working?
Usually because something other than your setting is deciding. Work through these in order:
- A client sends its own keep_alive. Since the request wins, a frontend that always sends a value overrides your env var on every call. A feature request asking for a server option to ignore client keep_alive has been open since April 2025, filed because “some frameworks enable keep_alive by default” (#10263). The mirror image also happens: a SillyTavern user set
keepAlive: 300in the client config, yetollama pskept showingForever(r/SillyTavernAI), meaning another client or the server env was still in charge. - The service never saw the env var.
export OLLAMA_KEEP_ALIVE=-1in your shell does nothing for a systemd service, the macOS app or a Docker container. Use the per-OS steps above and restart the server. - The value did not parse.
1din the env var silently means 5m;"-1"as a JSON string fails the request. Use-1,24hor a plain number. - Options changed, so the model reloaded. The scheduler reloads a runner when a request’s runner options differ,
num_ctxincluded. A Windows user withkeep_alive=-1andOLLAMA_KEEP_ALIVE=-1saw gemma2 27B unload about 30 seconds after each message; the logs showed “expire immediately to make room”, and the cause was code alternating between twonum_ctxvalues. Sending one constantnum_ctxfixed it (#7773). - Memory pressure or the model cap evicted it. See the previous section.
ollama psbefore and after the switch will show it. - The same weights under two names. Two Modelfiles built on the same
llama3.2:1bGGUF could not stay loaded together even with-1and plenty of RAM; a collaborator replied that Ollama does not load one GGUF file twice (#10948).
For deeper digging, start the server with OLLAMA_DEBUG=1 and look for scheduler lines such as “adding timer” versus “expiring to unload”.
What do developers report in practice?
The pattern across reports is that keep-alive works as coded, and the surprises come from interactions:
- Client resets. A Hacker News commenter in May 2025 described the next API call resetting both the context size and keep-alive they had set by hand, with the model dropping out of memory every few minutes (HN). That is the request-wins rule seen from the user side.
- Eviction stalls with one slot. On Ollama 0.40.x with
OLLAMA_MAX_LOADED_MODELS=1on an M5 Pro, a user found that if a model was marked for eviction and another request for it arrived carryingkeep_alive, the eviction was cancelled and the waiting request stalled for the full keep-alive window. Two others reproduced it, on Linux CPU-only and on an RTX 3060 under VRAM pressure. Sendingkeep_alive: 0to the resident model unblocked the queue. The issue was still open on 2026-11-02 (#19182). - Thrashing that is not keep-alive. The num_ctx reload (#7773) and the shared-GGUF case (#10948) both looked like a broken keep-alive to the reporter, and both were something else.
The practical setup for a single-user dev box: set OLLAMA_KEEP_ALIVE=-1 or a long duration on the service, keep num_ctx constant in your clients, and check what keep-alive your editor or chat UI sends. On a shared team server, prefer a finite duration such as 30m, since any client sending -1 can pin memory.
FAQ
How do I make Ollama keep a model loaded forever?
Set OLLAMA_KEEP_ALIVE=-1 on the server and restart it, or send "keep_alive": -1 (a number, not the string “-1”) with requests. ollama ps should then show Forever. It can still be evicted if another model needs the memory.
What does OLLAMA_KEEP_ALIVE=0 do?
It unloads every model right after each response, so every request pays the full load time. It is useful on machines where the GPU is shared with other work, not for interactive use.
Does keep_alive override OLLAMA_KEEP_ALIVE?
Yes. A keep_alive value on the request always wins for that model, and Ollama has no server option to stop clients from overriding it as of v0.40.13.
What is the default for OLLAMA_MAX_LOADED_MODELS?
Three times the number of GPUs, or 3 for CPU inference, limited by what actually fits in memory. Windows with Radeon GPUs defaults to 1.
How do I unload an Ollama model to free VRAM?
Run ollama stop <model>, or send {"model": "<model>", "keep_alive": 0} to /api/generate. Both unload as soon as in-flight requests finish.
We build CandorSkip the setup
Want an uncensored model without hosting one?
Candor (candorai.dev) is an uncensored AI chat app that runs in your browser: it answers the questions mainstream assistants dodge, on health, money, law and work, without the lecture. New accounts get free credits, no card needed.
Facts from candorai.dev, checked Nov 10, 2026. Candor never changes how we rank or review models. Disclosure.
Sources (10)
- Ollama docs FAQ: How do I keep a model loaded in memory or make it unload immediately?
- Ollama API docs: Generate a response (keep_alive parameter)
- ollama/ollama v0.40.13 source: envconfig/config.go (OLLAMA_KEEP_ALIVE parsing)
- ollama/ollama v0.40.13 source: api/types.go (keep_alive JSON parsing)
- ollama/ollama v0.40.13 source: server/sched.go (scheduler, eviction, defaults)
- ollama/ollama v0.40.13 source: openai/openai.go (keep_alive on /v1/chat/completions)
- GitHub issue #7773: model unloads after ~30 seconds despite keep_alive=-1 (community report)
- GitHub issue #19182: pending eviction cancelled by a request carrying keep_alive (community report)
- GitHub issue #10263: prevent keep_alive from being overwritten by API (feature request)
- GitHub issue #11458: keep_alive ignored with the OpenAI SDK on Ollama 0.9.6 (community report)
Written by Refusal Rate editors from papers, model cards, official docs and pricing pages. Anything we ran ourselves is labeled as our own test, with the hardware and settings. Our method.