Overview
defineHook creates a hook that runs at specific points during agent execution.
Type Definition
Parameters
The hook type. Determines when the hook runs and what parameters it receives.
Async function that executes when the hook is triggered. Parameters vary by hook type.
Hook Types
Transformation Hooks
These hooks receive data, can modify it, and return the modified version.filter_messages
Runs before SQL rows are transformed into messages. Use to filter or modify raw message data.Current execution context
Raw message rows from the database
MessageRow[] - Filtered/modified rows
prefilter_llm_history
Runs before messages are sent to the LLM. Use to modify context or add dynamic data.Current execution context
Messages about to be sent to LLM
Message[] - Modified messages
before_create_message
Runs before a message is inserted into the database. Use to add metadata or modify content.Current execution context
Message about to be created
Message - Modified message
before_update_message
Runs before a message is updated in the database.Current execution context
ID of message being updated
Updates being applied
Partial<Message> - Modified updates
after_tool_call_success
Runs after a tool executes successfully. Can modify the result or convert to a different message type.Current execution context
The tool call that was executed
Result from the tool
ToolResult | null - Modified result or null to remove
after_tool_call_failure
Runs after a tool fails. Can modify the error or suppress the failure.Current execution context
The tool call that failed
The error that occurred
ToolResult - Modified error result
Event Hooks
These hooks run after an event and don’t return anything.after_create_message
Runs after a message is inserted. Use for logging, analytics, or webhooks.Current execution context
The created message
void
after_update_message
Runs after a message is updated. Use to track status changes.Current execution context
ID of updated message
The updates that were applied
void
Error Handling
All hooks are wrapped in error handling:- If a hook throws, the error is logged
- Execution continues with original data
- The framework doesn’t crash
File Location
Hooks are auto-discovered fromagents/hooks/:
- File name must match hook name
- One hook per file
- Default export required
Quick Reference
| Hook | Type | When It Runs |
|---|---|---|
filter_messages | Transform | Before SQL → Message conversion |
prefilter_llm_history | Transform | Before sending to LLM |
before_create_message | Transform | Before INSERT |
before_update_message | Transform | Before UPDATE |
after_create_message | Event | After INSERT |
after_update_message | Event | After UPDATE |
after_tool_call_success | Transform | After successful tool |
after_tool_call_failure | Transform | After failed tool |