Compute full-jitter and equal-jitter retry delays
Why it matters Every production agent that calls rate-limited LLM or tool APIs needs jittered backoff to avoid synchronized retry storms.
When many agents retry a failing API at the same moments, they hammer it in waves. Adding random jitter to exponential backoff spreads the retries out.
Write backoff_schedule(attempts, base, cap, uniforms, mode). uniforms is a list of at least attempts floats in [0, 1). For retry k = 0, 1, ..., attempts - 1 let ceiling = min(cap, base * 2 ** k), then:
- mode "full": delay = uniforms[k] * ceiling
- mode "equal": delay = ceiling / 2 + uniforms[k] * ceiling / 2
Return the list of delays as floats.
def backoff_schedule(attempts, base, cap, uniforms, mode)Example
backoff_schedule(3, 1.0, 10.0, [0.5, 0.5, 0.5], "full") gives [0.5, 1.0, 2.0].
Understand the idea
Exponential backoff waits longer after each failure: 1 s, 2 s, 4 s and so on, up to a cap. But if a thousand clients fail at the same moment, plain backoff makes them all retry at the same moments too, and the server is hit by synchronized waves. Jitter adds randomness so retries spread out, like people leaving a stadium through different gates.
Example with base 1, cap 10 and a draw of 0.5 each time: ceilings are 1, 2, 4, so full jitter waits 0.5, 1 and 2 seconds. Equal jitter would wait 0.75, 1.5 and 3.
Rules:
ceiling = min(cap, base * 2^k)
full: wait = U(0, 1) * ceiling
equal: wait = ceiling / 2 + U(0, 1) * ceiling / 2
Full jitter gives the lowest total load on the server; equal jitter guarantees some minimum wait. Agent runtimes use these when LLM APIs return rate-limit errors.