Raise `ModelQuotaExceededError` for Anthropic's tier spend cap and user-set spend limits

Merged
DS
21d ago
+159-14

This pull request was posted by Claude Code using claude-opus-5-5 on behalf of David.

Closes #12061

Why we make these changes

Anthropic answers a reached spend limit in two ways, and AnthropicModel raised neither as a quota error. Retry and fallback logic then waited on a cap that lasts until the next month, or treated it as a bad request.

[!WARNING]
Compatibility impact: Existing code that catches ModelRateLimitError for Anthropic's tier spend cap, or ModelRequestRejectedError for a spend limit the user set, will no longer catch it: both are now a ModelQuotaExceededError, still a ModelHTTPError with the same status.

Why this can ship in a minor release: It fixes a misclassification, as #12042 (Z.ai) and #12422 (Gemini, Moonshot) did. except ModelHTTPError / except ModelAPIError and the default fallback_on are unaffected.

Migration: Catch ModelQuotaExceededError to handle these errors.

models/anthropic/_errors.py read only error.type and error.message; it now also reads error.details.error_code to classify. provider_error_code is unchanged. A ramp-limit 429 (ramp_limit_exceeded, with retry-after) stays a ModelRateLimitError. Stream error events and errored Message Batches results go through the same api_error, so they get the same classification, which anthropic-stream-spend-cap and the batch tests pin.

#12259 separately reads details.error_code in the same file for message threads; the two don't touch the same lines. Thanks to @sharziki for the earlier attempt in #12260.

New public surface

None.

User-visible behavior

Signal (Anthropic rate-limits docs)BeforeAfter
Tier spend cap: 429 rate_limit_error, error.details.error_code enforced_spend_limit_reached, no retry-afterModelRateLimitErrorModelQuotaExceededError
User-set org or workspace limit: 400 invalid_request_error, message starting You have reached your specified (workspace) API usage limitsModelRequestRejectedErrorModelQuotaExceededError

Verification

New cases in tests/models/test_error_categories.py built from the documented bodies: anthropic-spend-cap, anthropic-spend-limit and anthropic-workspace-spend-limit (each failed before the fix), the in-stream anthropic-stream-spend-cap, and the anthropic-ramp-limit control. In tests/models/test_anthropic_batch.py, a user-set spend-limit row and test_anthropic_batch_spend_cap_raises_quota_exceeded cover errored batch results.

What changes for existing users

Code that caught ModelRateLimitError for the tier spend cap, or ModelRequestRejectedError for a user-set spend limit, now gets ModelQuotaExceededError, which is still a ModelHTTPError that FallbackModel / ModelFallback fall back on by default.

PR decisions

2026-11-12 · Target category for both spend limits · iter -

  • Decision: Tier spend-cap 429 and user-set org/workspace spend-limit 400 both raise ModelQuotaExceededError
  • Why: DouweM's issue direction; same as OpenAI _QUOTA_CODES spend-limit codes and PR 12422 (Gemini/Moonshot)
  • Source: https://github.com/pydantic/pydantic-ai/issues/12061
  • Session: a9a64821-6de5-43ec-a7f6-3638aab22c68

2026-11-12 · No live recording; documented bodies as in-process Cases · iter -

  • Decision: Add Cases to tests/models/test_error_categories.py with the bodies from Anthropic's rate-limits docs, plus a ramp_limit_exceeded 429 control that stays ModelRateLimitError
  • Why: Tripping a spend limit needs a capped workspace; docs give exact bodies (saved in local-notes/readiness-12061/)
  • Source: https://platform.claude.com/docs/en/api/rate-limits#reaching-your-spend-cap
  • Session: a9a64821-6de5-43ec-a7f6-3638aab22c68

2026-11-12 · Do not set provider_error_code from details.error_code · iter -

  • Decision: Read details.error_code only to classify; provider_error_code stays None. Raise again only if a reviewer asks
  • Why: Not needed for the fix, and it would change attributes on ramp-limit errors too
  • Source: https://github.com/pydantic/pydantic-ai/issues/12061
  • Session: a9a64821-6de5-43ec-a7f6-3638aab22c68

