Skip to main content
NOV 2026 Latest guide updates. Latest: AI Design Patterns Local vs cloud AI Grill me Changelog →
M11 Intermediate Methodology

Hooks: Events & System

Automatically react to Claude Code actions

PDF editions Β· Guide v3.44.1 Β· Updated

What is a hook

Command hooks run on configured events and receive JSON on stdin. Other hook types include prompt, agent, HTTP and MCP tool hooks; availability depends on the event. Claude Code has 33 hook events in total; the tables show the main ones.

Main events

EventMomentCan block
SessionStartStartup or resumeNo
SessionEndSession endsNo
UserPromptSubmitBefore prompt processingYes
PreToolUseBefore a tool callYes
PostToolUseAfter a tool succeedsNo
PreCompactBefore context compactionYes
NotificationClaude sends a notificationNo
StopClaude finishes respondingYes
SubagentStopA sub-agent endsYes

Multi-agent and configuration events

EventSinceUse
TeammateIdlev2.1.32+ (Agent Teams)A team member is about to go idle
TaskCompletedv2.1.32+ (Agent Teams)A task is about to be marked complete
ConfigChangev2.1.49+A config file changed during the session

Team gates can request further work before completion. ConfigChange audits configuration independently of Agent Teams.

Configuration in settings.json

{
"hooks": {
"PreToolUse": [{
"matcher": "Bash",
"hooks": [{
"type": "command",
"command": "${CLAUDE_PROJECT_DIR}/.claude/hooks/security.sh",
"args": [], "timeout": 10
}]
}]
}
}

Configuration fields

FieldUsage
commandExecutable when args is present; otherwise shell command
argsArguments passed to command without a shell; does not replace the executable
timeoutSeconds; defaults: command 600, prompt 30, agent 60
asyncIf true, Claude continues without waiting for completion
continueOnBlockPrompt hooks: continue after ok: false on PreToolUse, PostToolUse and TeammateIdle
asyncRewakeRuns in the background and wakes Claude on exit code 2 (implies async)
onFailure"block" blocks the action when a command or HTTP hook cannot start, times out or exits unexpectedly (v2.1.295; default continue)

Exit codes

CodeMeaning
0Success; valid JSON may still deny an action
2Blocking on supported events; behavior varies by event
Other non-0Usually a nonblocking error without decision JSON

For PreToolUse hooks, exit 2 prevents the tool from executing. Claude reads the stderr message and can adapt its behavior.

Synchronous vs asynchronous

Keep validation synchronous. async: true runs a command hook in the background and cannot stop the action; use it for notifications or logging. Formatting in the background can race with subsequent edits.

stdin data flow

{
"tool_name": "Edit",
"tool_input": { "file_path": "src/auth.ts" },
"tool_response": { "success": true },
"hook_event_name": "PostToolUse"
}

Stop includes last_assistant_message. Full decisions and exceptions: Hooks reference.

Enter your email to read the full card and get the complete PDF bundle.

All content is free and open-source. We just ask for your email.

PDF: