Claude Hooks
Official scope (Exam Guide v1.0): hooks for guardrails and safety controls to prevent destructive actions. Lowest-weight skill: learn the rules and traps.
What a hook is
User-defined automation that runs at a lifecycle point. Handler types: command, http, mcp_tool, prompt, agent. Deterministic for command and HTTP handlers, unlike an instruction the model may ignore.
When and where
- Events: per session (
SessionStart,SessionEnd), per turn (UserPromptSubmit,Stop), per tool call (PreToolUse,PostToolUse), plus subagent and compaction events. - Configured in user, project, local or managed settings, plugin
hooks/hooks.json, and skill or subagent frontmatter. Project hooks follow workspace trust. Admins can setallowManagedHooksOnly. - Matchers filter by tool name (
Bash,Write|Edit,^mcp__); filter file paths inside the hook.
Exit codes
| Exit | Effect |
|---|---|
| 0 | Success; JSON on stdout is applied |
| 2 | Blocks the action (where the event can block); stderr is the reason. Blocks even if JSON says allow |
| Anything else (including 1) | Non-blocking error; the action proceeds |
Structured control: hookSpecificOutput.permissionDecision = allow / deny / ask / defer, plus a reason and optional updatedInput.
Hooks and permissions
A silent hook (exit 0, no output) approves nothing: the call continues through normal permission rules. A timed-out command or HTTP hook does not block unless onFailure: "block" is set. For a hard allow/deny the docs point to the permission system; use hooks for dynamic checks. Use both.
Hooks vs prompts (design guidance)
"Always format after edits" or "never rm -rf": a hook guarantees it runs; a prompt line does not.
Worked example
PreToolUse on Bash: read the command from stdin JSON; if it matches rm -rf, print the reason to stderr and exit 2; otherwise exit 0.
Exam traps
exit 1to block. It does not.- A hook as the only control.
- Assuming a timed-out hook blocks.
- Matching file paths in
matcher.
References
- Hooks reference — checked Oct 16, 2026
- Hooks in the SDK — checked Oct 16, 2026
- Permissions — checked Oct 16, 2026
Check your understanding
Hook exits 1 after detecting a forbidden command. Blocked?Show answerHide answer
Answer
No; use exit 2 or a deny decision.Must always run after edits. Hook or prompt?Show answerHide answer
Answer
Hook.A hook stays silent; is the call approved?Show answerHide answer
Answer
No, it goes through normal permission rules.Is a hook enough on its own?Show answerHide answer
Answer
No; combine with permissions, sandboxing and least privilege.