Intermediate level · Module 1: Python for AI Applications · Lesson 3 of 3
Errors, Retries and Logging
Make API programs robust to network problems and rate limits, and keep useful logs.
In this lesson you will
- Handle exceptions and timeouts in API calls
- Retry with backoff and log events
Key concepts
exceptionstimeoutsretry with backofflogging
The lesson
Networks fail, especially on mobile connections. Always set timeouts and catch exceptions such as connection errors, so your program reports the problem instead of crashing.
Services limit how many requests you can send (rate limits) and return status 429 when you exceed them. Retry a few times with increasing waits (backoff).
Use Python's logging module to record requests, errors and timings — but never log keys or personal data.
Step by step
- Add a timeout and exception handling.
- Simulate a failure and check the message.
- Add retries with backoff for 429 and 5xx responses.
- Add logging without secrets.
Practice activity
Complete the robust API script from the practical activity.
Expected result: A script that survives failures, retries sensibly and logs safely.
Check your understanding
-
What does status 429 mean?
Show answer
Too many requests — a rate limit was exceeded.
-
What must never be written to logs?
Show answer
API keys and personal data.
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.