Tidqom — Source-linked AI and developer tools
AITid
AI Tools

How to Stop Ollama From Unloading Models (keep_alive)

Ollama unloads models from VRAM after 5 minutes of inactivity. Here is exactly how to use the keep_alive parameter to force your models to stay loaded so you never wait for a cold start again.

T
Tidqom Editorial
August 8, 2026 · 5 min read
How to Stop Ollama From Unloading Models (keep_alive)

The 5-Minute Unload Problem

Related: Fix LM Studio "Failed to load model" Error →

You send a prompt to your local model. You get an instant response. You read an article for ten minutes, switch back to your terminal, and send a follow-up prompt. Then you sit there watching a blinking cursor for twenty seconds while your GPU fans spin up.

This is the cold start problem. By default, Ollama keeps a model loaded in memory for exactly five minutes after its last generation. If you do not interact with the API within that window, Ollama unloads the model weights from your VRAM to free up system resources.

When you check your server logs, you will see exactly when it happens:

text
time=2023-11-04T10:15:30.000Z level=INFO source=server.go:112 msg="unloading model" model=llama3

When you send your next request, Ollama has to read 4GB to 15GB of model data from your storage drive and push it back into the GPU. Even on a fast NVMe SSD, this takes time.

If you have dedicated hardware and want instant responses every time, you need to override this timeout. You do this using the keep_alive parameter. You can set it to a specific duration like 24h, or set it to -1 to keep the model loaded in memory forever. Here is exactly how I configure this across different setups.

Method 1: The API Request Fix

Related: Fix Stable Diffusion Out Of Memory on a 6GB VRAM GPU →

If you are building a custom application, the cleanest way to control model persistence is right inside your HTTP payload. Ollama accepts a keep_alive field in both the /api/generate and /api/chat endpoints.

This is the method I use when building a local RAG chatbot because my application code dictates exactly when the model should stay hot and when it should drop.

Here is the cURL command to keep a model loaded indefinitely by passing -1:

bash
curl http://localhost:11434/api/generate -d '{
  "model": "llama3",
  "prompt": "Why is the sky blue?",
  "keep_alive": -1
}'

You can also pass a formatted duration string. If you want the model to stay loaded for one hour, you pass "1h".

Here is how that looks in Python using the requests library:

python
import requests
import json

url = "http://localhost:11434/api/chat" payload = { "model": "llama3", "messages": [ {"role": "user", "content": "Write a python script to parse CSV."} ], "keep_alive": "1h" }

response = requests.post(url, json=payload) print(response.json())

terminal
Advertisement — In Article

The limitation here is that you have to include this parameter in every single API call. If you send a request without it, Ollama reverts to the default 5-minute countdown from that moment forward.

Method 2: Systemd Configuration on Linux

Related: Fix ComfyUI "Torch Not Compiled With CUDA Enabled" Error →

If you run Ollama as a background service on Linux, modifying the API requests is annoying. I prefer to set a global environment variable so the model stays loaded no matter how I interact with it.

When you install Ollama on Linux using their official script, it sets up a systemd service. You cannot just export a variable in your .bashrc because the background service will not read it. You have to inject OLLAMA_KEEP_ALIVE directly into the systemd config.

Open your terminal and run the systemctl edit command:

bash
sudo systemctl edit ollama.service

This opens a blank override file in your default text editor. Add the following three lines exactly as written:

ini
[Service]
Environment="OLLAMA_KEEP_ALIVE=-1"

Save the file and exit. Next, reload the systemd daemon so it registers the new configuration:

bash
sudo systemctl daemon-reload

Then restart the Ollama service:

bash
sudo systemctl restart ollama

If Ollama fails to start after this, check your syntax in the override file. A missing quote will crash the service, and you might find yourself staring at an Ollama connection refused on 127.0.0.1:11434 error. You can verify the variable was applied by checking the service environment:

bash
sudo systemctl show ollama | grep OLLAMA_KEEP_ALIVE

Method 3: Launch Daemon and Environment Variables (macOS & Windows)

Related: A One-File Docker Compose Stack for Ollama and Open WebUI →

The global fix is slightly different on macOS and Windows because they handle background processes differently than Linux.

macOS Configuration

If you run the Ollama Mac app (the one that lives in your top menu bar), putting export OLLAMA_KEEP_ALIVE="-1" in your .zshrc file will do absolutely nothing. The menu bar app is spawned by macOS launchd, which does not read your terminal profiles.

Advertisement — In Article

To set the variable globally for the UI app, open your terminal and use launchctl:

bash
launchctl setenv OLLAMA_KEEP_ALIVE "-1"

After running this command, quit the Ollama app from the menu bar and reopen it. The setting will apply. Note that launchctl setenv resets when you reboot your Mac. If you want it to persist across reboots, you will need to create a simple .plist file in ~/Library/LaunchAgents/ to run that command on login.

Windows Configuration

If you are using the native Windows preview of Ollama, you set this via the System Environment Variables GUI.

  1. Press the Windows key and type "Environment Variables".
  2. Click "Edit the system environment variables".
  3. Click the "Environment Variables..." button at the bottom.
  4. Under the "System variables" section, click "New".
  5. Set Variable name to OLLAMA_KEEP_ALIVE.
  6. Set Variable value to -1.
  7. Click OK.

