Guides › Rate Limits and Backoff: Being a Good API Citizen
Rate Limits and Backoff: Being a Good API Citizen
Every public API protects itself against overload, accidental infinite loops, and abusive scrapers using rate limits. If a caller sends too many requests in a short window, the server answers with HTTP 429 Too Many Requests.
Imagine a busy bakery with one counter clerk. If fifty people rush the counter at the exact same second, nobody gets served. So the bakery installs a number dispenser: customers take a ticket and wait their turn. If someone tries to skip the line, the clerk asks them to step back for 10 seconds before ordering.
How rate limits work
When an API throttles your requests, it includes a Retry-After header in the response. This header tells your code exactly how many seconds to pause before sending your next request.
Handling rate limits three ways
1. In your browser
If you rapidly hit refresh dozens of times on an API endpoint, the server temporarily blocks your IP address with an HTTP 429 status code to protect its resources.
2. With curl
Using curl with -i displays the Retry-After header when you exceed the threshold:
curl -i https://api.computewithpour.com/v1/tips
3. With Python (Polite Retry)
Never write an infinite retry loop that hammers a throttled server. Instead, read Retry-After and sleep politely:
import time
import requests
def polite_get(url: str):
for attempt in range(3):
r = requests.get(url)
if r.status_code != 429:
return r
wait_seconds = int(r.headers.get("Retry-After", "5")) + attempt
time.sleep(wait_seconds)
return r
Try it in the API Lab
The ComputeWithPour practice API allows 60 requests per minute. Explore endpoints like /v1/tips in the API Lab. Notice that caching responses locally prevents unnecessary network traffic.
What to notice
Status code 429 is not an error in your logic; it is a traffic control signal. Resilient applications implement exponential backoff: if your request is rejected, wait 1 second, then 2 seconds, then 4 seconds. Adding slight random variation (called jitter) ensures that hundreds of workers do not wake up and hit the server at the exact same instant.
Professional Python networking
Writing code that respects rate limits, handles retries, and caches server responses is a hallmark of professional software development. To see how production systems combine HTTP clients, data analysis, and background tasks, check out Python Top Ten Uses in our library.
Challenge
Write a Python function safe_get(url) that handles HTTP 429 by sleeping for the duration specified in the Retry-After header (or a default backoff) and retrying up to 3 times before returning the response.
Show the answer
Inspect r.status_code == 429. When throttled, parse r.headers.get('Retry-After', '1'), call time.sleep(), and retry the request. When status is 200, return immediately.
import time
import requests
base = "https://api.computewithpour.com"
def safe_get(url: str, retries: int = 3):
for attempt in range(retries):
r = requests.get(url)
if r.status_code != 429:
return r
wait = int(r.headers.get("Retry-After", "1")) + attempt
time.sleep(wait)
return r
resp = safe_get(f"{base}/v1/tips/2")
print("Status:", resp.status_code)
print("Tip:", resp.json()["tip"])