Types of Execution Hooks
CrewAI provides two main categories of execution hooks:1. LLM Call Hooks
Control and monitor language model interactions:- Before LLM Call: Modify prompts, validate inputs, implement approval gates
- After LLM Call: Transform responses, sanitize outputs, update conversation history
- Iteration limiting
- Cost tracking and token usage monitoring
- Response sanitization and content filtering
- Human-in-the-loop approval for LLM calls
- Adding safety guidelines or context
- Debug logging and request/response inspection
2. Tool Call Hooks
Control and monitor tool execution:- Before Tool Call: Modify inputs, validate parameters, block dangerous operations
- After Tool Call: Transform results, sanitize outputs, log execution details
- Safety guardrails for destructive operations
- Human approval for sensitive actions
- Input validation and sanitization
- Result caching and rate limiting
- Tool usage analytics
- Debug logging and monitoring
Hook Registration Methods
1. Decorator-Based Hooks (Recommended)
The cleanest and most Pythonic way to register hooks:2. Crew-Scoped Hooks
Apply hooks only to specific crew instances:Hook Execution Flow
LLM Call Flow
Tool Call Flow
Hook Context Objects
LLMCallHookContext
Provides access to LLM execution state:ToolCallHookContext
Provides access to tool execution state:Common Patterns
Safety and Validation
Human-in-the-Loop
Monitoring and Analytics
Response Sanitization
Hook Management
Clearing All Hooks
Clearing Specific Hook Types
Unregistering Individual Hooks
Best Practices
1. Keep Hooks Focused
Each hook should have a single, clear responsibility:2. Handle Errors Gracefully
3. Modify Context In-Place
4. Use Type Hints
5. Clean Up in Tests
When to Use Which Hook
Use LLM Hooks When:
- Implementing iteration limits
- Adding context or safety guidelines to prompts
- Tracking token usage and costs
- Sanitizing or transforming responses
- Implementing approval gates for LLM calls
- Debugging prompt/response interactions
Use Tool Hooks When:
- Blocking dangerous or destructive operations
- Validating tool inputs before execution
- Implementing approval gates for sensitive actions
- Caching tool results
- Tracking tool usage and performance
- Sanitizing tool outputs
- Rate limiting tool calls
Use Both When:
Building comprehensive observability, safety, or approval systems that need to monitor all agent operations.Alternative Registration Methods
Programmatic Registration (Advanced)
For dynamic hook registration or when you need to register hooks programmatically:Performance Considerations
- Keep Hooks Fast: Hooks execute on every call - avoid heavy computation
- Cache When Possible: Store expensive validations or lookups
- Be Selective: Use crew-scoped hooks when global hooks aren’t needed
- Monitor Hook Overhead: Profile hook execution time in production
- Lazy Import: Import heavy dependencies only when needed
Debugging Hooks
Enable Debug Logging
Hook Execution Order
Hooks execute in registration order. If a before hook returnsFalse, subsequent hooks don’t execute:
Related Documentation
- LLM Call Hooks → - Detailed LLM hook documentation
- Tool Call Hooks → - Detailed tool hook documentation
- Before and After Kickoff Hooks → - Crew lifecycle hooks
- Human-in-the-Loop → - Human input patterns
