[PR #13019] fix: prevent rate limit errors from being misclassified as context overflow #14476

Closed
opened 2026-02-16 18:19:16 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/anomalyco/opencode/pull/13019

State: closed
Merged: No


Summary

Fixes #13015

The /too many tokens/i regex in OVERFLOW_PATTERNS is too broad and matches rate limit / daily quota errors that contain "too many tokens" but are not context overflow errors.

Affected Error Messages

Error Message Source Before After
"Too many tokens per day, please wait..." AWS Bedrock context_overflow api_error (retryable)
"too many tokens processed" Cerebras (#7463) context_overflow api_error (retryable)
"prompt is too long: 285744 tokens > 200000 maximum" Anthropic context_overflow context_overflow (unchanged)

Root Cause

isOverflow() checks OVERFLOW_PATTERNS which includes /too many tokens/i. This matches rate limit errors → classified as ContextOverflowError → triggers compaction instead of retry → compaction also fails (same quota error) → user is stuck.

Changes

Two complementary fixes in packages/opencode/src/provider/error.ts:

  1. Narrowed the regex: Added negative lookahead to the "too many tokens" pattern so it excludes temporal qualifiers and "processed":

    /too many tokens(?! per (day|minute|hour|second))(?! processed)/i
    
  2. Added rate limit guard: New RATE_LIMIT_PATTERNS list and isRateLimit() function checked before overflow patterns in isOverflow(). This provides defense-in-depth — even if the regex lookahead misses a new rate limit phrasing, the explicit rate limit check catches it.

Test Plan

  • Verify "Too many tokens per day, please wait before trying again." → classified as retryable api_error
  • Verify "Tokens per minute limit exceeded - too many tokens processed" → classified as retryable api_error
  • Verify "prompt is too long: 285744 tokens > 200000 maximum" → still classified as context_overflow
  • Verify "maximum context length is 128000 tokens" → still classified as context_overflow

Assisted by Claude Opus 4.6 max thinking

Made with Cursor

**Original Pull Request:** https://github.com/anomalyco/opencode/pull/13019 **State:** closed **Merged:** No --- ## Summary Fixes #13015 The `/too many tokens/i` regex in `OVERFLOW_PATTERNS` is too broad and matches rate limit / daily quota errors that contain "too many tokens" but are **not** context overflow errors. ## Affected Error Messages | Error Message | Source | Before | After | |---|---|---|---| | "Too many tokens per day, please wait..." | AWS Bedrock | `context_overflow` | `api_error` (retryable) | | "too many tokens processed" | Cerebras (#7463) | `context_overflow` | `api_error` (retryable) | | "prompt is too long: 285744 tokens > 200000 maximum" | Anthropic | `context_overflow` | `context_overflow` (unchanged) | ## Root Cause `isOverflow()` checks `OVERFLOW_PATTERNS` which includes `/too many tokens/i`. This matches rate limit errors → classified as `ContextOverflowError` → triggers compaction instead of retry → compaction also fails (same quota error) → user is stuck. ## Changes Two complementary fixes in `packages/opencode/src/provider/error.ts`: 1. **Narrowed the regex**: Added negative lookahead to the "too many tokens" pattern so it excludes temporal qualifiers and "processed": ``` /too many tokens(?! per (day|minute|hour|second))(?! processed)/i ``` 2. **Added rate limit guard**: New `RATE_LIMIT_PATTERNS` list and `isRateLimit()` function checked **before** overflow patterns in `isOverflow()`. This provides defense-in-depth — even if the regex lookahead misses a new rate limit phrasing, the explicit rate limit check catches it. ## Test Plan - [ ] Verify "Too many tokens per day, please wait before trying again." → classified as retryable `api_error` - [ ] Verify "Tokens per minute limit exceeded - too many tokens processed" → classified as retryable `api_error` - [ ] Verify "prompt is too long: 285744 tokens > 200000 maximum" → still classified as `context_overflow` - [ ] Verify "maximum context length is 128000 tokens" → still classified as `context_overflow` *Assisted by Claude Opus 4.6 max thinking* Made with [Cursor](https://cursor.com)
yindo added the pull-request label 2026-02-16 18:19:16 -05:00
yindo closed this issue 2026-02-16 18:19:16 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#14476