Skip to main content
Hooks provide advanced control over the SDK’s behavior at different stages of execution. Use hooks to add custom logic, validate inputs, modify behavior, or stop execution based on conditions.

Hook Types

The SDK supports several hook types:
  • PreToolUse - Runs before a tool is executed (permission control)
  • PostToolUse - Runs after a tool completes (result validation)
  • UserPromptSubmit - Runs when a user prompt is submitted (context injection)
  • SessionStart - Runs when a session starts (initialization)

Basic PreToolUse Hook

Prevent specific bash commands from executing:

UserPromptSubmit Hook

Add custom context when prompts are submitted:
When matcher=None, the hook applies to all operations regardless of tool name.

PostToolUse Hook

Review tool output and provide feedback:

Permission Decision Hook

Use permissionDecision to explicitly allow or deny operations:

Stopping Execution

Use continue_=False to halt execution on critical errors:

Hook Response Format

Hooks return a HookJSONOutput dictionary with optional fields:

Multiple Hooks

You can attach multiple hooks to the same event:

Complete Example

Hook Execution Order

  1. SessionStart - When a new session begins
  2. UserPromptSubmit - When a user submits a prompt
  3. PreToolUse - Before each tool is executed
  4. PostToolUse - After each tool completes
Multiple hooks of the same type execute in the order they’re defined.

Key Takeaways

  • Use hooks for advanced control over SDK behavior
  • PreToolUse hooks control permissions before execution
  • PostToolUse hooks validate and respond to results
  • UserPromptSubmit hooks inject context
  • Use permissionDecision to explicitly allow/deny operations
  • Use continue_=False to stop execution on critical conditions
  • Configure hooks in the hooks parameter of ClaudeAgentOptions

Next Steps

Tool Permissions

Simpler permission control with callbacks

API Reference

Complete hooks API documentation