Agentic AI · Intermediate level

Intermediate level · Module 4: External API Integration · Lesson 2 of 3

Reliable API Calls

Learners call APIs from agent tools with httpx, using timeouts, retries with exponential back-off for temporary errors and clear error results for the model.

In this lesson you will

  • Call APIs with timeouts and retries
  • Translate API errors into useful tool results

Key concepts

httpx.Clienttimeoutretry with exponential back-offtemporary vs permanent errorserror messages for the model

The lesson

Every request needs a timeout (for example httpx.get(url, timeout=10)); without one, a slow server can freeze the agent. Rescue Academy's Python programme covers API basics; here the focus is on agent tools.

Retry only temporary failures (timeouts, 5xx errors, 429) and wait longer each time (exponential back-off: 1, 2, 4 seconds), with a maximum number of attempts. Do not retry permanent errors such as 400 or 404.

Return clear, short error results to the model ('price service unavailable, try later') and log the details, so that the agent can explain the problem or continue with other evidence.

Step by step

  1. Write a tool that calls the mock price API with httpx and a timeout.
  2. Add retries with back-off for temporary errors.
  3. Test against the slow and failing endpoints.
  4. Check the tool results the model receives.

Practice activity

Implement a reliable API tool and test it against failure modes.

Expected result: A tool that survives temporary failures, gives up on permanent errors and reports clearly.

Check your understanding

  1. Which errors should be retried?

    Show answer

    Temporary ones such as timeouts, 5xx errors and 429.

  2. Why use exponential back-off?

    Show answer

    To give a struggling service time to recover and avoid flooding it.

Further learning

Taught live by an instructor — online or in person. Free course lessons are for self-study and do not lead to a certificate — see registration options.