Skip to content

< all problems03 · Level 01, LLM APIs

Retry a Failed Model Call

easy · implement · LLM Fundamentals

Implement call_with_retry(fn, max_attempts=3, base_delay=1.0). Call fn() and return its result. If it raises:

  1. Retry RateLimitError, TimeoutError and ServerError.
  2. Do not retry anything else. A ValueError will fail the same way forever.
  3. Wait base_delay * (2 ** attempt) before each retry (1s, 2s, 4s), using the provided sleep() rather than time.sleep, so the tests don't actually wait.
  4. After max_attempts total attempts, re-raise the last exception.