Skip to main content
Hooks let you intercept agent execution at key points - before/after LLM calls, tool executions, and task completions.
Breaking change in PR #1558: execute_sync() cannot be called from within a running event loop. Use await runner.execute(event, input_data, target) instead in async contexts.Error message: execute_sync() cannot be called from within a running event loop. Use 'await runner.execute(event, input_data, target)' instead in async contexts.

Quick Start

1

Register Hooks

2

Create Agent

Alternative: HooksConfig


Hook Types

Available Hooks


Configuration


Common Patterns

Logging

Cost Tracking

Tool Validation


Multi-Agent Hooks

For multi-agent workflows, use MultiAgentHooksConfig:

Middleware

Middleware wraps the entire request/response cycle:

Async vs Sync Hook Execution

Choose the right execution method based on your context:

Migration Example


Best Practices

Hooks run synchronously. Heavy operations should be queued for async processing.
Wrap hook code in try/except to prevent hook failures from crashing the agent.
Logging, timing, and authentication are good middleware candidates.

Callbacks

Event-driven notifications

Guardrails

Output validation