Skip to content

retry

with_retries(fn, *, attempts=3, backoff_sec=0.5)

Execute a function with retries and exponential backoff.

Source code in wintermute/ai/retry.py
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
def with_retries(
    fn: Callable[[], T], *, attempts: int = 3, backoff_sec: float = 0.5
) -> T:
    """Execute a function with retries and exponential backoff."""
    last_exc: Exception | None = None
    for i in range(attempts):
        try:
            return fn()
        except Exception as exc:  # noqa: BLE001 - surface provider exceptions verbatim
            last_exc = exc
            if i == attempts - 1:
                raise
            time.sleep(backoff_sec * (2**i))
    # mypy: control never reaches here
    assert last_exc is not None
    raise last_exc