CertPrep
Domain 2 · Lesson 2.4 · 5 min read

Resilient Integrations: Errors, Streams and Files

Exam skillIntegration Resilience

Analyzing failing or degraded integration code and its error output to find the defect and select a correction consistent with documented API error semantics. Defects include mishandled streaming events and terminal-event semantics, retry and fallback logic that does not distinguish Anthropic's error classes (rate limits, overloaded, invalid request, timeout), and faulty multi-format ingestion through the Files API.

Read failing integration code and its errors, then fix the defect in line with the API's documented semantics. Classify errors before retrying, honor retry-after with jittered backoff, keep one retry layer, handle streams to message_stop including mid-stream errors, and ingest files in supported formats from the right workspace.

What you need to know

Classify, then respond

ErrorMeaningResponse
400 invalid_request_errorThe request is wrong (bad field, malformed tools, unsupported content)Do not retry; fix it; log request ID and alert
401 authentication_errorMissing or bad keyFix credentials
403 permission_errorKey lacks accessFix permissions
404 not_found_errorUnknown resource (for example, a file ID from another workspace)Fix the reference
413 request_too_largeRequest too bigShrink or split
429 rate_limit_errorOver a limitWait for retry-after, back off with jitter, retry
500 api_errorServer errorA few retries with backoff
529 overloaded_errorTemporary overloadA few retries with backoff, then fall back or degrade
Timeout / connection errorNetwork or long requestStream 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

InputHow to send
PDF, plain textDocument block (base64, URL or Files API file_id)
JPEG, PNG, GIF, WebPImage block
Word, TIFF, spreadsheets and othersConvert 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

Question 1

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.

Question 2

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.

Question 3

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

Exercise · about 25 minCase: Juniper Outdoor Co.

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.

  1. Write the retry policy by error class.
  2. Fix the retry layering and the storm.
  3. Fix the stream handler.
  4. Design the photo ingestion.
    Done when: deterministic failures are never retried and no failure looks like success.
Check your work
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.

Key termsCircuit breakerExponential backoffFiles APIRate limit

Sources