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
- Keep hooks lightweight - Hooks run synchronously, avoid heavy operations
- Use matchers - Only run hooks for relevant tools
- Return early - Return
allowquickly for non-matching cases - Log decisions - Log why hooks deny operations for debugging
- Handle errors - Wrap hook logic in try/except to avoid breaking agents
Related
- Agent - Using hooks with agents
- Tools - Tool system
- Guardrails - Output validation

