Hook Types And Events
Definition
Hooks are user-configured handlers that fire at defined lifecycle events in Claude Code, letting you observe, block, or modify actions deterministically instead of relying on model behaviour.
Key points
- Handler
type:command|http|mcp_tool|prompt|agent. - Config shape:
hooks.<EVENT>[].{matcher, hooks:[{type, command, args, if, timeout, statusMessage, once}]}.once: trueruns once per session then self-removes. - Common events (both TS callbacks and settings-file):
PreToolUse,PostToolUse,PostToolUseFailure,UserPromptSubmit,Stop,SubagentStart,SubagentStop,PreCompact,PermissionRequest,Notification. - TS-only SDK callbacks:
SessionStart/SessionEnd,PostToolBatch,MessageDisplay,Setup,TeammateIdle,TaskCompleted,ConfigChange,WorktreeCreate/Remove. In PythonSessionStart/SessionEndexist only as settings-file shell hooks. - Full settings-file event list also includes:
UserPromptExpansion,PermissionDenied,TaskCreated,StopFailure,InstructionsLoaded,CwdChanged,FileChanged,PostCompact,Elicitation,ElicitationResult. - Matcher rules:
*/empty/omitted = all. Only letters/digits/_/-/space/,/|→ exact match (alternatives split by|or,). Any other char → unanchored JS regex (^mcp__= all MCP;Edit.*= Edit + NotebookEdit). MCP tools match asmcp__<server>__<tool>;mcp__memoryalone matches nothing → usemcp__memory__.*.Stopignores matchers. - Hot-reload: hooks reload mid-session (unlike model/outputStyle).
Why it matters for the exam
- D3 (20% of scored content) tests which event to attach to (e.g.
PreToolUseto gate a call) and the matcher syntax, especially themcp__<server>__<tool>pattern. PreToolUseis the answer for deterministic guardrails — see PreToolUse hook enforcement.
Common gotchas
- Python SDK lacks
SessionStart/SessionEndcallbacks → use settings-file shell hooks instead. - A bare
mcp__memorymatcher matches nothing; you must writemcp__memory__.*. Stopignores the matcher field entirely.
See also
Sources
Referenced by
Practice questions optional · AI
Generate fresh practice questions about this concept with AI. These are not vault-verified.