feat: retry transient provider failures with backoff#1
Open
tmchow wants to merge 1 commit into
Open
Conversation
429s and 5xx from the provider currently abort the Claude Code turn on the first failure. Free tiers (Groq, NIM) throttle often, so a single rate-limit response ends the session. ProviderClient.complete and .stream now retry 429/500/502/503/504 and transport errors up to PROVIDER_MAX_RETRIES (default 3) with exponential backoff, honoring the Retry-After header when present. Non-retryable errors (400/401/404/422) still raise immediately. Streaming only retries before the first chunk is yielded so a mid-stream failure never re-emits message_start. Set PROVIDER_MAX_RETRIES=0 to disable. No new dependencies.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
A single 429 or 5xx from the provider currently aborts the whole Claude Code turn, because
client.pyraises on the first non-200 response. This adds a bounded retry with exponential backoff inProviderClientso a throttled free-tier provider recovers instead of killing the session.Why this matters
The README points people at free tiers like Groq and NVIDIA NIM, and those tiers rate-limit hard. CONTRIBUTING.md lists "rate limit headers" as a wanted contribution, which is the gap this closes. LiteLLM and claude-code-router both retry transient failures already, so this brings Backdoor to parity, and it does so without adding a dependency.
Changes
Both
completeandstreamnow retry on 429 and 5xx responses and on transport errors, up toPROVIDER_MAX_RETRIES(default 3) with exponential backoff, honoring aRetry-Afterheader when the provider sends one. Errors that will not fix themselves on a retry, like a 404 for a bad model name, still raise immediately rather than sleeping through three attempts. Streaming is the subtle case: it only retries before the first chunk is yielded, so a mid-stream failure surfaces to the caller and the SSE stream is never restarted with a duplicatemessage_start. SettingPROVIDER_MAX_RETRIES=0restores the old fail-fast behavior.Testing
Added
tests/test_retry.pywith five cases on the existing pytest-asyncio setup: a 429 followed by a 200 retries and succeeds, a persistent 503 gives up after the retry budget and surfaces the error, a 404 raises with no retry, a zero retry budget makes a single attempt, andRetry-Afterparsing covers the delta-seconds and fallback paths. All pass, with no new dependencies beyondasyncioand stdlibemail.utils.