{"slug": "how-to-handle-api-rate-limits-in-python-like-a-pro-with-exponential-backoff", "title": "How to Handle API Rate Limits in Python Like a Pro (with Exponential Backoff)", "summary": "A developer building an AI backend that calls the Gemini API described how to stop server crashes from 429 Too Many Requests errors by implementing exponential backoff in Python using the tenacity library. The approach retries failed requests with waits of 2, 4, then 8 seconds capped at 10 seconds, gives up after five attempts, and can honor a Retry-After header when an API provides one.", "body_md": "If you are building applications that rely on external APIs (like OpenAI, Gemini, GitHub, or even just fetching job data), you have probably run into the dreaded 429 Too Many Requests error.\n\nWhen I was building my AI backend, my server kept crashing because I was hammering the Gemini API too fast.\n\nIf you just write a try/except block and instantly try again, you will get banned. You need Exponential Backoff. Here is how it works and how to build it in Python.\n\n**What is Exponential Backoff?**\n\nInstead of retrying a failed request immediately, you wait a little bit. If it fails again, you wait twice as long. If it fails again, you wait twice as long as that.\n\n**· Attempt 1 fails -> Wait 1 second.**\n\n**· Attempt 2 fails -> Wait 2 seconds.**\n\n**· Attempt 3 fails -> Wait 4 seconds.**\n\n**· Attempt 4 fails -> Wait 8 seconds.**\n\nThis gives the API server time to breathe and clear its queue, making your request much more likely to succeed on the next try.\n\n**The Python Code (Using the tenacity Library)**\n\nYou could write this logic from scratch using a while loop, but why do that when Python has an incredible library called tenacity?\n\n**First, install it:**\n\n```\npip install tenacity\n```\n\n**Now, let's build a robust API request function:**\n\n``` python\nimport httpx\nfrom tenacity import retry, stop_after_attempt, wait_exponential, retry_if_exception_type\n\n# We only want to retry if we hit a 429 (Rate Limit) or 500 (Server Error)\ndef is_rate_limit_error(exception):\n    return isinstance(exception, httpx.HTTPStatusError) and exception.response.status_code in [429, 500]\n\n@retry(\n    retry=retry_if_exception_type(httpx.HTTPStatusError), \n    wait=wait_exponential(multiplier=1, min=2, max=10),\n    stop=stop_after_attempt(5)\n)\nasync def fetch_external_data(url: str):\n    print(f\"Attempting to fetch: {url}\")\n    async with httpx.AsyncClient() as client:\n        response = await client.get(url, timeout=10.0)\n\n        # Raise an exception if the status code is 4xx or 5xx\n        response.raise_for_status() \n        return response.json()\n```\n\n**What does this code do?**\n\n**· wait_exponential(multiplier=1, min=2, max=10):** This is the magic. It will wait 2s, then 4s, then 8s, capping at 10s so you don't wait forever.\n\n**· stop_after_attempt(5):** It will try 5 times before finally giving up and raising the error to your main application.\n\n**Bonus Tip: Respect the Retry-After Header**\n\nIf an API is really nice, it will send a Retry-After header in the 429 response telling you exactly how many seconds to wait. Tenacity can be configured to read this, but even with a standard exponential backoff, your server will stop crashing and start handling failures gracefully.\n\nHave you ever had your server crash from an API rate limit? Let me know below!", "url": "https://wpnews.pro/news/how-to-handle-api-rate-limits-in-python-like-a-pro-with-exponential-backoff", "canonical_source": "https://dev.to/ezenwa_christian_5243b406/how-to-handle-api-rate-limits-in-python-like-a-pro-with-exponential-backoff-2k7", "published_at": "2026-11-15 12:11:07+00:00", "updated_at": "2026-11-15 12:21:26.980121+00:00", "lang": "en", "topics": ["ai-tools", "developer-tools"], "entities": ["Gemini", "OpenAI", "GitHub", "tenacity", "httpx"], "also_reported_by": [], "alternates": {"html": "https://wpnews.pro/news/how-to-handle-api-rate-limits-in-python-like-a-pro-with-exponential-backoff", "markdown": "https://wpnews.pro/news/how-to-handle-api-rate-limits-in-python-like-a-pro-with-exponential-backoff.md", "text": "https://wpnews.pro/news/how-to-handle-api-rate-limits-in-python-like-a-pro-with-exponential-backoff.txt", "jsonld": "https://wpnews.pro/news/how-to-handle-api-rate-limits-in-python-like-a-pro-with-exponential-backoff.jsonld"}}