Overview
LLM hooks are executed at two critical points:- Before LLM Call: Modify messages, validate inputs, or block execution
- After LLM Call: Transform responses, sanitize outputs, or modify conversation history
Hook Types
Before LLM Call Hooks
Executed before every LLM call, these hooks can:- Inspect and modify messages sent to the LLM
- Block LLM execution based on conditions
- Implement rate limiting or approval gates
- Add context or system messages
- Log request details
After LLM Call Hooks
Executed after every LLM call, these hooks can:- Modify or sanitize LLM responses
- Add metadata or formatting
- Log response details
- Update conversation history
- Implement content filtering
LLM Hook Context
TheLLMCallHookContext object provides comprehensive access to execution state:
Modifying Messages
Important: Always modify messages in-place:Registration Methods
1. Global Hook Registration
Register hooks that apply to all LLM calls across all crews:2. Decorator-Based Registration
Use decorators for cleaner syntax:3. Crew-Scoped Hooks
Register hooks for a specific crew instance:Common Use Cases
1. Iteration Limiting
2. Human Approval Gate
3. Adding System Context
4. Response Sanitization
5. Cost Tracking
6. Debug Logging
Hook Management
Unregistering Hooks
Clearing Hooks
Listing Registered Hooks
Advanced Patterns
Conditional Hook Execution
Context-Aware Modifications
Chaining Hooks
Best Practices
- Keep Hooks Focused: Each hook should have a single responsibility
- Avoid Heavy Computation: Hooks execute on every LLM call
- Handle Errors Gracefully: Use try-except to prevent hook failures from breaking execution
- Use Type Hints: Leverage
LLMCallHookContextfor better IDE support - Document Hook Behavior: Especially for blocking conditions
- Test Hooks Independently: Unit test hooks before using in production
- Clear Hooks in Tests: Use
clear_all_llm_call_hooks()between test runs - Modify In-Place: Always modify
context.messagesin-place, never replace
Error Handling
Type Safety
Troubleshooting
Hook Not Executing
- Verify hook is registered before crew execution
- Check if previous hook returned
False(blocks subsequent hooks) - Ensure hook signature matches expected type
Message Modifications Not Persisting
- Use in-place modifications:
context.messages.append() - Don’t replace the list:
context.messages = []
Response Modifications Not Working
- Return the modified string from after hooks
- Returning
Nonekeeps the original response
