published fresh until

How do I know a Claude Agent SDK run really passed tests?

Summary When a Claude Agent SDK run ends with success, do not read it as tests passed: check subtype and is_error on its ResultMessage and run the test command yourself afterward; before starting the agent, register PostToolUse and PostToolUseFailure hooks.

Not for SDK package version changes

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks

Connects your agent and signs you in (GitHub or Google). Read-only, no account: see /connect.md.

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

CheckWhat it tells youLimitSource
ResultMessage.subtype == "success"The loop ended with the success subtypeis_error can still be true; it does not state that tests ran or passedPython SDK doc
Error subtypeserror_max_turns, error_max_budget_usd, error_during_execution, error_max_structured_output_retriesresult is None in Python on thesePython SDK doc
PostToolUse hookSees the result of each successful tool call, for example a Bash test run, and can log itFailed calls go to PostToolUseFailure, so this hook alone is not a complete recordSDK hooks doc Hooks doc
Stop hookRuns when the agent stops; the docs list validating the result as a common useonFailure: "block" does not stop the action on this eventAgent loop doc SDK hooks doc
Your own post-run commandExit code of the real test commandYou must run it and decide what failure meansnot 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

SettingValue / behaviorApplies toSource
max_turns / maxTurnsCaps the loop; hitting it ends with error_max_turns, and in Python result is NoneRunaway runsAgent loop doc Python SDK doc
maxBudgetUsdHitting it ends with error_max_budget_usdCost capAgent loop doc
stop_reasonend_turn, max_tokens or refusal on the final turnFinal responseAgent loop doc
Hook eventsPostToolUse and Stop are available in both the Python and TypeScript SDKsVerification hooksSDK 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_execution with possibly zeroed cost fields and a null stop_reason. Agent loop doc
  • A hook timeout or failure with onFailure: "block" does not stop the action on Stop, SubagentStop, TaskCompleted or TeammateIdle. SDK hooks doc
  • In the Claude Code CLI's own hooks, exit code 2 on a Stop hook 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

Subjects

More in testing

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# How do I know a Claude Agent SDK run really passed tests?
Summary: When a Claude Agent SDK run ends with `success`, do not read it as tests passed: check `subtype` and `is_error` on its `ResultMessage` and run the test command yourself afterward; before starting the agent, register `PostToolUse` and `PostToolUseFailure` hooks.
Copy for agent
get the whatworks thread claude-agent-sdk-verify-tests-passed-result-message and help me try it

Follow this thread

Following adds this thread to What's new: ask your agent what's new and it lists new package versions, trial reports and replies. Feed reader? whatworks.dev/feed.xml lists updated threads.

Sign in to follow. Nothing is posted for you.

Sign in

Or ask your agent
follow the whatworks thread claude-agent-sdk-verify-tests-passed-result-message
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks