Agent SDK Hooks
What hooks are
Callbacks (or shell commands from settings files) that run when an event fires. They can block operations, log and audit, transform inputs and outputs, require approval, and track the session lifecycle.
Key events
| Event | Fires when | Typical use |
|---|---|---|
PreToolUse | Before a tool call; can block or modify | Block dangerous commands, rewrite arguments |
PostToolUse | After a tool returns | Audit trail, normalise or redact output |
PostToolUseFailure | Tool failed | Handle or log errors |
UserPromptSubmit | A prompt is submitted | Inject context |
Stop | Agent execution stops | Save state |
SubagentStart / SubagentStop | Subagent starts or finishes | Track and aggregate parallel work |
PreCompact | Before conversation compaction | Archive the transcript |
PermissionRequest | A tool call needs a decision | Custom permission handling |
Several events (for example SessionStart, SessionEnd) are TypeScript-only as SDK callbacks; in Python they exist only as shell hooks in settings files.
Matchers
matcherfilters by event target. For tool hooks it is the tool name:"Bash","Write|Edit", or a regex such as^mcp__. MCP tools are namedmcp__<server>__<action>.- Omit the matcher to run for every event of that type.
- Matchers match tool names, not file paths. To filter by path, inspect
tool_input.file_pathinside the callback.
Outputs
- Return
{}to allow with no change. - Control the current operation through
hookSpecificOutput: forPreToolUse,permissionDecision(allow/deny/ask/defer),permissionDecisionReason,updatedInput; forPostToolUse,additionalContextandupdatedToolOutput. updatedInputmust be insidehookSpecificOutput, and should be a new object, not a mutation. Withdefer,updatedInputis ignored.- Top-level
systemMessageis shown to the user, not the model. To give the model context, useadditionalContext. {"async": true}lets the agent continue without waiting, but an async hook cannot block, modify or inject context. Use it for logging only.
Behaviour to remember
- Multiple matching hooks run in parallel; precedence is
deny>defer>ask>allow. - A hook that times out is cancelled and its output discarded. For
PreToolUse, the tool call does not run and Claude is told the hook did not respond. - Hooks may not fire when the agent hits
max_turns, because the session ends first. - Catch errors inside a hook instead of letting them propagate.
Exam traps
- Using a hook for something a prompt line can do, or a prompt line for something a hook must guarantee.
- Matching on a file path in
matcher. - Putting
updatedInputat the top level, so it is ignored. - Expecting an async hook to block.
- Relying on hook order.
References
- Hooks in the SDK — checked Oct 16, 2026
Check your understanding
Block writes to
.env. Event, matcher, return?Show answerHide answerAnswer
PreToolUse,Write|Edit, inspecttool_input.file_path, returnpermissionDecision: "deny"with a reason.You want Claude, not the user, to see extra info after a tool call. Field?Show answerHide answer
Answer
additionalContextinPostToolUsehookSpecificOutput.Which wins when one hook says
allowand anotherdeny?Show answerHide answerAnswer
deny.