skip to content

An app on Ollama stalls for seconds on the first request after a quiet spell — which setting causes it, and how do you keep models loaded?

level: middleimportance: should knowfreq 12%

answer

  1. the model is loading again
  2. idle timer after the last request
  3. five minutes unless changed
  4. request field beats server variable
  5. negative keeps, zero unloads

basics

~20 s

Ollama unloads an idle model after keep_alive, 5 minutes by default, so the next request reloads it. Send keep_alive with a longer duration or -1 per request, or set OLLAMA_KEEP_ALIVE on the server; 0 or ollama stop unloads.

solid answer

~40 s

The stall is a reload: by default Ollama keeps a model in memory for 5 minutes after its last request, then unloads it. `keep_alive` controls that — a duration such as `"30m"`, a number of seconds, any negative value to keep it loaded with no timer, or `0` to unload right after the response. Set it per request on `/api/chat` or `/api/generate`, or server-wide with `OLLAMA_KEEP_ALIVE`; the request field overrides the server. Preload with an empty request or `ollama run your-model ""`, and free memory with `ollama stop`. A negative value removes the timer but not eviction: `OLLAMA_MAX_LOADED_MODELS` (3 per GPU by default) and available memory can still unload an idle model.

go deeper

for a junior

Know that Ollama unloads an idle model after 5 minutes and that keep_alive or ollama stop controls loading and unloading.

for a middle

Explain the value forms, that the request field overrides OLLAMA_KEEP_ALIVE, how to preload, and how the UNTIL column confirms it.

for a senior

Trace a cold start to its real cause — the timer, a client resending keep_alive, the loaded-model cap, memory pressure, or a num_ctx mismatch forcing a reload.

for a principal

Set keep-alive policy per model on shared machines: interactive models warm, batch models timed out, so idle memory never pushes the busy model onto the CPU.

## The symptom: a cold start after a quiet spell An application calls a local Ollama model. During the day answers start quickly, but the first request after lunch — or after any gap of more than a few minutes — takes several seconds longer before the first token appears. The model is not slow; it is being **loaded again**. Loading means reading the weights from disk into RAM and VRAM and setting up the context, and for a large model on a slow disk that takes seconds to tens of seconds. ## keep_alive: how long an idle model stays loaded By default Ollama keeps a model in memory for **5 minutes** after its last request, then unloads it to give the memory back. The setting is `keep_alive`, and it can be given in two places: - **Per request** — a `keep_alive` field on `/api/chat` or `/api/generate`. It **overrides** the server setting for that model. - **Per server** — the `OLLAMA_KEEP_ALIVE` environment variable, which changes the default for every model. Both accept the same values: | Value | Effect | |---|---| | a duration string such as `"10m"` or `"24h"` | stay loaded that long after the last request | | a number such as `3600` | the same, in seconds | | any negative number such as `-1` or `"-1m"` | stay loaded with no timer | | `0` | unload right after the response | The UNTIL column of `ollama ps` shows the result: "4 minutes from now" for a timer, "Forever" for a negative value. ## Keeping a model warm on purpose 1. **Preload it.** A request with a model name and no prompt or messages loads the model without generating — for example `curl http://localhost:11434/api/generate -d '{"model": "your-model", "keep_alive": -1}'`. From the CLI, `ollama run your-model ""` does the same, and `ollama run your-model --keepalive 1h` sets the duration for an interactive session. 2. **Raise the server default** with `OLLAMA_KEEP_ALIVE=1h` (or `-1`) when every client should benefit, rather than editing every client. 3. **Unload deliberately** with `ollama stop your-model`, or a request with `keep_alive: 0`, when a batch job is done and the VRAM is wanted elsewhere. Remember that the request field wins: a client library that sends its own `keep_alive` on every call will silently undo a server-wide setting. ## The limits around it: how many models stay loaded `keep_alive` decides when an idle model times out; other rules decide whether it can stay at all: - **`OLLAMA_MAX_LOADED_MODELS`** caps how many models are loaded at once. The default is 3 times the number of GPUs, or 3 for CPU inference. When the cap is reached, the scheduler unloads one — preferring an idle model — to make room. - **Memory.** If a new model does not fit beside the ones already loaded, new requests queue while idle models are unloaded to make room. On GPU inference, concurrent loads require the new model to fit completely in VRAM. - **Changed load options.** A request with a different `num_ctx` (or other load-time option) than the loaded copy causes a reload, which looks exactly like a cold start. A negative `keep_alive` therefore means "no idle timer", not "pinned against eviction": an idle model can still be unloaded when the scheduler needs room for another. ## Diagnosing a cold start 1. **Send a request, then run `ollama ps` at once.** The UNTIL column shows the timer this request actually set — a short one means some client is sending its own `keep_alive`. 2. **Watch whether the row disappears early.** If it vanishes before UNTIL, another model's load usually evicted it (or a client sent `keep_alive: 0`): count the rows against `OLLAMA_MAX_LOADED_MODELS` and check free VRAM. 3. **Compare the CONTEXT column between clients.** A model that keeps reloading while it is busy is usually being asked for different `num_ctx` values. 4. **Read the server log** for unload and reload lines around the slow request. ## Choosing a value On a single-purpose box serving one model, `-1` or a long duration removes cold starts at no cost. On a laptop that also runs other GPU work, the 5-minute default is a reasonable compromise. On a shared server with several models, a long `keep_alive` keeps VRAM occupied by models nobody is using, which forces evictions or CPU/GPU splits for the next model — keep long durations for the model that serves interactive traffic, and let batch-only models time out.

  • You set OLLAMA_KEEP_ALIVE=-1 on the server, yet the model still unloads after five minutes. What is the likely cause?
    A client is sending its own `keep_alive` on each request, and the request field overrides `OLLAMA_KEEP_ALIVE`. Many client libraries send a default value. Check the request bodies, or the UNTIL column of `ollama ps` right after a call from that client, and change the client's setting.
  • Does a model loaded with keep_alive -1 ever leave memory without `ollama stop`?
    Yes. The negative value only removes the idle timer. When `OLLAMA_MAX_LOADED_MODELS` is reached, or a new model needs memory, the scheduler unloads a loaded model to make room and prefers an idle one, so a 'kept' model that is idle can still be evicted. A restart of the server unloads everything too.

keep_alive is like leaving a car's engine idling between errands: the next trip starts instantly, but the engine burns fuel — here, memory — the whole time, so you idle only the car you are about to use.

  • Ollama keeps a model loaded until the server restarts
  • OLLAMA_KEEP_ALIVE always wins over the request's keep_alive
  • keep_alive 0 keeps the model loaded forever
  • ollama rm unloads a model from memory
  • A keep_alive of -1 pins a model against any eviction