Claude Code SubagentStop hook: check a subagent's report
A SubagentStop hook runs when a subagent has finished its answer. It can read that answer and send the subagent back to work when something is missing.
This page covers the input, how to block, a script you can copy, and the traps. Every statement about Claude Code comes from the official hooks reference, linked at the end.
When the hook runs
SubagentStop runs when a Claude Code subagent has finished responding. The matcher filters on the agent type, with the same values as SubagentStart: built-in names such as general-purpose, Explore or Plan, or the name of a custom subagent. A subagent that ships with a plugin has a plugin-scoped type such as my-plugin:reviewer. The colon puts that matcher on the regular-expression path, so anchor it with ^ and $ for an exact match.
The event also fires when one of Claude Code's own internal agents finishes, for example for prompt suggestions or a /btw side question. For those, agent_type is the name of the agent the session itself runs as, for instance one set with --agent, or an empty string when there is none.
What arrives on stdin
Next to the common fields, a SubagentStop hook receives these:
- stop_hook_active
- True when a stop hook has already kept the subagent working.
- agent_id, agent_type
- Which subagent finished. For built-in agents the type is a name such as
ExploreorPlan. - agent_transcript_path
- The subagent's own transcript, in a nested
subagents/folder.transcript_pathis the main session's transcript. - last_assistant_message
- The text content of the subagent's final response.
- background_tasks, session_crons
- The same arrays as in Stop input. Both are scoped to the parent session, not to the subagent.
{
"session_id": "abc123",
"cwd": "/Users/...",
"permission_mode": "default",
"hook_event_name": "SubagentStop",
"stop_hook_active": false,
"agent_id": "def456",
"agent_type": "Explore",
"agent_transcript_path": "~/.claude/projects/.../abc123/subagents/agent-def456.jsonl",
"last_assistant_message": "Analysis complete. Found 3 potential issues...",
"background_tasks": [],
"session_crons": []
}On Claude Code v2.1.271 or later, a subagent that runs with the SubagentHandback tool delivers its report through that tool before it stops. Then last_assistant_message holds only the subagent's closing text, if any, and not the report. The report is the message input of that call, which a PreToolUse or PostToolUse hook matched on SubagentHandback receives as tool_input.message.
Keep the subagent working
To keep the subagent running, return decision: "block" with a reason, or return hookSpecificOutput.additionalContext. The subagent receives that text. In the hookSpecificOutput object, set hookEventName to "SubagentStop".
{
"decision": "block",
"reason": "End your report with a Files: line."
}- Exit code 2 prevents the subagent from stopping. Your stderr message is the reason.
continue: falsetakes precedence over exit code 2 on this event. The subagent ends its turn. Leave the field out to keep it working.- To add context to the parent session after a foreground subagent returns, use a
PostToolUsehook on theAgenttool instead.
Example: require a Files line
The script below sends an Explore subagent back once when its report has no line that starts with Files:. The Files: convention is ours, not a Claude Code feature. The script runs as it stands: a test in our repository executes this exact code.
1. Add the hook
import { readFileSync } from "node:fs";
const input = JSON.parse(readFileSync(0, "utf8"));
// Loop guard: a stop hook already sent the subagent back once.
if (input.stop_hook_active) process.exit(0);
const report = input.last_assistant_message ?? "";
if (!/^Files:/m.test(report)) {
console.log(
JSON.stringify({
decision: "block",
reason:
"End your report with one line that starts with Files: and lists the files you read.",
}),
);
}It checks stop_hook_active first. On the second stop the hook exits 0 and the subagent may end, even without the line.
2. Register it
{
"hooks": {
"SubagentStop": [
{
"matcher": "Explore",
"hooks": [
{
"type": "command",
"command": "node",
"args": [
"${CLAUDE_PROJECT_DIR}/.claude/hooks/require-files.mjs"
],
"timeout": 10
}
]
}
]
}
}Hooks from settings files, managed settings and plugins also run inside subagents, so a project file is enough.
Traps
- Always read
stop_hook_active. A hook that blocks on a condition the subagent cannot meet would block again and again. The reference tells Stop hooks to check this field, andSubagentStopreceives it too. The Stop hook page shows the guard in detail. - A missing script does not fail loudly. When stdout is blank and stderr contains
No such fileorcan't open, Claude Code assumes the script is missing, and exit 2 counts as a non-blocking error. Check the path. To block on purpose, printdecision: "block"JSON and exit 0. onFailure: "block"does nothing here. The reference listsSubagentStopamong the hooks it has no effect on, because exit code 2 sends it back to work and Claude cannot repair a hook that will not run.last_assistant_messagemay not be the report. WithSubagentHandbackthe report travels in a tool call. See the input section.- The web-fetch subagent is special. For the built-in
web-fetchsubagent, Claude Code runsSubagentStophooks from managed settings only. For a hook set anywhere else, matchWebFetchin aPostToolUsehook. - A matcher on agent types skips an empty type. A matcher that names types does not match an empty
agent_type. A hook with no matcher,""or"*"does run for those internal agents.
Sources
- Claude Code docs: Hooks reference (SubagentStop, SubagentStop input, Stop decision control, exit code 2 behavior per event)
Checked against the official docs on .
More: All Claude Code hook events · Stop hook · PostToolUse hook · All docs