Skip to content

fix(ai): classify per-window token throttles as retryable rate limit - #58796

Open
vitalyisaev2 wants to merge 1 commit into
anomalyco:devfrom
vitalyisaev2:tokens-per-minute-retry
Open

vitalyisaev2 wants to merge 1 commit into
anomalyco:devfrom
vitalyisaev2:tokens-per-minute-retry

Conversation

@vitalyisaev2

Copy link
Copy Markdown

Adds the phrase "too many tokens per minute/second/hour/day" to the context-overflow exclusions and the rate-limit text, so the provider throttle "Token limit reached: too many tokens per minute for this model" is treated as a retryable 429 rather than as a context overflow that compaction shrinks and then fails on. Mirrors the Bedrock per-day wording fix.

Fixes #58794

Issue for this PR

Closes #58794

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

DeepSeek-V4-Flash can throttle with the message Token limit reached: too many tokens per minute for this model. classifyProviderFailure() matches the plain /too many tokens/i pattern inside isContextOverflow, so this 429 was mistaken for a context overflow. The delivery loop would shrink the transcript via compaction and then fail — but shortening a transcript can't help a tokens-per-minute ceiling, so it should just be retried instead.

The change adds too many tokens per (minute|second|hour|day) to two places: the exclusions list so isContextOverflow no longer matches it, and RATE_LIMIT_TEXT so it's classified as RateLimit (retryable). Same approach as an earlier Bedrock per-day wording fix, just extended to the per-window phrasings. Bare "Too many tokens" still categorizes as overflow.

How did you verify your code works?

Added a regression test mirroring the existing Bedrock one: for ...too many tokens per minute... and ...per second... it asserts the failure classifies as RateLimit, isContextOverflow/isContextOverflowFailure are false, and isRetryable is true; bare "Too many tokens" still compacts. Verified with bun test test/provider-error.test.ts (41 pass), bun run typecheck:tsc in packages/ai, and the repo-wide bun run check (34/34 tasks pass).

Screenshots / recordings

N/A — logic-only change, no UI.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

Adds the phrase "too many tokens per minute/second/hour/day" to the
context-overflow exclusions and the rate-limit text, so the provider
throttle "Token limit reached: too many tokens per minute for this
model" is treated as a retryable 429 rather than as a context overflow
that compaction shrinks and then fails on. Mirrors the Bedrock per-day
wording fix.

Fixes anomalyco#58794

@opencode-agent opencode-agent Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. I ran the #58794 reproduction script in packages/ai on the base and with this change. On the base, the per-minute and per-second throttle messages classify as InvalidRequest (context overflow) and aren't retryable, with a 429 and with no status. With the change, both come back as a retryable RateLimit and aren't treated as overflow. bun test test/provider-error.test.ts (41 pass) and typecheck:tsc in packages/ai pass.

The new exclusion is what fixes the 429 path, because overflow is checked before the rate-limit branch. The RATE_LIMIT_TEXT addition also covers responses that arrive without a status. The new phrases don't match the 429 quota text, so these errors can't be classified as a quota error instead. A plain "Too many tokens" still counts as overflow.

Public API: unchanged. This is internal error classification in packages/ai.

No blocking concerns.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

compaction: 'too many tokens per minute' rate limit misclassified as context overflow

1 participant