codex: fast-fail 429s against an exhausted quota window (optional state-file integration) - #121
Open
dmitryanchikov wants to merge 1 commit into
Open
Conversation
…te-file integration) When a rate-limit window is exhausted, every retry inside the window is guaranteed to fail — but a 429 walked the transient-throttle backoff ladder anyway (measured: 13s buffered, ~165s live-stream per request, multiplied by the Anthropic client's own retries), surfacing only a generic 'Rate limited' minutes later. New optional env CCP_CODEX_QUOTA_STATE_FILE points at an externally sampled quota-state file (pct_5h|resets_5h_iso|pct_7d|resets_7d_iso|epoch). On an upstream 429, a coherent fresh sample with a window >=99% short-circuits both retry paths and returns 429 with Retry-After and a message naming the window and reset time. Every uncertainty is a no-verdict (normal ladder runs): env unset, file missing/malformed, windows cold, sample STALE (>900s), sample FUTURE-DATED (clock skew / corrupt line is not a fresher reading), or an already-EXPIRED reset time (internally incoherent — emitting Retry-After from it would tell clients to hammer a wall that may not exist). Unit tests cover hot-5h, weekly-precedence, cold, stale, future-dated, expired-reset, malformed/ missing, and empty-axis.
dmitryanchikov
force-pushed
the
feat/codex-quota-wall-fast-fail
branch
from
August 26, 2026 04:57
aabf661 to
8378e13
Compare
Author
|
Force-pushed a v2 adding two fail-open guards after internal review:
Unit tests now cover 8 cases: hot-5h, weekly-precedence, cold, stale, future-dated, expired-reset, malformed/missing, empty-axis. |
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
When a rate-limit window is exhausted, every retry inside the window is guaranteed to
fail — but an upstream 429 walks the transient-throttle backoff ladder anyway. Measured on
main @ 0185409 at a real 5h-window wall (2026-08-25): 13s per buffered request, ~165s
per live-stream request, multiplied by the Anthropic client's own retries — the user sees a
multi-minute spinner and finally a generic "Rate limited" with no reset information.
Change (opt-in, fail-open)
New optional env
CCP_CODEX_QUOTA_STATE_FILEpointing at an externally sampled quota-statefile:
pct_5h|resets_5h_iso|pct_7d|resets_7d_iso|epoch_secs(percents are USED-side). Wesample
chatgpt.com/backend-api/wham/usageon a cron; anything that produces this lineworks.
On an upstream 429: if the sample is fresh (≤900s) and either window is ≥99%, both retry
paths short-circuit and return an immediate Anthropic-shaped 429 with a
Retry-Afterheader (seconds to reset) and an explicit message:
Any uncertainty — env unset (the default), file missing/stale/malformed, windows cold —
returns no verdict and the existing ladder runs unchanged.
Notes
empty-axis case; full
cargo testgreen.codex.rate_limitsSSEsnapshots the proxy already receives mid-stream and consult the cache on 429. Happy to
rework toward that (or any preferred config surface) if you'd take it — the file variant
is what we could verify against a real wall.