Retries with exponential backoff
A busy model fails now and then. Retry without hammering the server: 100, 200, 400 ms.
Write withRetry(fn, { retries, baseMs }). Call fn(); if it throws, wait with sleep(ms) and try again. The wait doubles each time: baseMs, baseMs * 2, baseMs * 4… When retries run out, throw the last error.
You already have sleep(ms) and makeFlaky(n), which builds a function that fails n times and then returns 'ok'. Always use sleep: that is how the challenges check the waits.
Challenges 0/4
- Returns 'ok' after two failures
- Waits 100 then 200 ms (doubles)
- Throws once retries run out
- Does not wait when the first call works
async function withRetry(fn, { retries = 3, baseMs = 100 } = {}) {
// try fn(); on error wait sleep(baseMs * 2 ** attempt) and try again
return fn();
}
withRetry(makeFlaky(2), { retries: 3, baseMs: 100 }).then(console.log, (e) => console.error(e.message));Tab indents, Ctrl+Z undoes. To leave the editor with the keyboard: Esc, then Tab.
Why this matters
A model server fails temporarily more often than an ordinary API: the model is loading into memory, the queue is full, or the process is restarting. Retrying at once makes exactly those cases worse, because every client comes back together. Doubling the wait gives the model time to finish loading, and a cap on attempts stops a real outage from turning into a loop. It is the first thing to add to any call to Ollama from a script. More patterns of this kind are on the developers page.
Go deeper: the AWS reference →
This in production, with your data? Let's talk for 15 minutes →