Handling HTTP 429 Too Many Requests responses correctly in Python data pipelines requires more than a time.sleep(1) in an except block. The following tools and libraries are the practical toolkit for building rate limit resilience into production data automation. They cover everything from simple retry decorators to distributed rate limiting for multi-worker pipelines.
1. Tenacity
What it does: A Python retry library that provides a decorator-based interface for configuring retry behavior, backoff strategies, and logging.
Why it matters: Rolling your own exponential backoff is straightforward for simple cases, but production pipelines need configurable stop conditions, structured logging of retry attempts, and clean separation between business logic and retry behavior. Tenacity handles all of these.
Installation:
pip install tenacity
Basic usage for API rate limits:
from tenacity import (
retry,
stop_after_attempt,
wait_exponential_jitter,
retry_if_exception_type,
before_sleep_log,
)
import logging
logger = logging.getLogger(__name__)
@retry(
retry=retry_if_exception_type(RateLimitError),
wait=wait_exponential_jitter(initial=1, max=60),
stop=stop_after_attempt(6),
before_sleep=before_sleep_log(logger, logging.WARNING),
)
def call_api(url, session):
response = session.get(url, timeout=30)
if response.status_code == 429:
raise RateLimitError(response.headers.get("Retry-After"))
response.raise_for_status()
return response
Best for: Any production pipeline that makes retried API calls. The before_sleep_log parameter in particular is valuable for operations monitoring -- you get a WARNING log entry before every retry, making it easy to set up alerts when retry rates increase.
GitHub: jd/tenacity | Documentation
2. Requests with Session Reuse
What it does: The requests library's Session object reuses TCP connections across requests, significantly reducing overhead in high-volume API calls.
Why it matters: Each new requests.get() call opens a new TCP connection. At 100 requests per second, this overhead adds up. A Session object maintains a connection pool that reuses established connections, reducing latency and server load.
import requests
with requests.Session() as session:
session.headers.update({"Authorization": "Bearer TOKEN"})
session.headers.update({"User-Agent": "MyPipeline/1.0"})
for url in url_list:
response = session.get(url, timeout=30)
# process response
Best for: Any pipeline making multiple calls to the same API host. The combination of session reuse and rate limiting is the baseline for efficient API automation.
Documentation: requests.readthedocs.io

