Skip to content

fix: exclude billing errors from context overflow detection (#25371) - #25395

Closed
echoVic wants to merge 3 commits into
openclaw:mainfrom
echoVic:fix/billing-402-misclassified-as-overflow
Closed

echoVic wants to merge 3 commits into
openclaw:mainfrom
echoVic:fix/billing-402-misclassified-as-overflow

Conversation

@echoVic

@echoVic echoVic commented Feb 24, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Problem: OpenRouter 402 billing errors ("requires more credits, or fewer max_tokens. You requested up to 32000 tokens, but can only afford 11771") are misclassified as context overflow, triggering futile auto-compaction retries that drain remaining credits.
  • Root cause: The 402 error message contains token/limit language (max_tokens, tokens, limit) that matches CONTEXT_OVERFLOW_HINT_RE in isLikelyContextOverflowError(). Meanwhile, isBillingErrorMessage() fails to detect this error because its patterns only match "insufficient credits" / "payment required" — not OpenRouter's "requires more credits" / "can only afford" wording.
  • Fix:
    1. Add "more credits", "can only afford", "requires more credits" to billing error patterns
    2. Add isBillingErrorMessage() guard in isLikelyContextOverflowError() (mirrors existing isRateLimitErrorMessage() guard)
  • What did NOT change: isContextOverflowError() (strict patterns) does not match the 402 message, so no change needed there. formatAssistantErrorText() ordering is unaffected since the strict check runs first.

Change Type

  • Bug fix

Scope

  • Gateway / orchestration

Linked Issue/PR

User-visible / Behavior Changes

OpenRouter 402 billing errors now correctly show a billing error message instead of "Context overflow: prompt too large for the model". Auto-compaction retries are no longer triggered for billing errors, preventing unnecessary credit drain.

Security Impact

  • New permissions/capabilities? No
  • Secrets/tokens handling changed? No
  • New/changed network calls? No
  • Command/tool execution surface changed? No
  • Data access scope changed? No

Evidence

  • Failing test before + passing after
  • 4 new test cases: OpenRouter 402 billing detection, overflow exclusion, failover classification
  • All existing tests pass (38 billing tests, 11 overflow-compaction tests, 21 formatAssistantErrorText tests)

Compatibility / Migration

  • Backward compatible? Yes
  • Config/env changes? No
  • Migration needed? No

Failure Recovery

  • Revert this single commit
  • Known bad symptoms: billing errors showing as "Context overflow" again

Greptile Summary

This PR fixes a bug where OpenRouter 402 billing errors containing token/limit language were misclassified as context overflow errors, triggering wasteful auto-compaction retries that drained remaining credits.

Key changes:

  • Added three new billing error patterns to ERROR_PATTERNS.billing: "more credits", "can only afford", "requires more credits" (lines 653-655 in errors.ts)
  • Added isBillingErrorMessage() guard in isLikelyContextOverflowError() before the broad CONTEXT_OVERFLOW_HINT_RE check (lines 124-129), mirroring the existing rate limit guard pattern
  • Added comprehensive test coverage: 4 new test cases covering OpenRouter 402 detection, overflow exclusion, and failover classification

How it works:
The OpenRouter error "402 This request requires more credits, or fewer max_tokens. You requested up to 32000 tokens, but can only afford 11771" contains words like "max_tokens" and "tokens" that match the broad CONTEXT_OVERFLOW_HINT_RE pattern. The fix ensures billing errors are excluded early in the detection flow, preventing incorrect classification.

Impact:
Users will now see accurate billing error messages instead of confusing "Context overflow" messages, and the system won't waste credits on pointless retry attempts.

Confidence Score: 5/5

  • This PR is safe to merge with minimal risk
  • The fix is surgical and well-tested. It adds three targeted billing error patterns and a single guard clause that mirrors an existing pattern for rate limits. All 4 new tests pass, and existing tests remain unaffected. The implementation follows established conventions in the codebase and solves a real user-facing bug without introducing breaking changes.
  • No files require special attention

Last reviewed commit: 57845d4

@openclaw-barnacle

Copy link
Copy Markdown

This pull request has been automatically marked as stale due to inactivity.
Please add updates or it will be closed.

@openclaw-barnacle openclaw-barnacle Bot added the stale Marked as stale due to inactivity label Mar 3, 2026
…#25371)

OpenRouter 402 errors ('requires more credits, or fewer max_tokens')
contain token/limit language that matches CONTEXT_OVERFLOW_HINT_RE,
causing misclassification as context overflow. This triggers futile
auto-compaction retries that drain remaining credits.

Changes:
- Add 'more credits', 'can only afford', 'requires more credits' to
  billing error patterns so isBillingErrorMessage catches OpenRouter 402
- Add isBillingErrorMessage guard in isLikelyContextOverflowError
  (mirrors existing isRateLimitErrorMessage guard)
- Add tests for OpenRouter 402 billing detection, overflow exclusion,
  and failover classification
@echoVic

echoVic commented Mar 4, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto latest main and resolved merge conflicts; force-pushed updates. CI re-running. If the only failure is the Windows shard timeout in src/plugin-sdk/root-alias.test.ts, it appears flaky across multiple PRs.

@echoVic

echoVic commented Mar 4, 2026

Copy link
Copy Markdown
Contributor Author

Follow-up: removed unused ERROR_PATTERNS table (was tripping oxlint) and increased plugin-sdk root-alias test timeout to reduce Windows shard flake. CI re-running.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling size: XS stale Marked as stale due to inactivity

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: OpenRouter 402 billing error misclassified as 'Context overflow', auto-compaction retries drain remaining credits

2 participants