Skip to content

fix(provider): treat Anthropic billing_error as context overflow for auto-compaction#17766

Closed
codeg-dev wants to merge 2 commits intoanomalyco:devfrom
codeg-dev:fix/anthropic-billing-error-overflow
Closed

fix(provider): treat Anthropic billing_error as context overflow for auto-compaction#17766
codeg-dev wants to merge 2 commits intoanomalyco:devfrom
codeg-dev:fix/anthropic-billing-error-overflow

Conversation

@codeg-dev
Copy link

Problem

Anthropic returns HTTP 403 with billing_error type and the message:

"Extra usage is required for long context requests."

This occurs when a session context exceeds ~200K tokens for accounts not in Anthropic usage tier 4.

Root Cause

The OVERFLOW_PATTERNS array in provider/error.ts did not include this error message. As a result:

  1. parseAPICallError() classifies it as a generic api_error (not context_overflow)
  2. retry.ts treats it as retryable (depending on HTTP status interpretation)
  3. No automatic compaction is triggered — the session keeps retrying with the same oversized context

Observed in production: 57 consecutive failures over 2 hours on a session with 452K tokens.

Fix

Add /extra usage is required for long context/i to OVERFLOW_PATTERNS.

This causes opencode to:

  • Classify the error as ContextOverflowError instead of APIError
  • Trigger automatic compaction to reduce the session context
  • Retry with a smaller context that stays under Anthropic's threshold

Testing

Manually verified on a session with 452K+ tokens hitting this error. After fix, compaction is triggered automatically instead of infinite retry.

…rror "Extra usage is required for long context requests." from Anthropic(HTTP 403, billing_error type) was not recognized by OVERFLOW_PATTERNS,causing it to be treated as a generic retryable API error.This resulted in infinite retry loops (57+ retries observed) instead oftriggering automatic compaction to reduce the context size.Adding the pattern causes opencode to automatically compact the sessionwhen this error occurs, which resolves the underlying context size issue.
@github-actions github-actions bot added needs:compliance This means the issue will auto-close after 2 hours. needs:issue labels Mar 16, 2026
@github-actions
Copy link
Contributor

This PR doesn't fully meet our contributing guidelines and PR template.

What needs to be fixed:

  • PR description is missing required template sections. Please use the PR template.

Please edit this PR description to address the above within 2 hours, or it will be automatically closed.

If you believe this was flagged incorrectly, please let a maintainer know.

@github-actions
Copy link
Contributor

Thanks for your contribution!

This PR doesn't have a linked issue. All PRs must reference an existing issue.

Please:

  1. Open an issue describing the bug/feature (if one doesn't exist)
  2. Add Fixes #<number> or Closes #<number> to this PR description

See CONTRIBUTING.md for details.

@github-actions
Copy link
Contributor

The following comment was made by an LLM, it may be inaccurate:

Results

No duplicate PRs found.

However, I found several related PRs that address context overflow and compaction, though they address different aspects:

These PRs are in the same feature area but target different error scenarios or approaches, so they're not duplicates of PR #17766.

@github-actions
Copy link
Contributor

This pull request has been automatically closed because it was not updated to meet our contributing guidelines within the 2-hour window.

Feel free to open a new pull request that follows our guidelines.

@github-actions github-actions bot removed the needs:compliance This means the issue will auto-close after 2 hours. label Mar 16, 2026
@github-actions github-actions bot closed this Mar 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant