Hooks allow you to inject custom logic at key lifecycle points of an AI session, enabling audit logging, security controls, context injection, and dynamic behavior modification.
For complete event type definitions, see Hooks Reference.
Configure hooks in
The
Each hook callback receives the event input, tool use ID, and abort signal:
All events share common fields:
Callbacks return an object that controls behavior through these fields:
Block dangerous shell commands:
Override tool output to replace AK/Token and other sensitive information:
Trim overly long Bash output, keeping head and tail:
Prevent the AI from stopping when the task is incomplete:
Automatically approve Read tool permission requests:
Combine audit logging with security interception:
Event Overview
| Event | Trigger | Controllable Behavior |
|---|---|---|
PreToolUse | Before tool invocation | Intercept / allow / modify input |
PostToolUse | After tool succeeds | Audit / inject context / override output |
PostToolUseFailure | After tool fails | Error handling / logging |
UserPromptSubmit | Before user prompt is sent | Inject context / intercept |
SessionStart | Session begins | Initialize / inject context |
SessionEnd | Session ends | Cleanup / logging |
Stop | AI stops generating | Prevent stop, force continuation |
SubagentStart | Subagent starts | Observe / log |
SubagentStop | Subagent stops | Observe / log |
PreCompact | Before context compaction | Observe / log |
PostCompact | After context compaction | Observe / log |
CwdChanged | Working directory changes | Observe / log |
InstructionsLoaded | Instruction file loaded | Observe / log |
FileChanged | File created/modified/deleted | Observe / log |
PermissionRequest | Permission requested | Auto-approve / deny permission requests |
Configuration
Configure hooks in QueryOptions.hooks:
Matchers
The matcher field is a regex pattern — hooks only fire when the tool name matches:
Callback Functions
Each hook callback receives the event input, tool use ID, and abort signal:
Inputs
All events share common fields: hook_event_name (event type), session_id (session ID), transcript_path (transcript file path), cwd (working directory). Each event also has event-specific fields, such as tool_name and tool_input for PreToolUse.
For complete input type definitions, see Hooks Reference.
Outputs
Callbacks return an object that controls behavior through these fields:
continue: false— Terminate the sessiondecision: "block"+reason— Block tool execution or prevent AI from stoppinghookSpecificOutput— Event-specific output, such as modifying tool input (updatedInput), overriding tool output (updatedToolOutput), or injecting context (additionalContext)
Examples
Security Interception (PreToolUse)
Block dangerous shell commands:
Redact Sensitive Information (PostToolUse)
Override tool output to replace AK/Token and other sensitive information:
Truncate Long Output (PostToolUse)
Trim overly long Bash output, keeping head and tail:
Force Continuation (Stop)
Prevent the AI from stopping when the task is incomplete:
Auto-Approve Permissions (PermissionRequest)
Automatically approve Read tool permission requests:
For the complete permission model, see Permissions.
Audit and Security Controls (Combined)
Combine audit logging with security interception:
Notes
- Hook callbacks should return quickly to avoid blocking AI execution.
matcheruses JavaScript regex syntax, matching thetool_namefield.continue: falseterminates the session — only effective forPreToolUse,PostToolUse,PostToolUseFailure,UserPromptSubmit,Stop,SubagentStopevents. Observation events (e.g.SessionEnd,CwdChanged) ignore this field.- When multiple hooks return conflicting
decisionvalues,"deny"/"block"takes precedence (strictest rule wins). - When multiple hooks set
updatedToolOutput, the last non-empty value wins. For chained transforms (e.g. redact then truncate), execute them sequentially within a single callback.