The SDK provides a rich exception hierarchy. Catching specific error types lets you handle each failure mode appropriately.
Error Hierarchy
The SDK auto-retries on APIConnectionError, 408, 409, 429, and 500+ with exponential backoff. Default is 2 retries. Customize with max_retries.
Retry only what time will fix
The retryable errors are the ones that will succeed on a second attempt: RateLimitError (429 — wait and try again), APIConnectionError (network blip), and 5xx server errors (transient backend issue). Everything else — 400 bad request, 401 auth, 404 not found, 422 validation — will fail identically on retry, so retrying just hammers the wall.
Production code wraps the call in tenacity.@retry (or equivalent) with an exception filter, exponential jitter, and a finite cap. Without the cap, a multi-day rate-limit incident becomes a multi-day spend incident.