Skip to content
C.W.K.
Stream
Lesson 01 of 05 · published

Performance Tuning

~22 min · ops, performance

Level 0Downloader
0 XP0/41 lessons0/11 achievements
0/120 XP to next level120 XP to go0% complete

The five environment variables that matter

VariableWhat it doesDefault value
OLLAMA_FLASH_ATTENTIONForces Flash Attention on (1) or off (0). Ollama already uses it automatically wherever the backend and devices support it, so this is an override, not a switch you have to findautomatic
OLLAMA_KV_CACHE_TYPEQuantize KV cache (q8_0 halves it; q4_0 quarters it). Needs Flash Attention, which you already havef16
OLLAMA_NUM_PARALLELConcurrent requests per model1
OLLAMA_MAX_LOADED_MODELSHow many models can be in memory simultaneously3
OLLAMA_KEEP_ALIVEHow long models stay loaded after a request5m

Persisting on macOS

Ollama on macOS is launched by launchd, which doesn't see your shell's export. Set persistent env vars with launchctl setenv, then restart the Ollama service so it picks them up.

Context window math

Each parallel request multiplies KV cache. num_ctx=8192 with NUM_PARALLEL=4 = 32K of effective KV cache. OLLAMA_KV_CACHE_TYPE=q8_0 halves that. The default num_ctx on most models is 4096 — bump to 8192 or 16384 for real use, but watch memory.

Code

Persistent tuning on macOS·bash
# Set the five knobs (persists across restarts via launchd)
launchctl setenv OLLAMA_FLASH_ATTENTION 1
launchctl setenv OLLAMA_KV_CACHE_TYPE q8_0
launchctl setenv OLLAMA_NUM_PARALLEL 2
launchctl setenv OLLAMA_MAX_LOADED_MODELS 3
launchctl setenv OLLAMA_KEEP_ALIVE 30m

# Restart Ollama so the daemon picks up the new env
osascript -e 'tell application "Ollama" to quit'
open -a Ollama

# Verify the daemon sees them
curl -s http://localhost:11434/api/version
ps eauwx | grep -i ollama | head -1   # check env in process listing
Linux (systemd) version·bash
# Edit the systemd unit — the official install puts these here:
sudo systemctl edit ollama.service
# Add under [Service]:
#   Environment="OLLAMA_FLASH_ATTENTION=1"
#   Environment="OLLAMA_KV_CACHE_TYPE=q8_0"
#   Environment="OLLAMA_NUM_PARALLEL=2"
#   Environment="OLLAMA_MAX_LOADED_MODELS=3"
#   Environment="OLLAMA_KEEP_ALIVE=30m"

sudo systemctl daemon-reload
sudo systemctl restart ollama

External links

Exercise

Set the five env vars on your machine (persistent — launchctl setenv or a systemd unit edit). Restart Ollama. Now measure the two things these knobs actually move, because none of them speeds up a single warm decode: (1) time the same prompt on a 7B model *cold* and then *warm*, and see how much OLLAMA_KEEP_ALIVE is worth on the second call; (2) note the resident memory in ollama ps with OLLAMA_KV_CACHE_TYPE unset and then at q8_0, at a num_ctx large enough to matter. If a warm decode did get faster, write down what else changed — it was not these variables.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign in — Please sign in to comment.

No comments yet — be the first.