Fix Ollama 5-Minute Timeout
Real Issue from the Community
This guide is based on a real troubleshooting session from the OpenClaw Discord. A user running an RTX 4080 with a 20B model experienced generation cuts at exactly 5 minutes — every time.
Your Ollama model generates long responses fine via curl, but OpenClaw cuts the connection at exactly 5 minutes with a 500 error — then enters an infinite retry loop. The root cause is a hardcoded timeout in Node.js's HTTP client.
Next Step
Fix now, then reduce repeat incidents
If this issue keeps coming back, validate your setup in Doctor first, then harden your config.
Jump to Section
Symptoms
What You See
- •Generation stops at exactly 5 minutes (300 seconds), never more
- •Ollama logs show a 500 error at the 5m0s mark
- •OpenClaw immediately retries the entire generation from scratch
- •Retry hits the same 5-minute wall → infinite loop
Key Diagnostic
If curl requests to Ollama work fine for 10+ minutes, the issue is inside OpenClaw's HTTP client — not Ollama.
Confirm with Log Filtering
# Look for the specific undici error codes openclaw logs --follow | grep -E 'UND_ERR_HEADERS_TIMEOUT|UND_ERR_BODY_TIMEOUT'
If you see UND_ERR_HEADERS_TIMEOUT, this is your problem.
Root Cause: undici 300-Second Default
OpenClaw uses Node.js's built-in undici HTTP client. This client has a hardcoded 300-second (5-minute) headersTimeout. If the server doesn't complete the response within this window, undici kills the connection.
You send a prompt to a large local model
A 20B+ model on GPU needs 6-10 minutes for complex responses.
undici starts a 300s countdown
Node.js's HTTP client begins timing the request.
At exactly 5 minutes, undici aborts
The connection is killed with UND_ERR_HEADERS_TIMEOUT.
OpenClaw treats it as a transient error
isError=true triggers automatic retry — restarting the entire generation.
Why a Simple Override Doesn't Work
OpenClaw uses the @mariozechner/pi-ai library which calls setGlobalDispatcher(new EnvHttpProxyAgent()) — this overwrites any custom dispatcher you set, resetting timeouts back to 300s.
The Fix: Monkey-Patch Preload Script
The solution is a Node.js preload script that sets the timeout and prevents other libraries from resetting it.
Step 1: Create the Preload Script
Save this as undici-timeout-preload.cjs (e.g., in your home directory):
const undici = require("undici");
const { EnvHttpProxyAgent } = undici;
const OPTS = {
headersTimeout: 30 * 60 * 1000, // 30 minutes
bodyTimeout: 0, // Disable body timeout
};
const realSet = undici.setGlobalDispatcher.bind(undici);
function enforce() {
realSet(new EnvHttpProxyAgent(OPTS));
}
// 1. Set the custom dispatcher immediately
enforce();
// 2. Prevent other libraries from resetting it
undici.setGlobalDispatcher = function () {
enforce();
};Step 2: Load the Script at Startup
Linux (systemd)
systemctl edit --user openclaw # Add these lines: [Service] Environment="NODE_OPTIONS=--require /home/user/undici-timeout-preload.cjs"
Manual Start (Any OS)
NODE_OPTIONS="--require /path/to/undici-timeout-preload.cjs" \ openclaw gateway start
Windows (PowerShell)
$env:NODE_OPTIONS="--require C:\Users\YourName\undici-timeout-preload.cjs" openclaw gateway start
To make this permanent on Windows, add the environment variable via System Properties → Advanced → Environment Variables.
Disable the Retry Loop
Even after fixing the timeout, you should prevent OpenClaw from auto-retrying expensive long generations:
# Set retry attempts to 1 (no retry) for Ollama
openclaw config set models.providers.ollama.retry '{"attempts": 1}' --json
openclaw gateway restartThis prevents OpenClaw from restarting the entire generation from scratch if anything goes wrong. You'll get an error message instead of silently burning GPU time in a loop.
Why Streaming Doesn't Help
You might think streaming would bypass this (since the first byte arrives quickly). However:
OpenClaw disables streaming by default for Ollama due to bugs in the upstream SDK that cause garbled output with tool-calling models. This means the entire response must complete before any data is returned — hitting the timeout for long generations.
Verification
After applying the fix, verify it works:
# 1. Start gateway with the preload script NODE_OPTIONS="--require /path/to/undici-timeout-preload.cjs" \ openclaw gateway run --verbose # 2. Send a prompt that takes >5 minutes # 3. Monitor for timeout errors openclaw logs --follow | grep -E 'UND_ERR|timeout|500'
If no UND_ERR_HEADERS_TIMEOUT appears and the response completes, the fix is working.
Safety Warning
Do not set timeouts to 0 (infinity). A stuck request could hang forever and consume resources. The 30-minute limit in the preload script is a safe upper bound for even the largest models.
Still Stuck?
Run our diagnostic tool to catch issues automatically:
npx clawkit-doctor@latestSeeing Other Errors Too?
Ollama timeout issues often come with these related problems:
Did this guide solve your problem?