What this thread does
Do first: do not treat a success result as proof that tests passed; run the test command in your own code after the loop ends and use its exit code as the verdict. Agent loop doc
Short answer
In the Claude Agent SDK, the final ResultMessage has a subtype that is the primary way to check how the loop ended. A success subtype does not prove normal task completion: is_error can be true on success, so also check is_error, stop_reason and any error details, and no result field states that a test was run. Python SDK doc The tools that can show what actually ran are hooks: PostToolUse fires after a tool call succeeds, PostToolUseFailure after one fails, and Stop fires when the agent finishes. Agent loop doc Hooks doc So a verified result needs a check that lives outside the model's final text.
Choose / don't choose
| Check | What it tells you | Limit | Source |
|---|---|---|---|
ResultMessage.subtype == "success" | The loop ended with the success subtype | is_error can still be true; it does not state that tests ran or passed | Python SDK doc |
| Error subtypes | error_max_turns, error_max_budget_usd, error_during_execution, error_max_structured_output_retries | result is None in Python on these | Python SDK doc |
PostToolUse hook | Sees the result of each successful tool call, for example a Bash test run, and can log it | Failed calls go to PostToolUseFailure, so this hook alone is not a complete record | SDK hooks doc Hooks doc |
Stop hook | Runs when the agent stops; the docs list validating the result as a common use | onFailure: "block" does not stop the action on this event | Agent loop doc SDK hooks doc |
| Your own post-run command | Exit code of the real test command | You must run it and decide what failure means | not covered by a doc page; a design choice |
Choose the post-run command when "tests passed" must be true; use hooks to record what the agent ran. Not for checking cost or turns, which the result message carries: total_cost_usd, usage, num_turns and session_id on every subtype. Agent loop doc
Settings that matter
| Setting | Value / behavior | Applies to | Source |
|---|---|---|---|
max_turns / maxTurns | Caps the loop; hitting it ends with error_max_turns, and in Python result is None | Runaway runs | Agent loop doc Python SDK doc |
maxBudgetUsd | Hitting it ends with error_max_budget_usd | Cost cap | Agent loop doc |
stop_reason | end_turn, max_tokens or refusal on the final turn | Final response | Agent loop doc |
| Hook events | PostToolUse and Stop are available in both the Python and TypeScript SDKs | Verification hooks | SDK hooks doc |
Order of operations
1. Before starting the agent, register PostToolUse and PostToolUseFailure hooks in the options that record Bash commands and their results, so the log shows whether a test command ran at all. SDK hooks doc Hooks doc 2. Run the agent, consume the query stream to completion, and inspect its ResultMessage; check subtype and is_error before interpreting result. Agent loop doc Python SDK doc 3. After a success result, run the test command yourself and treat its exit code as pass or fail. 4. If it fails, resume the session with the failure output; the result carries a session_id for resuming. Agent loop doc
Known failures and fixes
- A single-shot
query()that ends on an error result yields the final result message and then raises an error, and the Claude Code process exits nonzero; wrap the loop in a try block if the code must continue. Agent loop doc - After a session crash the final result is
error_during_executionwith possibly zeroed cost fields and a nullstop_reason. Agent loop doc - A hook timeout or failure with
onFailure: "block"does not stop the action onStop,SubagentStop,TaskCompletedorTeammateIdle. SDK hooks doc - In the Claude Code CLI's own hooks, exit code 2 on a
Stophook sends Claude back to keep working; that page describes CLI command hooks, so the SDK callback behavior is not assumed to match. Hooks doc
Missing
- The docs read do not describe a built-in "tests passed" field on the result; none is claimed.
Evidence
Claude Agent SDK pages "How the agent loop works", "Control execution with hooks" and the Python reference, and the Claude Code hooks reference. 0 trials in this draft; not tried by WhatWorks.
Checked
2026-12-03; the four pages were read as served on that date.
Started from a post by @Anthropic, packaged by the editor. Original post