Skip to main content

Overview

Hooks allow you to intercept and control Claude’s behavior at key lifecycle events like tool usage, prompt submission, and task execution.

HookEvent

Supported hook event types.
HookEvent
Fires before a tool is executed. Can approve, deny, or modify tool input.
HookEvent
Fires after successful tool execution. Can add context or modify output.
HookEvent
Fires when tool execution fails. Can add error context.
HookEvent
Fires when user submits a prompt. Can add context to the prompt.
HookEvent
Fires when the main session stops.
HookEvent
Fires when a sub-agent (Task tool) completes.
HookEvent
Fires before conversation compaction.
HookEvent
Fires for system notifications.
HookEvent
Fires when a sub-agent starts.
HookEvent
Fires when permission is requested for a tool.

HookCallback

Function signature for hook callbacks.
Hook callbacks receive:
  1. input: HookInput - Strongly-typed input data for the event
  2. tool_use_id: str | None - Optional tool use identifier
  3. context: HookContext - Hook context (currently contains signal placeholder)
And must return a HookJSONOutput dictionary.

Example

HookMatcher

Configuration for matching and handling hook events.
str | None
Pattern to match against. For PreToolUse, this can be a tool name like "Bash" or a regex pattern like "Write|Edit|MultiEdit".See hook matcher documentation for details.
list[HookCallback]
List of callback functions to execute for this matcher.
float | None
Timeout in seconds for all hooks in this matcher (default: 60).

Example

HookInput Types

Strongly-typed input for each hook event. All hook inputs extend BaseHookInput.

BaseHookInput

PreToolUseHookInput

PostToolUseHookInput

PostToolUseFailureHookInput

UserPromptSubmitHookInput

StopHookInput

SubagentStopHookInput

PreCompactHookInput

NotificationHookInput

SubagentStartHookInput

PermissionRequestHookInput

HookJSONOutput Types

Output types for hook callbacks. Can be synchronous or asynchronous.

SyncHookJSONOutput

bool
Whether Claude should proceed after hook execution. Note: Use continue_ in Python (converted to continue for CLI).
bool
Hide stdout from transcript mode.
str
Message shown when continue_ is False.
Literal['block']
Set to "block" to indicate blocking behavior.
str
Warning message displayed to the user.
str
Feedback message for Claude about the decision.
HookSpecificOutput
Event-specific controls (see below).

AsyncHookJSONOutput

Defers hook execution. Use for long-running operations.

HookSpecificOutput

Event-specific output for fine-grained control.

HookContext

Context information passed to hook callbacks.
Currently a placeholder for future abort signal support.

Complete Example

Python uses async_ and continue_ (with underscores) to avoid keyword conflicts. These are automatically converted to async and continue when sent to the CLI.