Skip to main content
Each hook event fires at a specific point in the agent’s lifecycle. Use the matcher field (a regex matched against the hook event’s tool_name) to filter which tool invocations trigger your hook.

PreToolUse

Fires before a tool executes. Use this to block, modify, or add context to tool calls. Stdin data: Example — Block destructive commands:
Example — Require confirmation for writes outside src/: Use a script that inspects the tool input and returns a decision:
Example — Rewrite commands before execution: A hook can transparently rewrite the tool’s input by printing hookSpecificOutput.updatedInput to stdout (see Output format). For example, a hook script that routes shell commands through a wrapper:
The rewritten arguments are merged into the tool call before it runs — the agent executes the updated command instead of the original.

PostToolUse

Fires after a tool finishes executing. Use this for logging, validation, or triggering follow-up actions. Stdin data: Example — Log all shell commands:

PermissionRequest

Fires when the agent needs a permission decision. Use this to implement custom approval logic. Stdin data: Example — Auto-approve git commands:

UserPromptSubmit

Fires when the user submits a message. Use this to add context or trigger workflows. Stdin data: Example — Inject context on every prompt:
The command prints additionalContext inside a hookSpecificOutput object on stdout, tagged with the event name. That text is injected into the agent’s context:

Stop

Fires when the agent decides to stop (finish its turn). Use this to add follow-up instructions or prevent premature stopping. Stdin data: Example — Remind agent to run tests:
Be careful with stop hooks that block — they can cause the agent to loop if the condition isn’t eventually satisfied.

PostCompaction

Fires after context compaction completes successfully. Use this for logging, triggering follow-up actions, or re-injecting context that may have been lost during compaction. Stdin data: Example — Log compaction events:

SessionStart

Fires when a new session begins. Use this for initialization, logging, or environment setup. Stdin data: Example — Run setup script:
A SessionStart command can also inject context by printing additionalContext inside a hookSpecificOutput object on stdout:

SessionEnd

Fires when a session ends. Use this for cleanup or final logging. Stdin data:

Matching Multiple Events

A single hooks file can define hooks for multiple events:

Using the Matcher

The matcher field is a regex matched against the hook event’s tool_name. It is available for tool-related events: PreToolUse, PostToolUse, and PermissionRequest. For non-tool events (UserPromptSubmit, Stop, PostCompaction, SessionStart, and SessionEnd), there is no tool_name; use "" or omit the matcher to run the hook for every event of that type.
The matcher is not a permission glob. Patterns like mcp__github__* are useful in permissions, but hook matchers are regexes. Use mcp__github__.* in a hook matcher.

Tool names you can match

Hook matchers run against the same externally-visible tool names that hook scripts receive in stdin as tool_name. The exact tool names available can vary by CLI mode, model, and enabled integrations. The most common public core tool names are:
  • read
  • edit
  • grep
  • glob
  • exec
MCP server tools appear as mcp__<server>__<tool>. For example, a github MCP server tool named create_issue appears as mcp__github__create_issue. For other tools, match the exact tool_name shown in hook stdin. To confirm the complete set available in your current session, add a temporary PostToolUse hook with matcher: "" and log the stdin payload.