Start learning free

LESSON 8 OF 129 · AI ENGINEERING: CALLING A MODEL

Timeouts in Python

Practice

Sometimes the API doesn't say no: it says nothing, for a long time. Left alone, the SDK waits up to ten minutes for an answer, and your program, with the customer in front of it, waits too. A timeout is the longest you agree to wait.

The idea is the same for any service you call. With the requests library, for example, a call that waits 2 seconds at most looks like this:

try:
    answer = requests.get("https://supplier.example/prices", timeout=2)
except requests.Timeout:
    print("The supplier is too slow today.")

The Anthropic client takes its limit when you create it: timeout=1.0 means give up after one second without an answer. When the time is up, the SDK raises anthropic.APITimeoutError. With max_retries=0 the SDK doesn't quietly try again by itself, so you see it happen.

The course model takes 3 seconds over its first answer and answers the second call at once. Your program must give up on the first call after 1 second, say so, and try once more:

No answer after 1 second. Trying once more.
A pocket notebook with thick paper, ready for lists, sketches and big ideas.

Your task: MODEL and messages are already there. Create the client with timeout=1.0 and max_retries=0, call the model inside a try, catch the timeout error, print the notice and make the same call again, then print the reply's text.


Assignment
Follow the Coding Tutorial to give the call a one-second timeout, catch anthropic.APITimeoutError and try once more.


Hint
The second call goes inside the except, and it also keeps its result in reply. The last print has no indent, so it runs whichever call answered.


STEP 1 OF 7
    Ctrl + Enter to check

    Ai Timeouts in Python

    Keep going with Python1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free