Skip to content

Hooks API


Hook events

All events live in elsai.hooks:

python
from elsai.hooks import (
    AgentInitializedEvent,
    UserInputReceived,
    BeforeInvocationEvent,
    AfterInvocationEvent,
    BeforeModelCallEvent,
    AfterModelCallEvent,
    ModelStreamChunkEvent,
    BeforeToolCallEvent,
    AfterToolCallEvent,
    BeforeMemoryWrite,
    MessageAddedEvent,
    BeforeResponseDispatch,
    AgentErrorEvent,
)

Agent lifecycle events

AgentInitializedEvent

Fires once when Agent.__init__ completes.

AttributeType
agentAgent

UserInputReceived

Fires when new input arrives (before processing starts).

AttributeType
agentAgent
invocation_statedict
messageslist[Message]

BeforeInvocationEvent

Fires before the event loop starts for this turn.

AttributeWritableType
agentNoAgent
messageslist[Message] | None
invocation_stateNodict

AfterInvocationEvent

Fires after the event loop ends (even on exception).

AttributeWritableType
agentNoAgent
resultNoAgentResult | None
resumeAgentInput | None

Set resume to trigger another agent turn.

BeforeResponseDispatch

Fires just before the final AgentResult is returned.

AttributeWritableType
agentNoAgent
resultAgentResult
invocation_stateNodict

AgentErrorEvent

Fires when an unhandled exception occurs.

AttributeType
agentAgent
exceptionException
invocation_statedict

Model call events

BeforeModelCallEvent

AttributeType
agentAgent
messageslist[Message]

AfterModelCallEvent

AttributeType
agentAgent
responsedict
metricsMetrics

ModelStreamChunkEvent

Fires for each streaming chunk.

AttributeType
agentAgent
chunkdict

Tool call events

BeforeToolCallEvent

AttributeWritableType
agentNoAgent
selected_toolAgentTool
tool_useNodict

AfterToolCallEvent

AttributeType
agentAgent
toolAgentTool
responsedict

Memory events

BeforeMemoryWrite

AttributeWritableType
agentNoAgent
messageslist[Message]

MessageAddedEvent

AttributeType
agentAgent
messageMessage

HookProvider

python
from elsai.hooks import HookProvider, HookRegistry

class MyHooks(HookProvider):
    def register_hooks(self, registry: HookRegistry) -> None:
        registry.add_callback(BeforeModelCallEvent, self.on_before_model)
        registry.add_callback(AfterToolCallEvent, self.on_after_tool)

    def on_before_model(self, event: BeforeModelCallEvent) -> None:
        ...

    def on_after_tool(self, event: AfterToolCallEvent) -> None:
        ...

agent = Agent(hooks=[MyHooks()])

HookRegistry

python
registry = HookRegistry()
registry.add_callback(event_type, callback)
registry.add_hook(hook_provider)

Accessible via agent.hooks.


Registration patterns

python
# From type hint
agent.add_hook(lambda event: print(event), BeforeModelCallEvent)

# Inferred from type hint
def my_hook(event: AfterToolCallEvent) -> None:
    ...
agent.add_hook(my_hook)

# Multiple types
def multi(event: BeforeToolCallEvent | AfterToolCallEvent) -> None:
    ...
agent.add_hook(multi)

# List of types
agent.add_hook(my_hook, [BeforeModelCallEvent, AfterModelCallEvent])

Copyright © 2026 Elsai Foundry.