Calls to a model API or any HTTP service fail transiently: rate limits, overloaded servers, dropped connections. Write the async retry wrapper that every client needs, with the transport and the sleep function injected so it can be tested without a network or a clock.
Implement async def fetch_with_retry(send, request, sleep, max_retries=3, base_delay=0.5, max_delay=8.0).
send(request) is an async callable that returns (status, headers, body) or raises ConnectionError or TimeoutError. Any other exception propagates.sleep(seconds) is an async callable. Use it for every wait (never asyncio.sleep or time.sleep).1 + max_retries attempts are made.Retry-After header (name case-insensitive) whose value parses as a non-negative number of seconds, wait instead.Return a dict: status (last status, or None if the last attempt raised), body (last body or None), attempts (number of send calls), delays (list of waits, in order) and error (None, or the exception class name, such as "TimeoutError", if the last attempt raised).