Skip to content

Hook trigger reference

The complete lifecycle reference pulled out of the Agent Hooks lesson, so you can jump back to it mid-workshop. The lesson keeps the lifecycle diagram and the examples; this page holds the lookup tables.

TriggerFires whenMatcher matchesCan block?
SessionStartSession beginsNoneNo
StopSession endsNoneNo
PreToolUseBefore a tool runsTool name (regex)Yes
PostToolUseAfter a tool runsTool name (regex)No
PreTaskExecBefore a spec task startsNoneYes
PostTaskExecAfter a spec task finishesNoneNo
UserPromptSubmitYou submit a promptPrompt text (regex)Yes
PostFileCreateAfter a file is createdFile path (regex)No
PostFileSaveAfter a file is savedFile path (regex)No
PostFileDeleteAfter a file is deletedFile path (regex)No
ManualYou trigger it on demandNoneNo

matcher is a regex. What it’s tested against depends on the trigger:

TriggerMatcher evaluates against
PostFileSave, PostFileCreate, PostFileDeleteFile path
PreToolUse, PostToolUseTool name
UserPromptSubmitPrompt text
SessionStart, Stop, PreTaskExec, PostTaskExec, ManualNot evaluated. The hook always fires

Omitting matcher means the hook always matches. For file-related triggers, the {{filePath}} template variable is available inside a command action and expands to the path of the file that triggered the hook.

For command actions, the exit code carries meaning:

Exit codeBehavior
0Success. STDOUT is added to the agent’s context for SessionStart and UserPromptSubmit. For every other trigger STDOUT is ignored.
2Blocks the action (PreToolUse and UserPromptSubmit only). STDERR is returned to the agent.
OtherA warning with the command’s STDERR is shown to you, and execution proceeds.