What you need to know
Classify, then respond
| Error | Meaning | Response |
|---|---|---|
400 invalid_request_error | The request is wrong (bad field, malformed tools, unsupported content) | Do not retry; fix it; log request ID and alert |
401 authentication_error | Missing or bad key | Fix credentials |
403 permission_error | Key lacks access | Fix permissions |
404 not_found_error | Unknown resource (for example, a file ID from another workspace) | Fix the reference |
413 request_too_large | Request too big | Shrink or split |
429 rate_limit_error | Over a limit | Wait for retry-after, back off with jitter, retry |
500 api_error | Server error | A few retries with backoff |
529 overloaded_error | Temporary overload | A few retries with backoff, then fall back or degrade |
| Timeout / connection error | Network or long request | Stream long generations; bounded retry |
Fallback chains are for availability (overload, outages). Refusals and invalid requests never go to a fallback model: one bypasses a safety decision, the other fails everywhere.
Streams
- The terminal event is message_stop; the stop reason is in message_delta.
- A stream starts with HTTP 200 and can still fail later: an error event can arrive mid-stream. Mark partial output as incomplete and, on retry, replace it rather than append.
- Pauses are normal (thinking, ping events). Do not treat silence as the end.
- Long non-streaming requests can hit network idle timeouts; stream them, or use batch if nobody is waiting.
Files and multi-format ingestion
| Input | How to send |
|---|---|
| PDF, plain text | Document block (base64, URL or Files API file_id) |
| JPEG, PNG, GIF, WebP | Image block |
| Word, TIFF, spreadsheets and others | Convert first, or process with the code execution tool |
- Files uploaded with the Files API belong to the workspace of the key that uploaded them.
- Check type and size before sending; split documents over the limits.
- Fail loudly when a converter produces empty output; a blank image gives a confident wrong answer, not an error.
Batches
Results come back with each request's custom_id, in any order, with per-request errors. Match by ID, never by position.
Log what diagnoses the failure
Status, error type and request ID for every failure; never the API key.
In the exam
Items show integration code and error output. Find the defect (wrong terminal event, unclassified retries, stacked retry layers, unsupported file type, wrong workspace, positional batch matching) and pick the fix that follows the API's semantics. Distractors retry everything, rotate keys to dodge limits, abandon streaming, or rename file extensions.
Check your understanding
A wrapper retries every failure five times, including 400s from a malformed tools array. Fix?
Show answer
Answer: C. A malformed request fails the same way on every attempt.
File IDs uploaded by one service give not-found errors in another. Most likely cause?
Show answer
Answer: A. Files are scoped to the uploading workspace.
Some batch results attach to the wrong database rows. Why?
Show answer
Answer: D. Results can arrive in any order; custom_id is the link.
Apply it: Make Juniper's peak-season integration resilient
Last December, 529 errors made every Juniper worker retry immediately, then rate limits lasted 20 minutes. The team also wraps the SDK in its own five-retry loop, and its streaming chat handler shows half answers when an error arrives mid-stream. A new feature will let customers upload photos of damaged items, some as HEIC files.
- Write the retry policy by error class.
- Fix the retry layering and the storm.
- Fix the stream handler.
- Design the photo ingestion.
Done when: deterministic failures are never retried and no failure looks like success.
Show model answer
1. 429: wait retry-after, jittered exponential backoff. 529 and 500: up to three retries with jittered backoff, then degrade (answer with order status from the database only). 400, 401, 403, 404, 413: no retry, log request ID, alert. Refusal: safe message, log, no fallback.
2. Remove the custom five-retry loop and configure the SDK's retry count once; add a circuit breaker that stops calls after repeated 529s and probes before reopening.
3. Handle error events: mark the message incomplete, show "Something went wrong, retrying," and replace the partial text on retry; end only on message_stop and read stop_reason from message_delta.
4. Convert HEIC to JPEG and resize on upload; reject files that fail conversion with a clear message; send as image blocks before the question.