Start learning free

LESSON 7 OF 129 · AI ENGINEERING: CALLING A MODEL

When the API Says No: 429 and 529

Practice

A model's API is a busy service, and sometimes it answers with an error status instead of a message. Two of them are part of everyday life: 429, you sent too many requests in a short time, and 529, the service is overloaded right now. Neither means your request is wrong: the same request works a moment later.

The SDK turns an error status into a Python exception: anthropic.RateLimitError for a 429, and anthropic.APIStatusError for any error status, with the number in .status_code. You catch them with try and except, the same way you catch any other error:

try:
    number = int("ten")
except ValueError:
    print("That is not a number")

After a failure, don't ask again at once: wait, and wait longer after every failure. This is called backoff. By default the SDK does this quietly for you, up to two more tries. In this lesson you turn that off with max_retries=0 and do the work yourself, to see what happens underneath.

The course model answers 429 to the first call, 529 to the second, and a real answer from the third on. Your program must print:

Attempt 1 failed: too many requests (429)
Waiting 0.2 seconds
Attempt 2 failed: status 529
Waiting 0.4 seconds
Attempt 3 worked: A slim desk lamp with a warm, steady light for late-night reading.

Your task: MODEL, messages, wait = 0.2 and reply = None are already there. Create the client with max_retries=0, then try the call up to 5 times: catch the 429 and any other error status, stop at the first answer, and otherwise wait and double the wait.


Assignment
Follow the Coding Tutorial to build a retry loop that catches 429 and 529, waits longer after each failure and stops at the first answer.


Hint
The except for anthropic.RateLimitError must come before the one for anthropic.APIStatusError: Python uses the first except that fits. The waiting lines come after the if with the break, so they run only when the attempt failed.


STEP 1 OF 11
    Ctrl + Enter to check