Repository navigation
Description
Description
The error message "tokens in request more than max tokens allowed" (returned by some OpenAI-compatible providers) is not classified as a context overflow error. It does not match any existing pattern in OVERFLOW_PATTERNS in packages/opencode/src/provider/error.ts, so it falls through as a generic API call error.
Observed Behavior
When a session exceeds the model's context window and the provider returns this error:
- The error is classified as a generic
APICallError(notContextOverflowError) - Auto-compaction is not triggered — the processor receives a non-overflow error
- The session enters a retry loop (continuation doom loop): retry → same overflow error → retry → ...
- No compaction occurs, so the session cannot recover
- The user sees repeated failures with no recovery path
Expected Behavior
This error should be classified as context_overflow, which would:
- Trigger auto-compaction to reduce context size
- Allow the session to recover and continue
- Avoid the infinite retry loop
Error Text
tokens in request more than max tokens allowed
This exact text is returned by some OpenAI-compatible providers when the total token count (input + requested output) exceeds the model's context window.
Affected Providers
Any OpenAI-compatible provider that returns this error phrasing instead of the more common maximum context length is X tokens or context_length_exceeded patterns. This includes some self-hosted and third-party proxy implementations.
Root Cause
In packages/opencode/src/provider/error.ts, the OVERFLOW_PATTERNS array (line 8) contains 19 regex patterns for detecting context overflow from various providers. However, there is no pattern matching the exact text "tokens in request more than max tokens allowed", so isOverflow() returns false and the error is not treated as context overflow.
Proposed Fix
Add the following pattern to the OVERFLOW_PATTERNS array in packages/opencode/src/provider/error.ts:
/tokens in request more than max tokens allowed/i,This follows the established convention of adding a provider- or message-specific pattern to catch an unrecognized overflow error variant.
Related Issues
- Sub-agents hang indefinitely on context overflow — no compaction triggered #25187 — Sub-agents hang indefinitely on context overflow — no compaction triggered (general class of this bug)
- GLM / Vertex overflow errors with INVALID_ARGUMENT are not classified as context overflow #23713 — GLM / Vertex overflow errors with INVALID_ARGUMENT are not classified as context overflow (same class of bug, different provider)
- sglang token exedeeded count is not being recognized for auto compact #25231 — sglang token exedeeded count is not being recognized for auto compact (same class, different provider)
- Auto compaction is not triggered by code: context_length_exceeded in API call errors #17746 — Auto compaction not triggered by code: context_length_exceeded in API call errors (similar fix, already merged)
- Context overflow from vLLM is not handled -> no auto compaction #17762 — Context overflow from vLLM is not handled (similar fix, already merged)
- Bug:
/too many tokens/ioverflow regex misclassifies rate limit errors as context overflow #13015 —/too many tokens/ioverflow regex misclassifies rate limit errors as context overflow (historical context)
Environment
- OpenCode version: latest dev
- Provider: OpenAI-compatible provider returning this error text
- OS: OS-agnostic
Activity