2026-11-12 · Leave PR 12259's details reader alone · iter -

  • Decision: Keep this PR's details.error_code read minimal and inline; do not touch or comment on PR 12259's _thread_error_code; note the overlap in the PR body
  • Why: Open contributor PR reads the same field in the same file; avoid a conflicting shared helper
  • Source: https://github.com/pydantic/pydantic-ai/pull/12259
  • Session: a9a64821-6de5-43ec-a7f6-3638aab22c68

2026-11-12 · Claude Code workspace 429 stays a rate limit · iter -

2026-11-12 · Credit closed PR 12260 · iter -

  • Decision: Credit sharziki in the PR body; do not reopen or comment on PR 12260
  • Why: Auto-closed and never reviewed; predates error_category() and uses a non-doc body
  • Source: https://github.com/pydantic/pydantic-ai/pull/12260
  • Session: a9a64821-6de5-43ec-a7f6-3638aab22c68

2026-11-12 · Stream and batch classification: code trace, no extra test · iter -

  • Decision: Verified by code trace: error_category's only caller is api_error, which map_api_errors calls for HTTP errors and stream error events (in_stream) and _batch.batch_decode calls for errored batch results
  • Why: One shared path; no documented stream or batch spend-limit body to test against
  • Source: https://github.com/pydantic/pydantic-ai/issues/12061
  • Session: a9a64821-6de5-43ec-a7f6-3638aab22c68

2026-11-12 · pre-push review 1 of 3 at 632106760474bffb2b7c5f2b1975d09a3f47ad23 · iter -

2026-11-12 · pre-push review 1 of 3 at 632106760474bffb2b7c5f2b1975d09a3f47ad23 · iter -

2026-11-12 · Label compatibility impact with a WARNING section · iter -

  • Decision: Add the compatibility impact label and a [!WARNING] section, plus a 'What changes for existing users' section shaped like PR 12422
  • Why: Code catching ModelRateLimitError/ModelRequestRejectedError for these errors must change; agent_docs/api-design.md and docs/version-policy.md require label + warning; same-shape PR 12042 carried both
  • Source: https://github.com/pydantic/pydantic-ai/pull/12042
  • Session: a9a64821-6de5-43ec-a7f6-3638aab22c68

2026-11-12 · pre-push review 2 of 3 at 82c141563c829b4b852105b4fe7a2ce81dc87491 · iter -

2026-11-12 · pre-push review 2 of 3 at 82c141563c829b4b852105b4fe7a2ce81dc87491 · iter -

2026-11-12 · pre-push review 3 of 3 at 0f62b27a272906566c8ccfccf2cf1e375697395a · iter -

2026-11-12 · pre-push review 3 of 3 at 0f62b27a272906566c8ccfccf2cf1e375697395a · iter -

2026-11-12 · douwebot label authorized for every head of this PR · iter -

  • Decision: Apply douwebot once CI and PR Review Checks pass on a head, without asking again on later heads; manager enqueues and re-enqueues, never this agent
  • Why: S merge bar needs a clean douwebot approval; manager approved on David's lane (local-notes/dispatch-20261112T164729Z.md)
  • Source: https://github.com/pydantic/pydantic-ai/pull/13269
  • Session: a9a64821-6de5-43ec-a7f6-3638aab22c68

2026-11-12 · douwebot transient-failure retry rule · iter -

  • Decision: When a douwebot run fails before a verdict for a transient pipeline cause (a provider 429, or a fallback model's output failing to parse), re-apply the label once after about 15 minutes, using a background wait rather than a foreground sleep. If the retry also fails, report to the manager and hold. Do not file or touch the douwebot result-parser bug; the manager routes it
  • Why: First douwebot run on 0f62b27a2 failed in douwebot-opencode (OpenCode Go 429, then glm-5.3-flash prose before JSON broke douwebot_opencode_result.py); manager approved a retry on David's lane (local-notes/dispatch-20261112T171313Z.md)
  • Source: https://github.com/pydantic/pydantic-ai/actions/runs/43265435601
  • Session: a9a64821-6de5-43ec-a7f6-3638aab22c68

2026-11-12 · Hold douwebot until the manager's go · iter -

  • Decision: Hold in monitor mode; do not re-apply douwebot until the manager says go (sent when the parser-fix PR merges or OpenCode Go stops returning 429s)
  • Why: Both douwebot runs on 0f62b27a2 failed in douwebot's pipeline; a separate PR fixes the parser (local-notes/dispatch-20261112T172233Z.md)
  • Source: https://github.com/pydantic/pydantic-ai/actions/runs/43267136688
  • Session: a9a64821-6de5-43ec-a7f6-3638aab22c68
  • Supersedes: douwebot transient-failure retry rule

2026-11-12 · Poll at 120s for the rest of this PR · iter -

  • Decision: Every background watch (monitor mode, douwebot wait, CI wait) sleeps 120s between polls for the rest of this PR; no gh calls before 17:40Z on 2026-11-12
  • Why: Shared GitHub REST quota for the account exhausted until 17:40Z; polling is the main consumer (local-notes/dispatch-20261112T173258Z.md)
  • Source: https://github.com/pydantic/pydantic-ai/pull/13269
  • Session: a9a64821-6de5-43ec-a7f6-3638aab22c68

2026-11-12 · GitHub quota rule for CI diagnosis · iter -

  • Decision: Download each CI job log at most once to the scratchpad and read it from disk after; start CI diagnosis with one lookup via $PYDANTIC_AI_TOOLS_REPO/pydantic-ai-manager-config/.claude/skills/pr-orchestrator/helpers/failing-required-job , not repeated log pulls; poll every 120s
  • Why: The REST quota is shared by every dsfaccini gh call on every machine and hit 0 once today (local-notes/dispatch-20261112T174614Z.md)
  • Source: https://github.com/pydantic/pydantic-ai/pull/13269
  • Session: a9a64821-6de5-43ec-a7f6-3638aab22c68

2026-11-12 · Horizon-based waits replace the fixed 120s poll · iter -

  • Decision: Every wait uses the managed-pr-template horizon nap (a sixth of the time left, 60 s to 1 h, SIGNAL OVERDUE past H): monitor mode H=4h (1h while in the merge queue), douwebot wait H=15min; toolkit helpers take --horizon/--by, never --interval/--timeout. All holds stand: no douwebot until the manager's go
  • Why: Toolkit watchers changed in tooling 982e5be; manager relayed the new blocks (local-notes/dispatch-20261112T190918Z.md)
  • Source: https://github.com/pydantic/pydantic-ai/pull/13269
  • Session: a9a64821-6de5-43ec-a7f6-3638aab22c68
  • Supersedes: Poll at 120s for the rest of this PR

2026-11-12 · douwebot go after the parser fix merged · iter -

  • Decision: Applied douwebot on 0f62b27a2 at 2026-11-12T19:37:55Z and launched the block-2 douwebot wait; the one-retry rule for transient pipeline failures holds again (re-apply once after about 15 minutes via a background wait; if the retry also fails, report and hold)
  • Why: PR 13286 (douwebot parser) merged 2026-11-12T19:35:27Z as 1f631a3ea; manager sent the go (local-notes/dispatch-20261112T193723Z.md)
  • Source: https://github.com/pydantic/pydantic-ai/pull/13286
  • Session: a9a64821-6de5-43ec-a7f6-3638aab22c68
  • Supersedes: Hold douwebot until the manager's go

2026-11-12 · Pin the in-stream spend cap with a Case · iter -

  • Decision: Add anthropic-stream-spend-cap (SSE error event with details.error_code enforced_spend_limit_reached → ModelQuotaExceededError, status 429, in_stream True, retry_after None); batch stays covered by the code trace
  • Why: douwebot asked for the stream claim in the PR body to be pinned; one test-only Case mirroring anthropic-stream-rate-limit
  • Source: https://github.com/pydantic/pydantic-ai/pull/13269#discussion_r4555482070
  • Session: a9a64821-6de5-43ec-a7f6-3638aab22c68
  • Supersedes: Stream and batch classification: code trace, no extra test

2026-11-12 · Exempt delta from reviewed 0f62b27a2: stream spend-cap Case · iter -

2026-11-12 · Keep provider_error_code None after douwebot's optional finding · iter -

  • Decision: Keep provider_error_code None; replied citing the earlier decision, reacted -1, resolved the thread
  • Why: douwebot says no change is needed if None is intended; setting it would change attributes on ramp-limit errors, outside this fix (local-notes/dispatch-20261112T195059Z.md)
  • Source: https://github.com/pydantic/pydantic-ai/pull/13269#discussion_r4555482078
  • Session: a9a64821-6de5-43ec-a7f6-3638aab22c68

2026-11-12 · Rerun PR Review Checks once on d3be28230 · iter -

  • Decision: Reran the failed tests-and-dependencies job of PR Review Checks run 43293916874 once; if the rerun fails again, escalate to the manager. douwebot waits until PR Review Checks reads success
  • Why: Status 'No result: tests-and-dependencies': OpenCode Go 429, then the glm-5.3-flash fallback answer was rejected as json_invalid; no finding to triage; tracked skill says rerun once, then escalate
  • Source: https://github.com/pydantic/pydantic-ai/actions/runs/43293916874
  • Session: a9a64821-6de5-43ec-a7f6-3638aab22c68

2026-11-12 · Serial comma in the overview paragraph, pushed without pre-push review · iter -

  • Decision: Applied douwebot's optional serial comma in docs/models/overview.md (a reached spend limit, or disabled billing); pushed without a pre-push review: budget 3 of 3 used, prose-only one-character delta
  • Why: Cheap and matches the serial comma in the same paragraph's previous sentence (the rest of the file is mixed); the merge bar needs a douwebot approval with no comments on the head
  • Source: https://github.com/pydantic/pydantic-ai/pull/13269#discussion_r4555859068
  • Session: a9a64821-6de5-43ec-a7f6-3638aab22c68

2026-11-12 · Pin the batch spend limits too · iter -

  • Decision: Add a user-set spend-limit row to test_anthropic_batch_errors_raise_the_category_of_the_same_error_outside_a_batch and test_anthropic_batch_spend_cap_raises_quota_exceeded (_errored takes keyword-only details); pushed without pre-push review: budget 3 of 3 used, test code only
  • Why: douwebot asked for the batch half of the PR body's stream-and-batch claim to be pinned; batch_decode passes the whole error envelope, details included, to api_error
  • Source: https://github.com/pydantic/pydantic-ai/pull/13269#discussion_r4556156119
  • Session: a9a64821-6de5-43ec-a7f6-3638aab22c68
  • Supersedes: Pin the in-stream spend cap with a Case

2026-11-12 · Rerun CI once on 6ccfda13a for the pyright time-budget flake · iter -

  • Decision: Reran the failed jobs of CI run 43304517910 once; a second identical failure is treated as real and reported
  • Why: quality checks: pyright passed with 0 errors in 292.6s, over the 280s PYRIGHT_TIME_BUDGET; known flake issue 12792; the delta is one test function and a dict annotation
  • Source: https://github.com/pydantic/pydantic-ai/issues/12792
  • Session: a9a64821-6de5-43ec-a7f6-3638aab22c68

2026-11-12 · Merge main for the 370s Pyright budget · iter -

  • Decision: Merged upstream/main (b8e111d1f) as f525a5eae and pushed instead of waiting on the CI rerun, which keeps the old merge ref and its 280s budget; no conflict, git diff upstream/main...HEAD unchanged, no pyproject/uv.lock change; exempt delta (target-branch merge with no conflict resolution)
  • Why: PR 13289 raised the pull-request PYRIGHT_TIME_BUDGET to 370s at 20:56:55Z; douwebot had not run on 6ccfda13a, so the new head costs no extra douwebot run (local-notes/dispatch-20261112T211237Z.md)
  • Source: https://github.com/pydantic/pydantic-ai/pull/13289
  • Session: a9a64821-6de5-43ec-a7f6-3638aab22c68
  • Supersedes: Rerun CI once on 6ccfda13a for the pyright time-budget flake

2026-11-12 · Merge bar met at f525a5eae · iter -

  • Decision: CI green (run 43308102384), PR Review Checks success, clean douwebot APPROVE (run 43311195492, no douwebot comments on the head), 0 unresolved threads; holding for the manager's second-opinion choice before the final metadata check
  • Why: S merge bar from main's pushing-commits-to-the-repo; second-opinion choice belongs to the PR owner
  • Source: https://github.com/pydantic/pydantic-ai/pull/13269#pullrequestreview-5882435886
  • Session: a9a64821-6de5-43ec-a7f6-3638aab22c68

2026-11-12 · Optional douwebot nits on an APPROVE: resolve and hold for PR 13099 · iter -

  • Decision: Once PR 13099 lands, an inline douwebot finding marked optional, on an APPROVE, passes the clean-approval gate after it is resolved. Until then, don't re-apply douwebot only to clear a nit: address the nit, resolve its thread, then hold and wait for PR 13099. A re-apply after a pipeline failure (429, json_invalid, no verdict) still follows the one-retry rule. Review-body findings and discussion paragraphs still fail the gate, resolved or not: report those to the manager
  • Why: David chose to merge PR 13099 (in the merge queue), which lets resolved Optional: findings on a verified douwebot APPROVE pass Gate: clean douwebot approval (local-notes/dispatch-20261112T221048Z.md)
  • Source: https://github.com/pydantic/pydantic-ai/pull/13099
  • Session: a9a64821-6de5-43ec-a7f6-3638aab22c68

2026-11-12 · step 7: pydanty:review-lite · iter -

  • Decision: Second opinion is pydanty:review-lite, applied by the manager at 2026-11-12T22:15:06Z on f525a5eae; wait per 'waiting on pydanty' (15-min polls, 2 h 30 min horizon), triage DDD+, report regressions or scope questions first; if the head moves, re-meet the merge bar; then final metadata check and report; the manager enqueues
  • Why: David via p2-mgr-1, 2026-11-12; picked from the step-7 options (recommended: narrow diff that moves behavior and user-facing docs) (local-notes/dispatch-20261112T221518Z.md)
  • Source: https://github.com/pydantic/pydantic-ai/pull/13269
  • Session: a9a64821-6de5-43ec-a7f6-3638aab22c68

2026-11-12 · Dismiss pydanty's live-cassette finding · iter -

  • Decision: Dismissed: no cassette for the spend-limit shapes; replied (issuecomment-6838210222), reacted -1, minimized the findings comment as resolved; head unchanged at f525a5eae
  • Why: The repo's bar for a provider wire claim is a live call, a recorded cassette, or the provider's docs; the bodies come from Anthropic's rate-limits docs and pydanty's spec-conformance check matched the diff against that page; 'No live recording' decision stands (David via p2-mgr-1, local-notes/dispatch-20261112T225449Z.md)
  • Source: https://github.com/pydantic/pydantic-ai/pull/13269#issuecomment-6837413540
  • Session: a9a64821-6de5-43ec-a7f6-3638aab22c68

2026-11-12 · Final metadata check at f525a5eae · iter -

  • Decision: Kept 3 of 4 corrections: synced the PR-decisions mirror, linked each proving test by Files-tab anchor, moved the before/after table under a User-visible behavior section; also made What changes for existing users one sentence. Dropped adding the streaming label: stream classification is a shared-path pin, not a subject the PR materially covers
  • Why: haiku metadata checker, claude -p from policy-base worktree at 511d00512, read-only tools, no fan-out; rules verified in pushing-commits-to-the-repo 'Write the title and body' and 'Keep labels current'
  • Source: https://github.com/pydantic/pydantic-ai/pull/13269
  • Session: a9a64821-6de5-43ec-a7f6-3638aab22c68

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.

https://claude.ai/code/session_01HhQjZAzARWcdeZXtB2pH9P


Summary by cubic

Raises ModelQuotaExceededError for Anthropic spend-limit errors, which were previously misclassified as a rate limit or a rejected request. Closes #12061.

Migration

  • The tier spend cap (429 rate_limit_error with error code enforced_spend_limit_reached) was ModelRateLimitError and is now ModelQuotaExceededError; a user-set org/workspace limit (400 invalid_request_error starting "You have reached your specified ... API usage limits") was ModelRequestRejectedError and is now ModelQuotaExceededError.
  • Catch ModelQuotaExceededError instead of those two types; except ModelHTTPError, fallback behavior, and provider_error_code are unchanged, and a ramp-limit 429 with retry-after stays ModelRateLimitError.
  • Stream error events and errored Message Batches results classify through the same path, now pinned by the anthropic-stream-spend-cap and batch tests.