GrepBoard/Manuals
605 manuals ·

Ollama Tuning: OLLAMA_NUM_PARALLEL, OLLAMA_KEEP_ALIVE, num_ctx

·Tested on Ollama 0.40.11 (Docker · CPU · 2 GB container) · qwen2.5:0.5b · 2026-10·Test scope ↓·6 steps

OLLAMA_NUM_PARALLEL multiplies context memory; OLLAMA_KEEP_ALIVE sets how long a model stays loaded. Check both via the server config log line and ollama ps.

Three settings decide Ollama's memory use and time to first response. num_ctx (context length) and OLLAMA_NUM_PARALLEL (concurrent requests per model) multiply each other's memory cost, and OLLAMA_KEEP_ALIVE decides how long a model stays in memory after a request. Before changing anything, read the current values from the server config line in the startup log; afterwards, confirm the effect with SIZE, CONTEXT and UNTIL in ollama ps.

[1] Check the current values. The server prints every environment variable it read on one startup log line — journalctl -u ollama for the Linux service, docker logs for Docker.

console
$ docker logs ollama 2>&1 | grep 'server config' | grep -o 'OLLAMA_\(CONTEXT_LENGTH\|NUM_PARALLEL\|MAX_LOADED_MODELS\|MAX_QUEUE\|KEEP_ALIVE\|FLASH_ATTENTION\|KV_CACHE_TYPE\):[^ ]*'
output
OLLAMA_CONTEXT_LENGTH:0OLLAMA_FLASH_ATTENTION:falseOLLAMA_KEEP_ALIVE:5m0sOLLAMA_KV_CACHE_TYPE:OLLAMA_MAX_LOADED_MODELS:0OLLAMA_MAX_QUEUE:512OLLAMA_NUM_PARALLEL:1

0 or empty means "not set, use the default behavior". Per the official FAQ, that is:

VariableDefaultMeaning
OLLAMA_CONTEXT_LENGTH4096 tokenscontext when a request has no num_ctx
OLLAMA_NUM_PARALLEL1requests one model processes at the same time
OLLAMA_MAX_LOADED_MODELS3 × GPUs, or 3 on CPUmodels kept loaded at once
OLLAMA_MAX_QUEUE512cap on requests queued while busy
OLLAMA_KEEP_ALIVE5 minutestime before an idle model is unloaded
OLLAMA_KV_CACHE_TYPEf16KV cache precision (q8_0 ≈ 1/2, q4_0 ≈ 1/4 of the memory)

FLASH_ATTENTION:false means "not forced on"; per the FAQ, Ollama turns it on automatically when the backend supports it.

[2] Take a baseline. One model loaded on an untouched server:

console
$ ollama run qwen2.5:0.5b "Say OK." >/dev/null; ollama ps
output
NAME            ID              SIZE      PROCESSOR    CONTEXT    RUNNER      UNTILqwen2.5:0.5b    a8b0c5157701    486 MB    100% CPU     4096       llamacpp    4 minutes from now

SIZE is weights plus runtime memory such as the KV cache, so it is larger than the 398 MB file in ollama list. Compare everything below against it.

[3] See what OLLAMA_NUM_PARALLEL grows. The server started with 4:

output
NAME            ID              SIZE      PROCESSOR    CONTEXT    RUNNER      UNTILqwen2.5:0.5b    a8b0c5157701    682 MB    100% CPU     4096       llamacpp    4 minutes from now
console
$ docker logs ollama 2>&1 | grep -o 'starting llama-server.* -c [0-9]* -np [0-9]*'
output
starting llama-server" cmd="/usr/lib/ollama/llama-server --model … --port 43323 --host 127.0.0.1 --no-webui --offline -c 16384 -np 4

CONTEXT still shows the per-request 4096, but the runner was started with -c 16384 -np 4 — 4096 × 4. That is the FAQ's "required RAM will scale by NUM_PARALLEL × CONTEXT_LENGTH". Raise it only when several clients really hit the same model at once; on a single-user machine it just costs memory. On a GPU machine, also check that PROCESSOR in ollama ps did not drop from 100% GPU to a CPU/GPU split (check whether Ollama runs on CPU).

[4] Look at context length and KV cache quantization together. A server with only OLLAMA_CONTEXT_LENGTH=8192, then the same plus OLLAMA_KV_CACHE_TYPE=q8_0:

output
qwen2.5:0.5b    a8b0c5157701    588 MB    100% CPU     8192       llamacpp    4 minutes from now
output
qwen2.5:0.5b    a8b0c5157701    540 MB    100% CPU     8192       llamacpp    4 minutes from now

The q8_0 run logged enabling flash_attn since it is required for quantized V cache: Flash Attention was turned on for the quantized cache. The gap looks small on a tiny model, but only the KV cache share shrinks, so it grows with longer contexts and bigger models. When the model itself does not fit, see model requires more system memory. KV_CACHE_TYPE is server-wide; every model uses the same precision.

[5] Trade first-response latency for memory with KEEP_ALIVE. An unloaded model has to load again, which delays the first request. With OLLAMA_KEEP_ALIVE=-1, UNTIL changes:

output
NAME            ID              SIZE      PROCESSOR    CONTEXT    RUNNER      UNTILqwen2.5:0.5b    a8b0c5157701    486 MB    100% CPU     4096       llamacpp    Forever

A negative value keeps it loaded, 0 unloads right after the response, and durations such as 10m or 1h work too. The API's per-request keep_alive overrides the server value, so you can unload the current model without restarting the server:

console
$ curl -s http://localhost:11434/api/generate -d '{"model":"qwen2.5:0.5b","keep_alive":0}'
output
{"model":"qwen2.5:0.5b","created_at":"2026-10-27T11:13:53.574398721Z","response":"","done":true,"done_reason":"unload"}

ollama stop qwen2.5:0.5b does the same. On a memory-tight box keep the default 5 minutes or less; on a dedicated box serving one model, use -1.

[6] Apply it to the server and roll back. The variables must reach the server process, not the client. For the Linux service, follow the FAQ: sudo systemctl edit ollama.service and add them under [Service].

/etc/systemd/system/ollama.service.d/override.conf
[Service]Environment="OLLAMA_NUM_PARALLEL=2"Environment="OLLAMA_KEEP_ALIVE=30m"
console
$ sudo systemctl daemon-reload$ sudo systemctl restart ollama$ journalctl -u ollama --no-pager | grep 'server config' | tail -1

The new values should appear on that last line. With Docker, pass -e OLLAMA_NUM_PARALLEL=2 and recreate the container. To roll back, delete the lines in systemctl edit and run the same two commands (daemon-reload, restart) again. A restart unloads every model, so the next first request is slower.

#ollama#ollama-num-parallel#ollama-keep-alive#num-ctx#kv-cache#ollama-ps