Skip to content

DUX-8223: Map Bedrock 'too many tokens, please wait' 429 to RateLimitError#17

Merged
samuelboland merged 1 commit into
mainfrom
DUX-8223-bedrock-throttling-context-error-fix-ruby-llm
Jul 14, 2026
Merged

DUX-8223: Map Bedrock 'too many tokens, please wait' 429 to RateLimitError#17
samuelboland merged 1 commit into
mainfrom
DUX-8223-bedrock-throttling-context-error-fix-ruby-llm

Conversation

@staging-supernova-dx-appf-io

Copy link
Copy Markdown

Problem

Bedrock's ThrottlingException (HTTP 429) with the message "Too many tokens, please wait before trying again." was being misclassified as ContextLengthExceededError instead of RateLimitError. This caused incorrect error handling and retry logic in production.

Root Cause

The rate_limited? pattern matching in error_middleware.rb did not include the specific Bedrock throttling message pattern. When the 429 response didn't match rate limit patterns (/rate limit/, /per minute/, /per hour/, /per day/), the error fell through to context_length_exceeded?, which includes /too many tokens/i in its patterns, causing the misclassification.

Solution

Added the pattern /too many tokens.*please wait/i to the RATE_LIMITED_PATTERNS array to correctly identify Bedrock's token-rate throttling as a retryable rate limit error, not a context overflow error.

Changes

  • lib/ruby_llm/error_middleware.rb: Added Bedrock throttling pattern to RATE_LIMITED_PATTERNS
  • spec/ruby_llm/error_middleware_spec.rb: Added test case to verify the 429 response with Bedrock's specific message is correctly mapped to RateLimitError

Testing

Added test case: "maps Bedrock's 'too many tokens, please wait' 429 throttle to RateLimitError, not context length"


Agent session: https://staging.supernova.dx.appf.io/coders/073f6ce3-0d65-47e6-9df0-695acbc8a36c

Bedrock returns a 429 with "Too many tokens, please wait before trying
again" for token-rate throttling. The message matches the generic
"too many tokens" context-length pattern, so the 429 branch was
misclassifying it as ContextLengthExceededError instead of a retryable
RateLimitError. Add a rate-limit pattern that matches the throttle
phrasing so it is caught before the context-length check.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

Co-authored-by: Sam Boland <sam.boland@appfolio.com>
@staging-supernova-dx-appf-io staging-supernova-dx-appf-io Bot marked this pull request as ready for review July 14, 2026 18:40
@staging-supernova-dx-appf-io staging-supernova-dx-appf-io Bot requested a review from a team as a code owner July 14, 2026 18:40
@kermitapp

kermitapp Bot commented Jul 14, 2026

Copy link
Copy Markdown

@staging-supernova-dx-appf-io

Copy link
Copy Markdown
Author

🤖 Supernova Code Review — View trace

@staging-supernova-dx-appf-io staging-supernova-dx-appf-io Bot left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed the two changed files and found no actionable issues.

lib/ruby_llm/error_middleware.rb — The fix is correct. Adding /too many tokens.*please wait/i to RATE_LIMIT_PATTERNS ensures the Bedrock throttle message is matched by rate_limited? before the 429-handler falls through to context_length_exceeded?, which would have caught it via the broader /too many tokens/i pattern. The .*please wait qualifier makes the pattern specific enough to avoid misclassifying genuine context-length errors.

spec/ruby_llm/error_middleware_spec.rb — The new test case directly exercises the bug that was fixed and clearly documents the intended disambiguation. The existing test at line 87–95 ("still maps a 400 'too many tokens' error to ContextLengthExceededError") also confirms the 400-path is unaffected, which is good coverage.

No bugs, logic errors, or other concerns found.


↻ Re-run this review

@samuelboland samuelboland merged commit f78cac3 into main Jul 14, 2026
21 of 22 checks passed
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.

2 participants