Right-click the Ollama icon in your system tray and select "Quit". Open it again from the Start menu. The model will now stay loaded forever.

Method 4: Keeping Models Loaded in Docker

Related: Fixing Painfully Slow Whisper Transcription →

Many people run Ollama in Docker containers to keep their host OS clean. Passing environment variables in Docker is straightforward, but it is easy to forget to persist them if you tear down your containers often.

If you are running the container via the command line, use the -e flag to pass the keep alive value.

bash
docker run -d --gpus=all -v ollama:/root/.ollama -p 11434:11434 --name ollama -e OLLAMA_KEEP_ALIVE="-1" ollama/ollama

If you use Docker Compose, add it to the environment block in your docker-compose.yml file:

yaml
services:
  ollama:
    image: ollama/ollama
    container_name: ollama
    ports:
      - "11434:11434"
    volumes:
      - ollama:/root/.ollama
    environment:
      - OLLAMA_KEEP_ALIVE=-1
    deploy:
      resources:
        reservations:
          devices:
            - driver: nvidia
              count: 1
              capabilities: [gpu]

Rebuild your container with docker compose up -d. If your GPU is not triggering and the model falls back to CPU RAM, check your Docker Nvidia runtime. I ran into this recently and had to troubleshoot Ollama not using my NVIDIA GPU in WSL2.

How to Manually Unload a Model

Related: Fix Open WebUI Showing No Models in the Dropdown →

When you set keep_alive to -1, your model hogs VRAM indefinitely. If you have a 12GB GPU and you load an 8GB model, you now only have 4GB left for gaming, video editing, or running a second model.

Advertisement — In Article

When you want your memory back, you do not need to restart the Ollama service. You can manually force Ollama to unload the model by sending an API request with a keep_alive value of 0.

bash
curl http://localhost:11434/api/generate -d '{
  "model": "llama3",
  "keep_alive": 0
}'

This immediately flushes the model from memory. You will see the VRAM usage drop in nvidia-smi instantly. I keep a simple bash alias in my terminal specifically for this command so I can clear my GPU without thinking about it.

What Did NOT Work

When I first tried to solve the cold start problem, I went down a few dead ends. I am listing them here so you do not waste your time.

The Modelfile Myth

I assumed I could bake the keep alive setting directly into a custom model. I created a Modelfile and added PARAMETER keep_alive -1. I ran ollama create my-model -f Modelfile. It built successfully, but the parameter was completely ignored. Ollama's Modelfile specification only supports model-specific parameters like temperature, num_ctx, and stop tokens. It does not control server-level behavior.

Setting it to zero

Before I read the documentation, I passed "keep_alive": 0 thinking zero meant "infinite" (a common convention in other software). In Ollama, zero literally means zero seconds. The model unloaded instantly after generating the first token, making my performance dramatically worse.

Tweaking OLLAMA_HOST

I mistakenly thought my connection to the server was dropping, causing the model to unload. I spent an hour changing IP bindings and port configurations. The host variable has absolutely nothing to do with memory management.

Measuring the VRAM Trade-off

Setting keep_alive to -1 is not a free lunch. You are trading RAM/VRAM capacity for speed. If you are running a local LLM on 8GB of RAM, leaving a model loaded permanently might make your entire operating system sluggish because the OS cannot reclaim that memory for other applications.

Here is a look at the actual numbers on my test bench (RTX 3060 12GB) using Llama 3 8B.

StateVRAM UsedNext Prompt DelayOS Impact
Unloaded400 MB14.2 secondsNone
Loaded (keep_alive)5.8 GB0.4 secondsHigh
Manually Flushed400 MB14.5 secondsNone

When the model is unloaded, Ollama idles at a few hundred megabytes. When it is loaded, it reserves almost 6GB. If I want to open DaVinci Resolve or play a game, I have to manually flush the model first.

My rule of thumb: If you have 24GB of VRAM (like an RTX 3090), set OLLAMA_KEEP_ALIVE="-1" globally and forget about it. If you have 8GB of VRAM, use the 5-minute default, or trigger -1 via the API only during active chat sessions.

FAQ

Question: What is the default keep_alive time in Ollama?

Ollama defaults to 5 minutes. If no requests are made to the model within that window, it unloads from your GPU VRAM or system RAM to free up hardware resources.

Question: Can I load multiple models at once with keep_alive?

Yes, if you have enough VRAM. Set OLLAMA_NUM_PARALLEL and OLLAMA_MAX_VRAM accordingly, then send requests to both models with keep_alive set to -1.

Question: Does keep_alive work with the Ollama WebUI (Open-WebUI)?

Yes. Open-WebUI has a specific setting in the admin panel under Settings > Models. You can set the Keep Alive duration there, which overrides the Ollama default.

Question: Why did my model unload even with keep_alive set to -1?

If you restart the Ollama background service, reboot your machine, or trigger an out-of-memory error that crashes the server, the loaded models are flushed from memory.

Advertisement

مواضيع مقترحة · Suggested Topics

استكشف مواضيع ومحاور ذات صلة بهذا المقال — روابط داخلية لتعميق قراءتك.

The Daily Pulse

Newsletter delivery is not connected yet. This form only saves your address in this browser; no email is sent.

Get concise, source-linked technology notes without the hype.

Advertisement