AI Atlas
Beginner · ~1 min read #keep_alive #ollama #inference

keep_alive

How long Ollama keeps a model in memory

The Ollama setting that controls how long a model stays loaded in RAM/VRAM after the last request. Default 5 minutes; after that the model is unloaded and the next request starts cold.

TWO REQUESTS, 8 MINUTES APART0 min2 min4 min6 min8 min10 minrequest 1request 2keep_alive: 0unloadedcold start5m (default)unloadedcold startkeep_alive: -1instant replyin memoryloadingThe timer starts after the last request; the UNTIL column in ollama ps shows what is left
Definition

Reading a model from disk into RAM/VRAM takes seconds. So Ollama keeps the model in memory for a while after each request; requests arriving in that window are answered right away. When the timer runs out the model is unloaded and the next request waits for a full load (cold start).

Two places to set it:

  • Per request: keep_alive in the /api/generate and /api/chat body. This overrides the server setting.
  • Server-wide: the OLLAMA_KEEP_ALIVE environment variable.

Accepted values (as of October 2026, default 5m):

ValueMeaning
"10m", "24h"Duration (Go duration format)
3600Number = seconds
-1 or "-1m"Negative = stay loaded indefinitely
0Unload right after the response

On the command line: ollama run model --keepalive 1h, and ollama stop model to unload immediately. The UNTIL column of ollama ps shows how long each loaded model will stay.

Analogy

Keeping the stove on in a restaurant. If orders keep coming, turning it off and relighting it every time (cold start) wastes time. But if no one is coming all night, a lit stove just burns gas (occupies memory). keep_alive answers "how many minutes after the last order should the stove stay on?"

Real-world example

A coding assistant sends a request to Ollama every 10-15 minutes. With the 5-minute default, the model has been unloaded every time, and the first response is noticeably delayed by the load.

Fix 1, server-wide (macOS app): `` launchctl setenv OLLAMA_KEEP_ALIVE "1h" ` Then restart Ollama. On Linux, add Environment="OLLAMA_KEEP_ALIVE=1h"` to the systemd service.

Fix 2, per request: `` curl http://localhost:11434/api/generate -d '{ "model": "llama3.2", "keep_alive": "1h", "prompt": "Hello" }' ``

The reverse: after a one-off batch job, send "keep_alive": 0 to get the memory back immediately.

Code examples
Ollama · preload a model, keep it forever, then unload bash
# Empty prompt = load only (preload). -1 = stay loaded indefinitely.
curl http://localhost:11434/api/generate -d '{"model": "llama3.2", "keep_alive": -1}'

# What's loaded, and until when? (UNTIL column)
ollama ps

# Free the memory now
ollama stop llama3.2
# or via the API:
curl http://localhost:11434/api/generate -d '{"model": "llama3.2", "keep_alive": 0}'
Ollama · changing the server-wide default bash
# One-off (server started from a terminal)
OLLAMA_KEEP_ALIVE=30m ollama serve

# A bare number also works in the env var: read as seconds
OLLAMA_KEEP_ALIVE=1800 ollama serve
When to use
  • Infrequent but regular requests (IDE assistant, Slack bot) — extend it so each request isn't a cold start
  • A server that only ever runs one model — keep it with -1
  • Benchmarks — preload first so load time doesn't leak into the numbers
  • After a batch job — 0 frees the memory for other work right away
When not to use
  • Tight memory with several models used in rotation — -1 hogs the space the others need
  • On a laptop where battery and RAM matter — the 5-minute default is a sensible balance
  • llama.cpp server — keep_alive is Ollama-specific; the llama.cpp equivalent is --sleep-idle-seconds (default -1 = off, the model stays loaded while the process runs)
Common pitfalls

The API value beats the env var

You set OLLAMA_KEEP_ALIVE=-1, but a client sends keep_alive: "5m" with every request, so the model still unloads after 5 minutes. The per-request value always wins; check what your client sends.

Unitless strings

In JSON, "keep_alive": 300 (a number) means 300 seconds; "keep_alive": "300" (a string with no unit) can't be parsed as a Go duration and returns an error. Use a number or "300s".

Forever = locked memory

If there's no room for a new model, Ollama queues requests and unloads idle models to make space. OLLAMA_MAX_LOADED_MODELS caps how many stay loaded at once (default: 3 × number of GPUs, or 3 on CPU). Pinning a big model with -1 can leave the others less memory or push them onto the CPU.