Skip to main content
Execution Hooks provide fine-grained control over the runtime behavior of your CrewAI agents. Unlike kickoff hooks that run before and after crew execution, execution hooks intercept specific operations during agent execution, allowing you to modify behavior, implement safety checks, and add comprehensive monitoring.

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
Use Cases:
  • 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
View LLM Hooks Documentation →

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
Use Cases:
  • 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
View Tool Hooks Documentation →

Hook Registration Methods

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:
For typed tool outputs, tool_result is the string the agent sees. By default, this is JSON. If the tool uses custom formatting, it can be Markdown or another string. raw_tool_result is the original Python value returned by the tool.

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:
Note: For most use cases, decorators are cleaner and more maintainable.

Performance Considerations

  1. Keep Hooks Fast: Hooks execute on every call - avoid heavy computation
  2. Cache When Possible: Store expensive validations or lookups
  3. Be Selective: Use crew-scoped hooks when global hooks aren’t needed
  4. Monitor Hook Overhead: Profile hook execution time in production
  5. 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 returns False, subsequent hooks don’t execute:

Conclusion

Execution hooks provide powerful control over agent runtime behavior. Use them to implement safety guardrails, approval workflows, comprehensive monitoring, and custom business logic. Combined with proper error handling, type safety, and performance considerations, hooks enable production-ready, secure, and observable agent systems.