Skip to content

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

@mrm007

Description

@mrm007

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions