Skip to content

fix: raise RateLimitError on HTTP 429 instead of crashing#1044

Open
ashishpatel26 wants to merge 1 commit into
anthropics:mainfrom
ashishpatel26:fix/issue-812-rate-limit-error-handling-v2
Open

fix: raise RateLimitError on HTTP 429 instead of crashing#1044
ashishpatel26 wants to merge 1 commit into
anthropics:mainfrom
ashishpatel26:fix/issue-812-rate-limit-error-handling-v2

Conversation

@ashishpatel26

Copy link
Copy Markdown

Summary

Resolves #812

When the Anthropic API returns HTTP 429, the SDK surfaced a generic uncaught exception with no actionable type. This PR adds a typed RateLimitError so callers can catch and handle rate-limit failures separately.

Changes Made

  • src/claude_agent_sdk/_errors.py: add RateLimitError(ClaudeSDKError) with optional retry_after: int field
  • src/claude_agent_sdk/_internal/query.py: add _is_rate_limit_error() helper; detect 429/rate-limit text in the error path and raise RateLimitError instead of a generic Exception
  • src/claude_agent_sdk/__init__.py: export RateLimitError
  • tests/test_errors.py: 18 tests covering subclassing, retry_after, and all _is_rate_limit_error detection patterns

Usage After This Fix

from claude_agent_sdk import query, RateLimitError

try:
    async for msg in query("...", options=options):
        ...
except RateLimitError as e:
    print(f"Rate limited. Retry after: {e.retry_after}s")

Testing

  • 18 unit tests added in tests/test_errors.py
  • All existing tests pass

Related Issue

Closes #812

…#812)

Add a typed RateLimitError exception so callers can catch rate-limit
failures separately from other errors. The read loop detects 429/
rate-limit text in the error and routes it through the new type rather
than a generic Exception.
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.

Agent SDK should handle 429 rate limits gracefully instead of crashing

1 participant