Handle errors
Use the SDK to catch Claude API errors by error code, HTTP status, or connection failure and to handle an error that arrives in a stream.
Use the SDK to catch Claude API errors by error code, HTTP status, or connection failure and to handle an error that arrives in a stream. To learn what each error means or to call the API without the SDK, see Claude API errors.
Catch errors by kind
This example handles one error code, one HTTP status, a connection failure, and every other error response:
client = anthropic.Anthropic()
try:
client.messages.create(
model="claude-opus-5-5",
max_tokens=1024,
messages=[{"role": "user", "content": "Hello, Claude"}],
)
except anthropic.EnforcedSpendLimitReachedError as e:
# One error code: enforced_spend_limit_reached
print("Monthly spend cap reached:", e.api_message)
except anthropic.RateLimitError as e:
# One HTTP status: any other 429
print("Rate limited. Request ID:", e.request_id)
except anthropic.APIConnectionError:
# An error with no response, such as a network failure or a timeout
print("Connection failed")
except anthropic.APIStatusError as e:
# Every other error response
print(e.status_code) # HTTP status
print(e.type) # error.type
print(e.api_message) # error.message
print(e.error_code) # error.details.error_code if any
print(e.details) # error.details if any
print(e.request_id) # request_idAn error reaches your code after the SDK’s own retries, if any, have failed (see Retries). For what each status means, see HTTP errors.
Keep a narrow case before a broad one. The Messages API returns enforced_spend_limit_reached, the code for the monthly spend cap, with a 429. If the branch for a 429 came first, it would catch that code too.
To handle another case, such as a 404, see SDK error types.
Handle a specific error code
To handle another error code, add its branch to Catch errors by kind before any broader branch.
Each code has its own class or constant named after the code, such as anthropic.EnforcedSpendLimitReachedError. Classes for other codes might sit in other namespaces. To find one, search the SDK’s source for the class name. A method’s doc comment lists the class or constant for each code that the method can return.
For the full list of codes, see ErrorDetailsPreview with links to the API reference available — press Tab until focus enters the card, Escape to return. in the API reference.
Handle an error in a stream
After a stream starts, the API has already returned a 200, so it sends an error as an error event. The SDK doesn’t retry the request.
Every error event reaches you as APIStatusError. Its HTTP status is the stream’s 200 or is missing. So a branch for one status or for one code’s class doesn’t catch it. Instead, check its error.type for one of the types in HTTP errors:
client = anthropic.Anthropic()
try:
with client.messages.stream(
model="claude-opus-5-5",
max_tokens=1024,
messages=[{"role": "user", "content": "Hello, Claude"}],
) as stream:
for text in stream.text_stream:
print(text, end="", flush=True)
except anthropic.APIStatusError as e:
if e.type == "overloaded_error":
# One error.type: overloaded_error
print("\nOverloaded:", e.api_message)
else:
raiseAn error event can also have an error code. Read it from error_code and compare it with the code that you want to handle, such as "enforced_spend_limit_reached".
A stream can fail in two other ways:
- The request fails before the stream starts: You get the errors in Catch errors by kind.
- The connection drops in mid-stream: No error event arrives, and the connection branch in Catch errors by kind might not catch the error. To find what does, see SDK error types.
To continue the response after a stream fails, see Error recovery.
Was this page helpful?
Cookie settings
We use cookies to deliver and improve our services, analyze site usage, and if you agree, to customize or personalize your experience and market our services to you. You can read our Cookie Policy here(opens in new tab).