AI Engineer Problems › Error Handling & Edge Cases

Exponential Backoff with Jitter

Medium · Coding · Error Handling & Edge Cases · Asked at Stripe, Airbnb

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:

  1. Raise a ValueError if attempt is negative.
  2. Compute the delay using the backoff and max bounds.
  3. Add the computed jitter: jitter_factor * jitter_range (where jitter_factor is a float between 0.0 and 1.0).
  4. Return the calculated delay.

Solve it in the browser on GenAIDojo