ai-query.
Use hooks when you need to influence execution at stable boundaries. Use turn events when you only need to observe what is happening.
Hooks vs Events vs Lifecycle Overrides
Hooks are intentionally narrow and typed. They are not a full plugin system.
Available Hooks
AgentHooks currently supports:
before_stepafter_stepbefore_tool_callafter_tool_callon_reasoning_event
Configuring Hooks
The best DX for subclass-based agents is to override hook methods on the class:before_step
Runs before each provider request in a tool loop. ReturnStepControl to inject messages or stop before the next step begins.
after_step
Runs after a step completes. Use it for logging, status updates, and post-step policy.before_tool_call
Runs before a tool executes. ReturnBeforeToolCallResult(block=True, reason=...) to block execution.
after_tool_call
Runs after a tool finishes. ReturnAfterToolCallResult to override the tool result or terminate the current step batch.
- omitted fields preserve the original tool result
- returned fields replace the original value for that field
on_reasoning_event
Runs when provider reasoning/thinking output arrives.on_reasoning_event callback if you configured both.
Chump-Style Example
Best Practices
- keep hooks small
- use hooks for control and policy, not presentation
- use events for UI output
- prefer agent-level defaults and only override per turn when necessary
- avoid expensive work inside
before_stepandbefore_tool_call