Skip to Content
User GuideHook reference

Hook events and protocol reference

Start with the Hooks guide. The table lists the 27 events explicitly exposed in the settings schema. Delivery depends on the associated feature and action; configuring an event alone does not create a workflow, team, or file watcher.

Events

EventTrigger
PreToolUseBefore a tool executes; inspect tool_name and tool_input.
PostToolUseAfter successful tool execution; includes tool_response.
PostToolUseFailureAfter tool failure; includes error and interruption context.
SubagentStartWhen a subagent starts.
SubagentStopWhen a subagent is about to finish.
NotificationA notification is emitted; inspect notification_type.
SessionStartSession initialization; source identifies how it started.
SessionEndSession shutdown; includes reason.
PreCompactBefore context compaction.
PostCompactAfter context compaction.
StopThe assistant is about to stop responding.
QueryEndA query completes; notification only.
UserPromptSubmitBefore a submitted user prompt is processed.
ConfigChangeA configuration change is being processed.
StopFailureAn error stops the agent; notification only.
TeammateIdleA teammate attempts to become idle.
InstructionsLoadedInstruction files load; notification only.
CwdChangedThe working directory changes; old_cwd and new_cwd.
FileChangedA watched file changes; file_path and event.
WorktreeCreateCreate a worktree; return its path.
WorktreeRemoveRemove a worktree.
ElicitationAn MCP server requests structured user input.
ElicitationResultAfter the MCP elicitation result.
TaskCreatedA task is created.
TaskCompletedA task is marked complete.
PermissionRequestA tool operation requires a permission decision.
PermissionDeniedA tool operation has been denied.

Configuration structure

{ "hooks": { "SessionStart": [ { "hooks": [ { "type": "command", "command": "node", "args": ["-e", "process.stdout.write(JSON.stringify({systemMessage: 'Session started'}))"], "timeout": 10 } ] } ] } }

An event contains an array of definitions. Each definition has a hooks array and optional matcher, sequential, and async. Matcher values depend on the event; do not apply tool-name matching rules to every lifecycle event. For command hooks, args enables direct process invocation: shell pipes, redirects, and globbing are not interpreted, and shell is ignored.

TypeMain fieldsBehavior
commandcommand, args?, shell?, env?, timeout?Read JSON on stdin; return output on stdout
httpurl, headers?, allowedEnvVars?, timeout?POST input JSON and read a JSON response, subject to URL policy
promptprompt, model?, timeout?Evaluate the event with a single model request
agentprompt, tools?, maxTurns?, timeout?Verify a condition with a tool-using agent and structured result

timeout is measured in seconds. runtime is an internal function form, not a user hook to serialize in JSON. Model-based hooks make model requests. Asynchronous output is not guaranteed to arrive before the original operation; do not use asynchronous hooks for checks that must block synchronously.

Input and output

Common input fields are session_id, transcript_path, cwd, and hook_event_name. model, permission_mode, agent_id, agent_type, and workspace_dirs are optional according to context. Events add their own fields; scripts should tolerate unknown fields and missing optional fields.

{ "systemMessage": "Validation completed" }

systemMessage is a UI message; do not assume the model receives it. For model context, use the event-supported hookSpecificOutput fields. SessionStart and UserPromptSubmit also have dedicated stdout-to-context handling. When returning JSON on stdout, send debugging text to stderr to avoid corrupting parsing.

Command hooks generally use exit code 0 for success, 2 for blocking feedback, and other nonzero codes for non-blocking errors. Event semantics take precedence: StopFailure and InstructionsLoaded are notification-only and ignore control output; any nonzero WorktreeCreate exit fails creation, and success must return the worktree path. Not every event is a permission interceptor.

HTTP hooks transmit the event payload. Headers support ${ENV_VAR} interpolation, optionally restricted by allowedEnvVars; review configurations using permissions and data and privacy. First verify delivery with a notification-only hook, then add the necessary control fields. Avoid unbounded continuation loops in stop hooks.

Last updated on