Skip to main content

Hooks Module

The hooks module provides a powerful hook system for intercepting and modifying agent behavior at various lifecycle points. Unlike callbacks (which are for UI events), hooks can intercept, modify, or block tool execution.

Installation

Features

  • Event-based hook system - BeforeTool, AfterTool, BeforeAgent, etc.
  • Shell command hooks - External integrations via shell commands
  • Python function hooks - In-process customization
  • Matcher patterns - Selective hook execution
  • Decision outcomes - allow, deny, block, ask

Quick Start (Simplified API)

The simplest way to use hooks:

Return Values

Classes

HookEvent

Enumeration of available hook events.

HookDecision

Possible decisions a hook can return.

HookResult

Result returned by a hook function.

Attributes

HookRegistry

Registry for managing hooks.

Methods

HookRunner

Executes hooks for events.

Event Input Types

BeforeToolInput

AfterToolInput

BeforeAgentInput / AfterAgentInput

Usage Examples

Basic Hook Registration

Logging Hook

Input Sanitization Hook

Shell Command Hook

Matcher Patterns

Best Practices

  1. Keep hooks lightweight - Hooks run synchronously, avoid heavy operations
  2. Use matchers - Only run hooks for relevant tools
  3. Return early - Return allow quickly for non-matching cases
  4. Log decisions - Log why hooks deny operations for debugging
  5. Handle errors - Wrap hook logic in try/except to avoid breaking agents