Debugging and Error Handling
Official scope (Exam Guide v1.0): error type identification, recovery strategy selection, trace analysis, isolating the problem origin between the integration layer and the model output.
Error types
| Status | Type | Retry? |
|---|---|---|
| 400 | invalid_request_error (also: a spend limit you set; model-specific validation such as prefill, sampling params, forced tool choice) | No: fix the request |
| 401 | authentication_error (bad, revoked, expired key) | No |
| 402 | billing_error | No |
| 403 | permission_error | No |
| 404 | not_found_error | No |
| 409 | conflict_error | After resolving the conflict |
| 413 | request_too_large (32 MB Messages) | No: shrink or use Files API |
| 429 | rate_limit_error | Yes, honour retry-after; except the spend-cap 429 (no retry-after, enforced_spend_limit_reached) |
| 500 | api_error | Yes, backoff |
| 504 | timeout_error | Yes; prefer streaming or batch for long requests |
| 529 | overloaded_error | Yes, backoff |
SDKs retry transient errors twice by default. Streaming errors can arrive after a 200. Always log the request-id.
"Bad answer" is often a stop reason
max_tokens (truncated), refusal (a 200), unhandled tool_use, pause_turn not continued, model_context_window_exceeded, empty end_turn after text was added behind tool_result. Check stop_reason before blaming the prompt.
Recovery strategy selection (design guidance)
Retry transient errors with backoff and jitter (capped) · fix and resend request faults · fall back (smaller model, other model on refusal, cached answer, standard speed after a fast-mode 429) · repair malformed output (retry with the validation error fed back) · escalate to a human.
Trace analysis (design guidance)
Read the run in order: requests, tool calls with inputs and outputs, stop reasons, token usage, latency, and the transcript. Find the first step where reality diverged from intent. In Claude Code and the Agent SDK, hooks and OpenTelemetry export can capture this.
Isolate the origin (design guidance)
Replay the exact request in isolation: - Payload wrong (missing history, wrong tools, bad parameters, parsing bug, dropped thinking blocks) → integration fault; fix the code. - Payload right, answer still wrong → prompt or model issue; go to the eval set and iterate.
Evals catch what unit tests miss (verified principle)
Non-deterministic output needs eval sets that mirror real traffic plus edge cases (empty, very long, hostile, ambiguous input). Grading: exact match, code checks, similarity, or model-graded rubrics; grade with clear criteria, check the grader on a sample, prefer many automated cases. Re-run after every prompt, tool or model change.
Exam traps
- Retrying a 400.
- Retrying a spend-cap 429 forever.
- Blaming the model for a truncation or an integration bug.
- Green unit tests = working feature.
- No request id in logs.
References
- Errors — checked Oct 16, 2026
- Rate limits — checked Oct 16, 2026
- Handling stop reasons — checked Oct 16, 2026
- Define success criteria and build evaluations — checked Oct 16, 2026
- Observability with OpenTelemetry (Agent SDK) — checked Oct 16, 2026
Check your understanding
Retry 429 or 400?Show answerHide answer
Answer
429 (with backoff); 400 is a request fault.Answers cut off mid-sentence. First check?Show answerHide answer
Answer
stop_reason(max_tokens).The replayed request shows the tool results were never appended. Origin?Show answerHide answer
Answer
Integration layer.Unit tests pass, users report wrong answers. Why?Show answerHide answer
Answer
Tests do not reflect real inputs; use evals on realistic and edge cases.