Back to skills
SKILL.md
2644 Exponential Backoff 0eea2e94
ASecurity```python import time import random from functools import wraps from typing import TypeVar, Callable
- 9 stars
- 0 votes
- 0 copies
- 0 views
- Added October 11, 2026
Works with
Security analysis
100/100npx -y skills add tools-only/X-Skills --skill 2644-exponential-backoff_0eea2e94 --agent claude-codeAre you the author of 2644 Exponential Backoff 0eea2e94?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/tools-only-2644-exponential-backoff-0eea2e94)# Exponential Backoff
## Exponential Backoff
```python
import time
import random
from functools import wraps
from typing import TypeVar, Callable
T = TypeVar('T')
def exponential_backoff(
max_retries: int = 5,
base_delay: float = 1.0,
max_delay: float = 60.0,
exponential_base: float = 2.0,
jitter: bool = True
) -> Callable:
"""Decorator for exponential backoff on rate limits."""
def decorator(func: Callable[..., T]) -> Callable[..., T]:
@wraps(func)
def wrapper(*args, **kwargs) -> T:
last_exception = None
for attempt in range(max_retries):
try:
return func(*args, **kwargs)
except Exception as e:
if "429" not in str(e) and "rate" not in str(e).lower():
raise # Not a rate limit error
last_exception = e
if attempt == max_retries - 1:
raise
# Calculate delay
delay = min(base_delay * (exponential_base ** attempt), max_delay)
# Add jitter to prevent thundering herd
if jitter:
delay = delay * (0.5 + random.random())
print(f"Rate limited. Attempt {attempt + 1}/{max_retries}. "
f"Waiting {delay:.2f}s...")
time.sleep(delay)
raise last_exception
return wrapper
return decorator
# Usage
@exponential_backoff(max_retries=5, base_delay=2.0)
def generate_video(prompt: str):
# API call here
pass
```Attribution
Comments
Loading comments…