The hooks system enables extensible agent functionality through strongly-typed event callbacks.
- Paired events: Events that denote the beginning and end of an operation
- Hook callback: A function that receives a strongly-typed event argument
- Hook provider: An object implementing
HookProviderthat registers callbacks viaregister_hooks()
- All hook events have a suffix of
Event - Paired events follow
Before{Action}EventandAfter{Action}Event - Action words come after the lifecycle indicator (e.g.,
BeforeToolCallEventnotBeforeToolEvent)
- For every
Beforeevent there is a correspondingAfterevent, even if an exception occurs Afterevents invoke callbacks in reverse registration order (for proper cleanup)
Some events have writable properties that modify agent behavior. Values are re-read after callbacks complete. For example, BeforeToolCallEvent.selected_tool is writable - after invoking the callback, the modified selected_tool takes effect for the tool call.