Skip to content

fix(provider): retry attempts that outlive the backoff budget - #114

Merged
lg2m merged 1 commit into
mainfrom
fix/provider-retry-header-timeout
Sep 22, 2026
Merged

lg2m merged 1 commit into
mainfrom
fix/provider-retry-header-timeout

Conversation

@lg2m

@lg2m lg2m commented Sep 22, 2026

Copy link
Copy Markdown
Collaborator

Problem

A run on litellm/us.anthropic.claude-fable-5-1 (124 model turns, ~305k tokens of context) failed with:

provider request failed: timed out waiting for response headers

exactly 300 s after its last tool call, on attempt 1, with no retry. All six provider_transport failures in the reporting session store share this signature (the four older ones rendered as error sending request before #106).

Two things combined:

  1. reqwest arms read_timeout when the request is sent and resets it only on a body read, so READ_TIMEOUT (300 s) also bounds the wait for response headers. A gateway that accepts a request and never answers fails after five minutes.
  2. AttemptLedger measured its 30 s budget as wall time since the first send. After a 300 s stall, elapsed was ten times the budget, so next_delay returned None and the exchange gave up. Any attempt that failed after >30 s was therefore unretryable — precisely the failure mode retries exist for.

Change

  • AttemptLedger charges only the backoff it grants (slept) to the budget, not attempt duration. Each attempt is already bounded by the client's connect (30 s), header (300 s), and read (300 s) deadlines, so the worst case stays bounded: at most four attempts plus ≤30 s of backoff.
  • The header-timeout message names its deadline like the connect and body variants: timed out waiting for response headers (none within 300s).
  • docs/design/providers.md documents that the budget bounds backoff sleeps, and that READ_TIMEOUT is also the header deadline.
  • exchange.rs: next_delay now takes &mut self (guard becomes mut).

No wire, schema, or configuration change. Default policy values are unchanged.

Verification

  • exchange_retries_a_header_timeout_longer_than_the_budget — loopback server stalls the first connection past the budget, then serves 200. Fails on the previous ledger (verified by temporarily restoring the wall-clock check), passes now; stable over 5 reruns.
  • ledger_charges_only_granted_backoff_to_the_budget — budget arithmetic with deterministic jitter.
  • stalled_headers_transport_error_names_the_deadline — rendered message.
  • cargo fmt --all -- --check ✅
  • cargo clippy --workspace --all-targets --all-features -- -D warnings ✅
  • cargo test --workspace ✅ (one unrelated flaky observation: headless::tests::turn_budget_cancels_before_a_silent_over_budget_turn_can_hang failed once under full-workspace load on a 1 s wall-clock bound, passed 3/3 in isolation and in the other full run)
  • cargo test -p qq-provider --no-default-features --features test-support ✅ (166 + 17)

Performance impact

None on the hot path: the ledger is touched only on failure. The successful-first-attempt path is unchanged.

Tracking

Linear issue not yet linked — the Linear connection needed reauthorization when this was opened. Will link once available.

The attempt ledger measured its 30 s budget from the first send, so any
attempt that failed after more than 30 s could never be retried. The
header deadline is 300 s (reqwest arms read_timeout at send and resets it
only on a body read), so a gateway that accepted a request and never
answered failed after five minutes with "timed out waiting for response
headers" on attempt 1 and was never resent, losing the whole run.

Charge only granted backoff sleeps to the budget. Each attempt is already
bounded by its own connect/header/read deadlines. Name the deadline in the
header-timeout message like the connect and body variants do.

Regression: exchange_retries_a_header_timeout_longer_than_the_budget fails
on the previous ledger and passes now; ledger_charges_only_granted_backoff_
to_the_budget and stalled_headers_transport_error_names_the_deadline cover
the ledger arithmetic and the rendered message.
@lg2m
lg2m merged commit c02b55f into main Sep 22, 2026
4 checks passed
@lg2m
lg2m deleted the fix/provider-retry-header-timeout branch September 22, 2026 02:10
lg2m added a commit that referenced this pull request Sep 22, 2026
… (RR5) (#118)

Two gaps in the pre-stream retry policy ended real runs that a short wait
would have saved. Anthropic answers overloaded_error with HTTP 529, which
is not one of http's named statuses and so was never in
is_retryable_status: five audited runs "gave up after 8 attempts" on it.
And a Retry-After above max_delay was clamped to 8 s, so the retry went
out before the server said it would accept one and earned the same
rejection again.

529 is retryable. Retry-After is a floor: parsed in delta-seconds and
HTTP-date form (httpdate, already in the lock through hyper), never
jittered down, and slept in full above the exponential cap up to 60 s. The
ledger charges a server-directed wait at the exponential rate, so honouring
the server cannot by itself exhaust the 30 s budget that bounds QQ's own
guessing. Compatible with #114, which stopped charging attempt wall time.

Tests: HTTP-date past and future, 529, the 60 s cap, ledger charging; the
Retry-After exchange test no longer relies on max_delay clamping. Both
provider profiles green.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant