Claude API · Errors and configuration
Handle API errors
Catch the most specific exception first. The SDK already retries rate limits and server errors.
try:
response = client.messages.create(...)
except anthropic.BadRequestError as e:
print("Bad request:", e.message)
except anthropic.AuthenticationError:
print("Invalid API key")
except anthropic.RateLimitError as e:
print("Retry after", e.response.headers.get("retry-after"))
except anthropic.APIStatusError as e:
print("API error", e.status_code)
except anthropic.APIConnectionError:
print("Network error")
Official Claude API documentation
More errors and configuration commands
All errors and configuration commands · Claude API cheatsheet