Command Palette

Search for a command to run...

Hectal
Phase 2: Calling a Model
LLM Engineering · Exercise 4 of 6 PreviousNext
Intermediate

Retry only what can succeed

send(attempt) returns (status, headers, text) for the given attempt number (starting at 0). Write call_with_retries(send, max_attempts=3) that returns (text, waits) on status 200.

Retry only statuses 408, 429 and 500 to 599. After each failed attempt that will be retried, append a wait in seconds to waits: the integer in the retry-after header if present, otherwise 2 ** attempt. Raise ApiError(status) immediately for any other status, and also when the last attempt fails.

What Check tests

  1. Succeeds after a 429 (with Retry-After) and a 503
  2. A 401 is not retried: exactly one call
  3. Gives up after max_attempts and raises the last status
  4. Immediate success returns no waits

Stuck? One hint at a time

The solution unlocks after your first Check. Try first: that is where the learning happens.

main.py
Sign in to run your code.

Output

Press Run to see what your program prints.