Raise `ModelQuotaExceededError` for Anthropic spend-limit errors
Closed
SH
1mo ago+96+96+01234567890123456789-4-4-0123456789
- Closes #12061
What broke
Anthropic's two spend-limit errors were misclassified:
- Tier spend cap: a 429
rate_limit_errorwhose body haserror.details.error_code == 'enforced_spend_limit_reached'becameModelRateLimitError. Anthropic's rate-limit docs say this response has noretry-afterand keeps failing until access resumes, soRetryOnModelErrorsand fallback-after-retry logic wasted requests waiting on it. - User/workspace spend limit: a 400
invalid_request_errorwith "You have reached your specified (workspace) API usage limits..." becameModelRequestRejectedError.
Root cause
models/anthropic/_errors.py::api_error read only error.type and error.message, never error.details, and only matched "credit balance is too low" among quota messages.
Fix
In the Anthropic error mapper only:
details.error_code == 'enforced_spend_limit_reached'->ModelQuotaExceededError(also applies to an in-streamerrorevent, which goes through the same function), and the code is surfaced asprovider_error_code.- An
invalid_request_errorwhose message starts with "You have reached your specified" and mentions "API usage limits" ->ModelQuotaExceededError, next to the existing credit-balance check.
No other provider or error path changes.
Tests
Added four cases to tests/models/test_error_categories.py: tier cap 429, user limit 400, workspace limit 400, and the tier cap as a stream error event.
- On
main: 4 failed (ModelRateLimitError/ModelRequestRejectedErrorinstead ofModelQuotaExceededError). - With the fix:
tests/models/test_error_categories.py+tests/models/test_anthropic_batch.py241 passed;tests/models/test_anthropic.py441 passed. ruff format --check,ruff check, andpyrightclean on both changed files.
Checklist
- No breaking changes in accordance with the version policy.
- Any permitted compatibility impact has a label, warning, migration and release note, and an exact API-check waiver if the gate reports it.
- PR title is fit for the release changelog.
🤖 Generated with Claude Code
Summary by cubic
Classifies Anthropic tier, user, and workspace spend-limit errors as ModelQuotaExceededError instead of rate-limit or request-rejection errors, so retry and fallback logic won’t waste requests on exhausted limits.
- Exposes Anthropic’s tier spend-cap error code as
provider_error_code. - Covers HTTP responses and streamed error events.