Photo by Brett Sayles on Pexels
3. PyRateLimit / ratelimit
What it does: A simple decorator library for rate limiting function calls. Enforces a maximum number of calls per time period.
Installation:
pip install ratelimit
Usage:
from ratelimit import limits, sleep_and_retry
CALLS_PER_SECOND = 10
ONE_SECOND = 1
@sleep_and_retry
@limits(calls=CALLS_PER_SECOND, period=ONE_SECOND)
def fetch(url, session):
return session.get(url, timeout=30)
The @sleep_and_retry decorator combined with @limits creates a proactive rate limiter: when the call limit is reached, it sleeps until the window resets rather than raising an exception. This prevents most 429 responses from occurring rather than recovering from them.
Limitations: This library is effective for single-process pipelines but does not coordinate state across multiple workers or processes. For distributed pipelines, you need shared state (see Redis below).
PyPI: pypi.org/project/ratelimit
4. Redis-Based Distributed Rate Limiting
What it does: Uses Redis as a shared rate limit counter across multiple workers, enabling coordinated rate limiting in distributed pipelines.
Why it matters: A token bucket or rate limiter that lives in a single Python process is correct for single-worker pipelines. When you have 10 workers making concurrent requests, each with its own in-process rate limiter, the total request rate will be 10x the per-worker limit. Shared Redis state ensures the total rate is correct regardless of worker count.
Basic pattern using redis-py:
import redis
import time
r = redis.Redis(host="localhost", port=6379)
def acquire_token(key, limit, window_seconds):
"""Sliding window rate limiter backed by Redis."""
now = time.time()
pipe = r.pipeline()
# Remove old entries outside the window
pipe.zremrangebyscore(key, 0, now - window_seconds)
# Count current entries
pipe.zcard(key)
# Add current request timestamp
pipe.zadd(key, {str(now): now})
# Set TTL to clean up automatically
pipe.expire(key, window_seconds + 1)
results = pipe.execute()
current_count = results[1]
if current_count >= limit:
return False # Rate limit exceeded
return True
Best for: Multi-worker or multi-machine pipelines where coordination of API usage across processes is required.
5. httpx with AsyncClient
What it does: An async HTTP client with request/response API compatible with requests, supporting async/await for concurrent API calls.
Why it matters: asyncio-based pipelines can make many concurrent API calls efficiently without multi-threading overhead. httpx integrates cleanly with tenacity for async retry:
import httpx
from tenacity import retry, stop_after_attempt, wait_exponential_jitter
@retry(
wait=wait_exponential_jitter(initial=1, max=60),
stop=stop_after_attempt(5),
)
async def async_fetch(client, url):
response = await client.get(url, timeout=30.0)
if response.status_code == 429:
raise RateLimitError(response.headers.get("Retry-After"))
response.raise_for_status()
return response
async def batch_fetch(urls, headers):
async with httpx.AsyncClient(headers=headers) as client:
tasks = [async_fetch(client, url) for url in urls]
return await asyncio.gather(*tasks, return_exceptions=True)
Caveat: Async concurrency increases the rate at which you consume API quota. Pair httpx with an async-compatible rate limiter (aiolimiter is one option) to avoid immediately hitting limits with the increased concurrency.
6. Monitoring: Structlog for Retry Visibility
What it does: A structured logging library that produces machine-readable log output, making retry events easier to query and alert on.
Why it matters: Knowing that your pipeline is retrying is only useful if you are notified when retry rates increase. Structlog output integrates cleanly with log aggregation systems (Datadog, CloudWatch, Grafana Loki) where you can set alerts on retry event counts.
import structlog
log = structlog.get_logger()
def log_rate_limit_event(response, wait_seconds):
log.warning(
"rate_limit_encountered",
status_code=response.status_code,
retry_after=response.headers.get("Retry-After"),
x_ratelimit_remaining=response.headers.get("X-RateLimit-Remaining"),
wait_seconds=round(wait_seconds, 1),
)
Each retry event emits a structured log record with all the context needed to diagnose pattern changes: which endpoint, how long the wait, how many tokens remain. This is the observability layer that turns rate limit handling from a fire-and-forget implementation into a monitored pipeline component.

Photo by Suki Lee on Pexels
Testing Rate Limit Handling
Rate limit handling code is only as good as its test coverage. Three approaches for testing retry logic without hitting a live API:
Mock the response object: Use unittest.mock.patch to replace requests.Session.get with a function that returns a mock response with status_code=429 for the first N calls, then 200 thereafter. This verifies that your retry loop executes the correct number of retries and calls the wait function with the expected arguments.
httpbin: httpbin.org/status/429 returns a real 429 response, letting you verify that your Retry-After parsing and backoff logic work against a live endpoint without consuming your actual API quota.
Local proxy: A local reverse proxy (nginx, mitmproxy) configured to inject 429 responses at a configured rate lets you test the full pipeline behavior -- including session reuse, token bucket throttling, and retry logging -- under simulated rate limit conditions.
Choosing the Right Combination
For most Python data automation pipelines, the practical starting point is:
- tenacity for retry logic (reactive)
- ratelimit or a hand-rolled token bucket for rate throttling (proactive)
- requests Session for connection efficiency
- Structured logging for observability
For distributed pipelines with multiple workers:
- Add Redis for shared rate limit state across workers
- Consider httpx + asyncio for high-concurrency fetch patterns
The full implementation of these patterns -- including a complete working example combining tenacity, token bucket, and session reuse -- is covered in How to Handle API Rate Limits in Python Data Automation.
For production data pipeline work where rate limit resilience is a design requirement from day one, visit https://137foundry.com/services/data-automation.
Top comments (0)