Raise `ModelQuotaExceededError` for Anthropic spend-limit errors

Closed
SH
1mo ago
+96-4

What broke

Anthropic's two spend-limit errors were misclassified:

  • Tier spend cap: a 429 rate_limit_error whose body has error.details.error_code == 'enforced_spend_limit_reached' became ModelRateLimitError. Anthropic's rate-limit docs say this response has no retry-after and keeps failing until access resumes, so RetryOnModelErrors and fallback-after-retry logic wasted requests waiting on it.
  • User/workspace spend limit: a 400 invalid_request_error with "You have reached your specified (workspace) API usage limits..." became ModelRequestRejectedError.

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-stream error event, which goes through the same function), and the code is surfaced as provider_error_code.
  • An invalid_request_error whose 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 / ModelRequestRejectedError instead of ModelQuotaExceededError).
  • With the fix: tests/models/test_error_categories.py + tests/models/test_anthropic_batch.py 241 passed; tests/models/test_anthropic.py 441 passed.
  • ruff format --check, ruff check, and pyright clean 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.