Prefer RateLimitError on 429 with rate-limit body#775
Open
juanmanuelramallo wants to merge 2 commits into
Open
Prefer RateLimitError on 429 with rate-limit body#775juanmanuelramallo wants to merge 2 commits into
juanmanuelramallo wants to merge 2 commits into
Conversation
Some providers include token-budget context in their HTTP 429
rate-limit bodies. Anthropic, for example, returns messages like
"...rate limit of 5,000,000 input tokens per minute...". The
"input tokens" substring matches the existing /input[_\\s-]?token/i
entry in CONTEXT_LENGTH_PATTERNS, so the current 429 branch classifies
these as ContextLengthExceededError — making it impossible for callers
to tell a token-quota rate-limit (retriable with backoff) apart from a
true context-window overflow (non-retriable).
On 429, prefer RateLimitError whenever the body carries an explicit
rate-limit signal ("rate limit"), and only fall back to
ContextLengthExceededError when the message is unambiguously
context-length-shaped. A single regex is sufficient — the Anthropic
body and other documented rate-limit responses use the literal phrase
"rate limit".
Preserves the existing "429 + Request too large for model" →
ContextLengthExceededError behavior (that phrase has no rate-limit
signal). All other status-code branches and the 400 branch are
unchanged.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #775 +/- ##
=======================================
Coverage 87.21% 87.22%
=======================================
Files 121 121
Lines 5703 5707 +4
Branches 1442 1443 +1
=======================================
+ Hits 4974 4978 +4
Misses 729 729 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Author
|
@crmne does it make sense to merge this? |
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.
What this does
Some providers include token-budget context in their HTTP 429 rate-limit bodies. Anthropic, for example, returns messages like "...rate limit of 5,000,000 input tokens per minute...". The "input tokens" substring matches the existing
/input[_\s-]?token/ientry inCONTEXT_LENGTH_PATTERNS, so the current 429 branch classifies these asContextLengthExceededError— making it impossible for callers to tell a token-quota rate-limit (retriable with backoff) apart from a true context-window overflow (non-retriable).On 429, prefer
RateLimitErrorwhenever the body carries an explicit rate-limit signal ("rate limit"), and only fall back toContextLengthExceededErrorwhen the message is unambiguously context-length-shaped. A single regex is sufficient — the Anthropic body and other documented rate-limit responses use the literal phrase "rate limit".Preserves the existing "429 + Request too large for model" →
ContextLengthExceededErrorbehavior (that phrase has no rate-limit signal). All other status-code branches and the 400 branch are unchanged.Type of change
Scope check
Required for new features
PRs for new features or enhancements without a prior approved issue will be closed.
Quality check
overcommit --installand all hooks passbundle exec rake vcr:record[provider_name]bundle exec rspecmodels.json,aliases.json)AI-generated code
API changes