Quick Start
1
Register Hooks
2
Create Agent
Alternative: HooksConfig
Hook Types
Available Hooks
| Hook | When Triggered | Use Case |
|---|---|---|
BEFORE_LLM | Before LLM call | Modify prompt, log request |
AFTER_LLM | After LLM response | Log response, track tokens |
BEFORE_TOOL | Before tool execution | Validate args, log call |
AFTER_TOOL | After tool execution | Log result, transform output |
AFTER_AGENT | After agent completes | Cleanup, final logging |
Configuration
| Option | Type | Description |
|---|---|---|
on_step | Callable | Called on each execution step |
on_tool_call | Callable | Called when tools are invoked |
middleware | list | List of middleware functions |
Common Patterns
Logging
Cost Tracking
Tool Validation
Multi-Agent Hooks
For multi-agent workflows, useMultiAgentHooksConfig:
Middleware
Middleware wraps the entire request/response cycle:Best Practices
Keep hooks lightweight
Keep hooks lightweight
Hooks run synchronously. Heavy operations should be queued for async processing.
Handle errors gracefully
Handle errors gracefully
Wrap hook code in try/except to prevent hook failures from crashing the agent.
Use middleware for cross-cutting concerns
Use middleware for cross-cutting concerns
Logging, timing, and authentication are good middleware candidates.

