Skip to main content
LLM Call Hooks provide fine-grained control over language model interactions during agent execution. These hooks allow you to intercept LLM calls, modify prompts, transform responses, implement approval gates, and add custom logging or monitoring.

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
Signature:

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
Signature:

LLM Hook Context

The LLMCallHookContext 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

  1. Keep Hooks Focused: Each hook should have a single responsibility
  2. Avoid Heavy Computation: Hooks execute on every LLM call
  3. Handle Errors Gracefully: Use try-except to prevent hook failures from breaking execution
  4. Use Type Hints: Leverage LLMCallHookContext for better IDE support
  5. Document Hook Behavior: Especially for blocking conditions
  6. Test Hooks Independently: Unit test hooks before using in production
  7. Clear Hooks in Tests: Use clear_all_llm_call_hooks() between test runs
  8. Modify In-Place: Always modify context.messages in-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 None keeps the original response

Conclusion

LLM Call Hooks provide powerful capabilities for controlling and monitoring language model interactions in CrewAI. Use them to implement safety guardrails, approval gates, logging, cost tracking, and response sanitization. Combined with proper error handling and type safety, hooks enable robust and production-ready agent systems.