Thought for 4 s
The decorator retries the listed exceptions and doubles the ceiling of the wait on each attempt. Each sleep is a random value between zero and that ceiling, so many clients that fail together do not retry together.
python Copy code
import random, time
from functools import wraps
def retry(attempts=5, base=0.2, exceptions=(Exception,)):
def decorate(fn):
@wraps(fn)
def wrapper(*args, **kwargs):
for attempt in range(attempts):
try:
return fn(*args, **kwargs)
except exceptions:
if attempt == attempts - 1:
raise
time.sleep(random.uniform(0, base * 2 ** attempt))
return wrapper
return decorate The last attempt re-raises the original exception, so the caller sees the real error.
31.8 tok/s · 0.4 s to first token
