Command Palette

Search for a command to run...

Hectal
Phase 6: Cost, Latency and Limits
LLM Engineering · Exercise 4 of 6 PreviousNext
Intermediate

Write the retry loop

Write call_with_retry(send, max_attempts, sleep). send(attempt) returns (status, text, retry_after_s); attempt counts from 1. sleep(ms) is a function you call to wait (the tests record it instead of waiting).

RETRYABLE, NonRetryable and GaveUp are already defined in the starter. On status 200 return the text. If the status is not in RETRYABLE, raise NonRetryable(status) at once without sleeping. Otherwise, if it was the last attempt raise GaveUp, else call sleep(wait) with wait = max(500 * 2 ** (attempt - 1), retry_after_s * 1000) (leave out the second part when retry_after_s is None) and try again.

What Check tests

  1. Two 503s then 200 returns the text and sleeps 500 then 1000
  2. A 429 with Retry-After of 3 seconds sleeps 3000 ms
  3. A 400 raises NonRetryable and never sleeps
  4. Always 503 with 3 attempts raises GaveUp after sleeping twice
  5. It stops asking once it succeeds

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.