Hitting an API rate limit and not knowing what to do with the HTTP 429 response is one of the most common causes of brittle data automation scripts. This is a step-by-step implementation guide: from a minimal correct backoff function to a production-grade tenacity decorator that logs retries, handles Retry-After headers, and distinguishes between retriable and non-retriable errors.

What We Are Building

By the end of this guide, you will have:

A calculate_wait() function that reads Retry-After headers when present and falls back to exponential backoff with jitter when not

A fetch_with_backoff() wrapper function for single requests