AI-Assisted Coding Lab · Add Retries With Backoff

40:00left of 40:00

Add Retries With Backoff

The billing service calls a payment provider that times out under load, and every caller has its own retry loop. Build one shared helper with the assistant. The README has the rules. Accept, edit, or reject each suggestion, and keep the tests green.

backoff.py1 of 3

Files

backoff.py

Python · Editable
import functools
import random
import time
class RetryError(Exception):
"""Raised when retrying stops. The last error is chained as __cause__."""
def backoff_delay(retry_n, base_s, cap_s, rng):
"""Seconds to wait before retry number retry_n, counting from 0."""
raise NotImplementedError
def fits_deadline(start, now, delay, deadline_s):
"""True when a sleep of delay seconds starting at now ends by start + deadline_s."""
raise NotImplementedError
def retry(
fn,
attempts=3,
base_s=0.1,
cap_s=5.0,
retry_on=(ConnectionError, TimeoutError),
deadline_s=None,
sleep=time.sleep,
clock=time.monotonic,
rng=random.random,
):
"""Calls fn() and returns its result, retrying errors in retry_on with backoff."""
start = clock()
raise NotImplementedError
def retrying(**options):
"""The decorator form of retry. Takes the same keyword arguments."""
raise NotImplementedError

Test Output

No runs yet.

Assistant

Scripted
  1. Send the first prompt to start.

Next Prompt

Write backoff_delay: exponential backoff with full jitter, capped at cap_s.