Skip to main content
Hooks provide lifecycle callbacks that let you execute custom logic at key points during agent execution, enabling monitoring, logging, validation, and integration with external systems.

Overview

The Hooks primitive allows you to attach custom code to specific events in the agent lifecycle. Like React hooks or Git hooks, agent hooks let you intercept execution at critical moments to add custom behavior, logging, validation, or integration logic. Hooks are essential for:
  • Execution Monitoring: Track agent activity in real-time
  • Custom Logging: Send execution data to your logging infrastructure
  • Validation: Verify inputs and outputs meet your requirements
  • Error Handling: Implement custom error recovery logic
  • Metrics Collection: Track performance and usage metrics
  • External Integration: Sync agent activity with other systems

Lifecycle Events

Hook into all major agent lifecycle events from start to completion

Async Support

Hooks can execute async operations without blocking agent execution

Error Isolation

Hook failures don’t crash agent execution - they’re logged and isolated

Flexible Integration

Integrate with any logging, monitoring, or analytics platform

How Hooks Work

Available Hooks

Hooks are triggered at specific points in the agent lifecycle:
  1. onStart: Agent execution begins
  2. onThinking: Agent is reasoning about the task
  3. onToolUse: Agent calls a tool
  4. onToolResponse: Tool returns a response
  5. onProgress: Progress update (background tasks)
  6. onStep: Agent completes a step
  7. onComplete: Agent execution completes successfully
  8. onError: Error occurs during execution
  9. onCancel: Execution is cancelled

Hook Execution

Hooks execute asynchronously:
  1. Event Occurs: Agent lifecycle event happens
  2. Hook Triggered: Registered hook function is called
  3. Async Execution: Hook executes (can be async)
  4. Error Handling: Hook errors are caught and logged
  5. Continuation: Agent execution continues regardless of hook result
Non-Blocking: Hooks execute asynchronously and don’t block agent execution. Hook failures are logged but don’t stop the agent.

Code Examples

Basic Hooks

Logging Hook

Metrics Collection Hook

Validation Hook

External Integration Hook

Use Cases

1. Production Monitoring

Monitor agent health and performance:

2. Audit Logging

Maintain detailed audit trails:

3. Cost Tracking

Track and control costs:

4. Security and Compliance

Enforce security policies:

5. User Experience Enhancement

Improve user experience with real-time updates:

Best Practices

Hook Design

Performance Optimization

Integration with Other Primitives

With Traces

Hooks complement traces by adding custom logic:
Learn more: Traces Primitive

With Background Tasks

Monitor long-running background tasks:
Learn more: Background Tasks Primitive

With Evals

Add custom validation in evals:
Learn more: Evals Primitive

Performance Considerations

Hook Overhead

  • Hook Registration: < 1ms per hook
  • Hook Execution: Depends on hook logic (should be < 100ms)
  • Async Hooks: Don’t block agent execution
  • Error Handling: Failed hooks are logged but don’t stop execution

Optimization

Troubleshooting

Problem: Hook function not being calledSolutions:
  • Verify hook name is spelled correctly
  • Check hook is passed in hooks object
  • Ensure event actually occurs during execution
Problem: Hook failures stopping agentSolutions:
  • Wrap hook logic in try-catch
  • Log errors instead of throwing
  • Use error boundaries

Traces

Event streaming and execution monitoring

Background Tasks

Async task lifecycle events

Evals

Testing with custom validation hooks

Versioning

Version lifecycle callbacks

Additional Resources

API Reference

Hook parameters and events

Event Types

Complete event reference

Best Practices

Hook implementation patterns
Remember: Hooks are for observing and reacting to agent execution, not for modifying it. Keep hooks fast, handle errors gracefully, and use async operations wisely.