Skip to main content
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.

Event Overview

For complete event type definitions, see Hooks Reference.

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 session
  • decision: "block" + reason — Block tool execution or prevent AI from stopping
  • hookSpecificOutput — Event-specific output, such as modifying tool input (updatedInput), overriding tool output (updatedToolOutput), or injecting context (additionalContext)
For complete output type definitions, see Hooks Reference.

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.
  • matcher uses JavaScript regex syntax, matching the tool_name field.
  • continue: false terminates the session — only effective for PreToolUse, PostToolUse, PostToolUseFailure, UserPromptSubmit, Stop, SubagentStop events. Observation events (e.g. SessionEnd, CwdChanged) ignore this field.
  • When multiple hooks return conflicting decision values, "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.