Home › Interview Questions › In Python, how would you handle throttling errors …

In Python, how would you handle throttling errors when calling an external service?

🟡 Medium Coding Junior level
1Times asked
Oct 2026Last seen
Oct 2026First seen

💡 Model Answer

When calling an external API in Python, throttling errors (HTTP 429) indicate you exceeded the rate limit. The typical approach is to implement retry logic with exponential backoff and jitter. Use the requests library to send the request, catch requests.exceptions.HTTPError, check response.status_code == 429, then sleep for an increasing interval: time.sleep(min(backoff 2 retry, max_backoff)). Optionally read the Retry-After header to respect the server's suggested wait time. Libraries like tenacity or urllib3's Retry can automate this. Example code: import requests, time; def call_api(url, retries=5, backoff=1): for i in range(retries): try: resp = requests.get(url); resp.raise_for_status(); return resp.json(); except requests.exceptions.HTTPError as e: if e.response.status_code == 429: wait = int(e.response.headers.get('Retry-After', backoff 2 i)); time.sleep(wait); continue; raise; The complexity is O(retries) per call; overall linear in the number of requests. This ensures graceful handling of throttling and keeps the application responsive.

This answer was generated by AI for study purposes. Use it as a starting point — personalize it with your own experience.

🎤 Get questions like this answered in real-time

Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers on a discreet on-screen overlay.

Get Assisting AI — Starts at ₹500