Skip to main content

retry_delay_from_headers

Function retry_delay_from_headers 

Source
pub fn retry_delay_from_headers(
    headers: &HeaderMap,
    attempt: u32,
) -> (Duration, bool)
Expand description

Compute how long to wait before the next retry, consulting response headers.

Priority order:

  1. retry-after (integer seconds, or an HTTP-date — integer is what Anthropic sends in practice, HTTP-date is handled as a best-effort fallback).
  2. anthropic-ratelimit-tokens-reset / anthropic-ratelimit-requests-reset (RFC 3339 UTC timestamp) — take the minimum of whichever is present.
  3. Classic exponential back-off: 1s * 2^(attempt-1) (1 s, 2 s, 4 s …).

The returned duration is capped at RETRY_DELAY_CAP so a far-future reset timestamp never hangs the turn forever. attempt is 1-based (first retry = 1).

Returns (delay, from_header) — the bool is true when a header was used (callers can emit a more informative notice message).