Exponential Backoff with Jitter
When querying external LLM APIs (like OpenAI or Anthropic), you often hit rate limits (HTTP 429). To avoid overwhelming the server, you should retry using an exponential backoff sequence, combined with a random "jitter" to spread out retry load.
The formula to compute the retry delay for retry attempt $i$ (0-indexed) is:
$Wait_i = \min(MaxWait, Base \cdot 2^{attempt}) + Jitter$
Where:
- $Base$ is the starting retry delay in seconds.
- $MaxWait$ is the maximum allowable retry delay.
- $Jitter$ is a random factor between $0$ and $JitterRange$.
To keep this challenge deterministic for testing, we will pass the random jitter multiplier jitter_factor directly as a parameter.
Tasks
Implement the function calculate_retry_delay(attempt, base_delay, max_delay, jitter_range, jitter_factor) to:
- Raise a
ValueErrorifattemptis negative. - Compute the delay using the backoff and max bounds.
- Add the computed jitter:
jitter_factor * jitter_range(wherejitter_factoris a float between 0.0 and 1.0). - Return the calculated delay.