fix(gateway): mid-tier failover for 402 spending-limit - #740
Merged
chenyme merged 3 commits intoJul 22, 2026
Merged
Conversation
Stop passthrough of upstream "add credits / SuperGrok" 402 bodies to clients (Grok CLI). Always rotate accounts on payment-required, pause exhausted free/unrecognized profiles for freeQuotaRecoveryPause (2h), and map quota exhaustion to a sanitized 503 for the client. - ignore X-Should-Retry:false for 402 so multi-account failover runs - MarkPaidQuotaExhausted falls back to free recovery when billing is unknown - freeQuotaRecoveryPause defaults to 2h (was hardcoded 24h for free) - never surface upgrade/billing prompts through inference transport
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
Makes Grok2API behave like a mid-tier relay for Grok Build 402
personal-team-blocked:spending-limitfailures:X-Should-Retry: falsefor payment-required so multi-account failover actually runs.freeQuotaRecoveryPause(2h, was hardcoded 24h for free recovery). Paid profiles with a known period end still wait until period end. Failed probes re-apply the pause.upstream_unavailableso Grok CLI / OpenAI-compatible clients do not pop "Add credits / SuperGrok" dialogs.Motivation
In production we observed:
402+ bodypersonal-team-blocked:spending-limit/ "You have run out of credits..."X-Should-Retry: falseas final → attempt_count=1 and raw 402 body passthroughMarkPaidQuotaExhaustedreturnedfalsewhen billing plan fields were empty → accounts only got short cooldown and re-entered the poolBehavior after this change
routing.maxAttempts)quota_recoveryfree exhausted,next_probe_at = now+2hquota_recoverypaid exhausted until period endTest plan
TestPaymentRequiredAlwaysRetriesDespiteUpstreamVeto— 402 +X-Should-Retry: falseremains retryableupstream_request_failedwith differentaccount_idon 402account_quota_recoverywith ~2hnext_probe_atgo test ./backend/internal/application/gateway/ -count=1Notes / non-goals
freeQuotaRecoveryPause = 2his a product choice for mid-tier relay freshness; happy to make it arouting/ runtime setting in a follow-up if maintainers prefer.