Bug: /too many tokens/i overflow regex misclassifies rate limit errors as context overflow #9003

Closed
opened 2026-02-16 18:11:22 -05:00 by yindo · 2 comments
Owner

Originally created by @mrm007 on GitHub (Feb 10, 2026).

Originally assigned to: @rekram1-node on GitHub.

Problem

The regex /too many tokens/i in OVERFLOW_PATTERNS (packages/opencode/src/provider/error.ts, line 22) is too broad. It matches rate limit / daily quota errors that contain "too many tokens" but are not context overflow errors.

Example: AWS Bedrock returns this when the daily token quota is exhausted:

Too many tokens per day, please wait before trying again.

This matches /too many tokens/i → classified as ContextOverflowError → triggers compaction → compaction also fails (same quota error) → user is stuck. The error never reaches the retry loop or plugin event hooks (session.status with type: "retry").

Cerebras has the same issue: "Tokens per minute limit exceeded - too many tokens processed" (see #7463).

Expected Behavior

Daily/minute token quota errors should be classified as retryable API errors (or at least non-overflow errors), so that:

  1. The retry loop can handle them with backoff
  2. Plugins listening for session.status retry events can trigger fallback logic
  3. Compaction is NOT triggered (context size isn't the problem)

Root Cause

The classification chain:

  1. ProviderError.parseAPICallError() calls isOverflow(message)
  2. isOverflow() checks OVERFLOW_PATTERNS/too many tokens/i matches
  3. Returns { type: "context_overflow" } → becomes ContextOverflowError
  4. SessionRetry.retryable() line 63: ContextOverflowError.isInstance(error) → returns undefined (not retryable)
  5. SessionProcessor.process() sets error on message, emits session.error, never emits session.status with type: "retry"
  6. Processor returns "compact" → compaction triggered on a quota error

Suggested Fix

Make the regex exclude temporal qualifiers:

// Before
/too many tokens/i, // Generic fallback

// After — exclude "per day", "per minute", "per hour", "processed"
/too many tokens(?! per (day|minute|hour|second))(?! processed)/i,

Or add a pre-check for rate limit patterns before checking overflow patterns, since rate limits should take priority.

Affected Error Messages

Error Message Source Should Be
"Too many tokens per day, please wait before trying again." AWS Bedrock Rate limit (retryable)
"Tokens per minute limit exceeded - too many tokens processed" Cerebras Rate limit (retryable)
"prompt is too long: 285744 tokens > 200000 maximum" Anthropic/Bedrock Context overflow (correct)

Environment

  • OpenCode version: 1.1.53+
  • Provider: amazon-bedrock (Claude Opus 4.6)
  • OS: macOS

Related Issues

  • #12507 — Beta header not sent for Opus 4.6 (separate issue causing 200K limit)
  • #12688 — Prompt too long on Bedrock Opus 4.6
  • #11267 — Feature request: 1M token support on Bedrock
  • #7463 — Cerebras "too many tokens processed" (also affected by this regex)
  • #7602 — Native model fallback support
  • #3525 — Quota limit exceeded error handling (partially fixed, this is a regression path)
Originally created by @mrm007 on GitHub (Feb 10, 2026). Originally assigned to: @rekram1-node on GitHub. ## Problem The regex `/too many tokens/i` in `OVERFLOW_PATTERNS` (`packages/opencode/src/provider/error.ts`, line 22) is too broad. It matches rate limit / daily quota errors that contain "too many tokens" but are **not** context overflow errors. **Example:** AWS Bedrock returns this when the daily token quota is exhausted: ``` Too many tokens per day, please wait before trying again. ``` This matches `/too many tokens/i` → classified as `ContextOverflowError` → triggers compaction → compaction also fails (same quota error) → user is stuck. The error never reaches the retry loop or plugin event hooks (`session.status` with `type: "retry"`). Cerebras has the same issue: "Tokens per minute limit exceeded - too many tokens processed" (see #7463). ## Expected Behavior Daily/minute token quota errors should be classified as retryable API errors (or at least non-overflow errors), so that: 1. The retry loop can handle them with backoff 2. Plugins listening for `session.status` `retry` events can trigger fallback logic 3. Compaction is NOT triggered (context size isn't the problem) ## Root Cause The classification chain: 1. `ProviderError.parseAPICallError()` calls `isOverflow(message)` 2. `isOverflow()` checks `OVERFLOW_PATTERNS` — `/too many tokens/i` matches 3. Returns `{ type: "context_overflow" }` → becomes `ContextOverflowError` 4. `SessionRetry.retryable()` line 63: `ContextOverflowError.isInstance(error)` → returns `undefined` (not retryable) 5. `SessionProcessor.process()` sets error on message, emits `session.error`, never emits `session.status` with `type: "retry"` 6. Processor returns `"compact"` → compaction triggered on a quota error ## Suggested Fix Make the regex exclude temporal qualifiers: ```typescript // Before /too many tokens/i, // Generic fallback // After — exclude "per day", "per minute", "per hour", "processed" /too many tokens(?! per (day|minute|hour|second))(?! processed)/i, ``` Or add a pre-check for rate limit patterns **before** checking overflow patterns, since rate limits should take priority. ## Affected Error Messages | Error Message | Source | Should Be | |---|---|---| | "Too many tokens per day, please wait before trying again." | AWS Bedrock | Rate limit (retryable) | | "Tokens per minute limit exceeded - too many tokens processed" | Cerebras | Rate limit (retryable) | | "prompt is too long: 285744 tokens > 200000 maximum" | Anthropic/Bedrock | Context overflow ✅ (correct) | ## Environment - OpenCode version: 1.1.53+ - Provider: amazon-bedrock (Claude Opus 4.6) - OS: macOS ## Related Issues - #12507 — Beta header not sent for Opus 4.6 (separate issue causing 200K limit) - #12688 — Prompt too long on Bedrock Opus 4.6 - #11267 — Feature request: 1M token support on Bedrock - #7463 — Cerebras "too many tokens processed" (also affected by this regex) - #7602 — Native model fallback support - #3525 — Quota limit exceeded error handling (partially fixed, this is a regression path)
yindo closed this issue 2026-02-16 18:11:22 -05:00
Author
Owner

@github-actions[bot] commented on GitHub (Feb 10, 2026):

This issue might be a duplicate of or closely related to existing issues. Please check:

  • #7463: Cerebras "Tokens per minute limit exceeded - too many tokens processed" (explicitly mentioned, same regex misclassification)
  • #3525: "Quota Limit Exceeded Error is not handled properly" (older quota handling issue)
  • #12507: "Beta header not sent for Claude Opus 4.6, causing 200k hard limit" (related AWS Bedrock context)
  • #12688: "Prompt too long when using Claude Opus 4.6 (Global) Amazon Bedrock" (related context)
  • #1712: "Implement exponential back-off when hitting rate limits" (retry handling this bug prevents)
  • #11705: "LLM provider SDK lacks retry logic for transient API errors" (related retry logic needed)
  • #7602: "Native Model Fallback / Failover Support" (mentioned in this issue for fallback logic)

The core fix needed here (distinguishing rate limit errors from context overflow) will likely help resolve or unblock several of these related issues.

@github-actions[bot] commented on GitHub (Feb 10, 2026): This issue might be a duplicate of or closely related to existing issues. Please check: - #7463: Cerebras "Tokens per minute limit exceeded - too many tokens processed" (explicitly mentioned, same regex misclassification) - #3525: "Quota Limit Exceeded Error is not handled properly" (older quota handling issue) - #12507: "Beta header not sent for Claude Opus 4.6, causing 200k hard limit" (related AWS Bedrock context) - #12688: "Prompt too long when using Claude Opus 4.6 (Global) Amazon Bedrock" (related context) - #1712: "Implement exponential back-off when hitting rate limits" (retry handling this bug prevents) - #11705: "LLM provider SDK lacks retry logic for transient API errors" (related retry logic needed) - #7602: "Native Model Fallback / Failover Support" (mentioned in this issue for fallback logic) The core fix needed here (distinguishing rate limit errors from context overflow) will likely help resolve or unblock several of these related issues.
Author
Owner

@rekram1-node commented on GitHub (Feb 10, 2026):

ill just drop the generic fallbacks then

@rekram1-node commented on GitHub (Feb 10, 2026): ill just drop the generic fallbacks then
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#9003