Idiomatic Error Handling: Transient vs. Client Errors
CoreApply software-engineering foundations and error handling to Claude integrations · Difficulty 2/5
Explanation
Two Classes of Error
Robust integration code has to distinguish two fundamentally different failure classes and respond to each correctly:
| Status | Class | Correct response |
|---|---|---|
| 429 | Rate limit -- transient | Exponential backoff with jitter, then retry |
| 529 / 5xx | Overloaded -- transient | Exponential backoff with jitter, then retry |
| 400 | Malformed request -- your bug | Fix the payload; retrying unchanged will not help |
| 401 | Bad key -- your bug | Fix authentication; retrying unchanged will not help |
The SDKs surface these as typed exceptions, so integration code can branch on error type rather than parsing raw status codes by hand.
Why Backoff (and Jitter) Specifically
A 429 or 529 means the request itself was fine -- the service is asking you to slow down or come back later. Retrying immediately (or in a tight loop) tends to make the underlying congestion worse. Exponential backoff spaces retries out with growing delay; adding jitter (small randomization) prevents many clients from retrying in lockstep and re-creating the same spike.
Why Not for 400/401
A 400 or 401 means the request itself is wrong -- a malformed payload or an invalid credential. No amount of waiting fixes a bug in the request; retrying the identical request will fail identically. These require fixing the payload or the key, not backoff.
Common exam traps
- Applying the same backoff-and-retry strategy to a 400 or 401 as to a 429/529. Retrying an unfixed bug just repeats the failure.
- Treating a 429 as a bug to fix in the payload rather than a transient, retryable condition.
- Retrying without jitter, which can cause synchronized retry storms across many clients hitting the same transient condition.
Key Takeaways
- 429 (rate limit) and 529/5xx (overloaded) are transient -- retry with exponential backoff and jitter
- 400 (malformed request) and 401 (bad auth) are client-side bugs -- retrying unchanged will not help; fix the payload or the key
- Jitter prevents synchronized retry storms across many clients backing off in lockstep
- The SDKs surface typed exceptions so integration code can branch on error class rather than parsing raw status codes
Glossary Terms
Related Concepts
REST/JSON, Async I/O, Version Control & Refactoring for Claude Integrations
The Claude API is a standard HTTPS + JSON REST service -- status codes, headers, request/response shape, idempotency, and pagination all apply
Messages API Request Shape, stop_reason & usage
A Messages API request needs messages (alternating user/assistant), model, and required max_tokens; system and tools are optional