keep_alive: -1 — keeping an Ollama model in RAM between calls
By default, Ollama unloads a model from RAM after 5 minutes of inactivity.
The next request reloads it from disk. On CPU, that takes 10-15 seconds.
For an agent generating messages on demand, that's a visible delay on every call spaced more than 5 minutes apart.
The fix
Pass keep_alive: -1 on every call.
await ollama.chat({
model: "qwen2.5:7b",
messages,
stream: true,
keep_alive: -1, // keeps the model in RAM indefinitely
options: { num_predict: 100 },
});The model stays loaded between calls. No more 13-second reloads.
When not to use it
If the machine has little RAM and several models are loaded in parallel, keep_alive: -1 can waste memory unnecessarily.
The default 5-minute value is reasonable for interactive use (one-off questions). It becomes a penalty for an agent generating messages at irregular intervals — sometimes 2 minutes apart, sometimes 20.
Centralizing the client
Instead of passing keep_alive on every call in every file, use a single shared Ollama instance:
// src/ollama/manager.ts
export const ollamaClient = new Ollama({
host: config.ollama.host,
fetch: fetchWithTimeout(10 * 60 * 1000),
});And keep_alive: -1 on each chat() call. Every module imports ollamaClient instead of instantiating its own new Ollama().