PreToolUse hook exit code ignored for subagent tool calls
Resolved 💬 5 comments Opened Mar 29, 2026 by MayurVirkar Closed May 25, 2026
Description
PreToolUse hooks configured in ~/.claude/settings.json run for subagent (Agent tool) tool calls but the exit code is ignored — the tool call proceeds even when the hook returns exit code 2 (block).
Steps to Reproduce
- Configure a PreToolUse hook in
~/.claude/settings.json:
{
"hooks": {
"PreToolUse": [
{
"matcher": "Grep|Glob|Read",
"hooks": [
{
"type": "command",
"command": "~/.claude/hooks/my-gate"
}
]
}
]
}
}
- Hook script (
~/.claude/hooks/my-gate) that blocks.tsfiles:
#!/bin/bash
INPUT=$(cat)
if echo "$INPUT" | /bin/grep -qE '\.(ts|tsx|js|jsx)"'; then
echo 'BLOCKED: Use alternative tool.' >&2
exit 2
fi
exit 0
- In the main session, the hook correctly blocks Read on
.tsfiles (returns error to Claude).
- Dispatch a subagent:
Agent({ prompt: "Read /path/to/file.ts", model: "haiku" })
- The subagent successfully reads the
.tsfile — the hook runs (confirmed via logging), receives the correct JSON input with the file path, and returns exit code 2, but the tool call is NOT blocked.
Expected Behavior
PreToolUse hooks should block tool calls for subagents the same way they do for the main session. If exit code 2 is returned, the tool call should be rejected.
Observed Behavior
- Hook IS called for subagent tool calls (confirmed via file logging)
- Hook receives correct JSON input (tool_name, tool_input with file_path)
- Hook returns exit code 2 with stderr message
- But the tool call proceeds anyway — the agent gets the file contents
Environment
- Claude Code CLI (latest)
- Linux (WSL2, Ubuntu)
- Permission mode: bypassPermissions
- Hook configured in
~/.claude/settings.jsonunderPreToolUse
Impact
This makes it impossible to enforce tool usage policies on subagents via hooks. Our use case: forcing agents to use a knowledge graph (codebase-memory-mcp) for code discovery instead of directly reading source files.
This issue has 5 comments on GitHub. Read the full discussion on GitHub ↗