Claude API cheatsheet
Claude API: Errors and configuration
4 common Claude API tasks.
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")
Set a timeout and retries for one call
Override client settings for a single request without changing the client.
client.with_options(timeout=30.0, max_retries=5).messages.create(
model="claude-opus-5-5",
max_tokens=1024,
messages=[{"role": "user", "content": "Hello"}],
)
Log the request ID
Include the request ID when reporting a problem to Anthropic.
print(response._request_id)
Turn on SDK logging
Print the SDK's requests and responses while debugging.
export ANTHROPIC_LOG=debug