strands.hooks.registry
Hook registry system for managing event callbacks in the Strands Agent SDK.
This module provides the core infrastructure for the typed hook system, enabling composable extension of agent functionality through strongly-typed event callbacks. The registry manages the mapping between event types and their associated callback functions, supporting both individual callback registration and bulk registration via hook provider objects.
BaseHookEvent
@dataclassclass BaseHookEvent()Defined in: src/strands/hooks/registry.py:25
Base class for all hook events.
should_reverse_callbacks
@propertydef should_reverse_callbacks() -> boolDefined in: src/strands/hooks/registry.py:29
Determine if callbacks for this event should be invoked in reverse order.
Returns:
False by default. Override to return True for events that should invoke callbacks in reverse order (e.g., cleanup/teardown events).
__post_init__
def __post_init__() -> NoneDefined in: src/strands/hooks/registry.py:49
Disallow writes to non-approved properties.
__setattr__
def __setattr__(name: str, value: Any) -> NoneDefined in: src/strands/hooks/registry.py:55
Prevent setting attributes on hook events.
Raises:
AttributeError- Always raised to prevent setting attributes on hook events.
HookEvent
@dataclassclass HookEvent(BaseHookEvent)Defined in: src/strands/hooks/registry.py:71
Base class for single agent hook events.
Attributes:
agent- The agent instance that triggered this event.
TEvent
Generic for adding callback handlers - contravariant to allow adding handlers which take in base classes.
TInvokeEvent
Generic for invoking events - non-contravariant to enable returning events.
HookProvider
@runtime_checkableclass HookProvider(Protocol)Defined in: src/strands/hooks/registry.py:89
Protocol for objects that provide hook callbacks to an agent.
Hook providers offer a composable way to extend agent functionality by subscribing to various events in the agent lifecycle. This protocol enables building reusable components that can hook into agent events.
Example:
class MyHookProvider(HookProvider): def register_hooks(self, registry: HookRegistry) -> None: registry.add_callback(StartRequestEvent, self.on_request_start) registry.add_callback(EndRequestEvent, self.on_request_end)
agent = Agent(hooks=[MyHookProvider()])register_hooks
def register_hooks(registry: "HookRegistry", **kwargs: Any) -> NoneDefined in: src/strands/hooks/registry.py:107
Register callback functions for specific event types.
Arguments:
registry- The hook registry to register callbacks with.**kwargs- Additional keyword arguments for future extensibility.
HookCallback
class HookCallback(Protocol, Generic[TEvent])Defined in: src/strands/hooks/registry.py:117
Protocol for callback functions that handle hook events.
Hook callbacks are functions that receive a single strongly-typed event argument and perform some action in response. They should not return values and any exceptions they raise will propagate to the caller.
Example:
def my_callback(event: StartRequestEvent) -> None: print(f"Request started for agent: \{event.agent.name}")
# Or
async def my_callback(event: StartRequestEvent) -> None: # await an async operation__call__
def __call__(event: TEvent) -> None | Awaitable[None]Defined in: src/strands/hooks/registry.py:136
Handle a hook event.
Arguments:
event- The strongly-typed event to handle.
HookRegistry
class HookRegistry()Defined in: src/strands/hooks/registry.py:145
Registry for managing hook callbacks associated with event types.
The HookRegistry maintains a mapping of event types to callback functions and provides methods for registering callbacks and invoking them when events occur.
The registry handles callback ordering, including reverse ordering for cleanup events, and provides type-safe event dispatching.
__init__
def __init__() -> NoneDefined in: src/strands/hooks/registry.py:156
Initialize an empty hook registry.
add_callback
def add_callback(event_type: type[TEvent], callback: HookCallback[TEvent]) -> NoneDefined in: src/strands/hooks/registry.py:160
Register a callback function for a specific event type.
Arguments:
event_type- The class type of events this callback should handle.callback- The callback function to invoke when events of this type occur.
Example:
def my_handler(event: StartRequestEvent): print("Request started")
registry.add_callback(StartRequestEvent, my_handler)add_hook
def add_hook(hook: HookProvider) -> NoneDefined in: src/strands/hooks/registry.py:182
Register all callbacks from a hook provider.
This method allows bulk registration of callbacks by delegating to the hook provider’s register_hooks method. This is the preferred way to register multiple related callbacks.
Arguments:
hook- The hook provider containing callbacks to register.
Example:
class MyHooks(HookProvider): def register_hooks(self, registry: HookRegistry): registry.add_callback(StartRequestEvent, self.on_start) registry.add_callback(EndRequestEvent, self.on_end)
registry.add_hook(MyHooks())invoke_callbacks_async
async def invoke_callbacks_async( event: TInvokeEvent) -> tuple[TInvokeEvent, list[Interrupt]]Defined in: src/strands/hooks/registry.py:204
Invoke all registered callbacks for the given event.
This method finds all callbacks registered for the event’s type and invokes them in the appropriate order. For events with should_reverse_callbacks=True, callbacks are invoked in reverse registration order. Any exceptions raised by callback functions will propagate to the caller.
Additionally, this method aggregates interrupts raised by the user to instantiate human-in-the-loop workflows.
Arguments:
event- The event to dispatch to registered callbacks.
Returns:
The event dispatched to registered callbacks and any interrupts raised by the user.
Raises:
ValueError- If interrupt name is used more than once.
Example:
event = StartRequestEvent(agent=my_agent)await registry.invoke_callbacks_async(event)invoke_callbacks
def invoke_callbacks( event: TInvokeEvent) -> tuple[TInvokeEvent, list[Interrupt]]Defined in: src/strands/hooks/registry.py:250
Invoke all registered callbacks for the given event.
This method finds all callbacks registered for the event’s type and invokes them in the appropriate order. For events with should_reverse_callbacks=True, callbacks are invoked in reverse registration order. Any exceptions raised by callback functions will propagate to the caller.
Additionally, this method aggregates interrupts raised by the user to instantiate human-in-the-loop workflows.
Arguments:
event- The event to dispatch to registered callbacks.
Returns:
The event dispatched to registered callbacks and any interrupts raised by the user.
Raises:
RuntimeError- If at least one callback is async.ValueError- If interrupt name is used more than once.
Example:
event = StartRequestEvent(agent=my_agent)registry.invoke_callbacks(event)has_callbacks
def has_callbacks() -> boolDefined in: src/strands/hooks/registry.py:297
Check if the registry has any registered callbacks.
Returns:
True if there are any registered callbacks, False otherwise.
Example:
if registry.has_callbacks(): print("Registry has callbacks registered")get_callbacks_for
def get_callbacks_for( event: TEvent) -> Generator[HookCallback[TEvent], None, None]Defined in: src/strands/hooks/registry.py:311
Get callbacks registered for the given event in the appropriate order.
This method returns callbacks in registration order for normal events, or reverse registration order for events that have should_reverse_callbacks=True. This enables proper cleanup ordering for teardown events.
Arguments:
event- The event to get callbacks for.
Yields:
Callback functions registered for this event type, in the appropriate order.
Example:
event = EndRequestEvent(agent=my_agent)for callback in registry.get_callbacks_for(event): callback(event)