Overview
defineHook creates a hook that runs at specific points during agent execution. Each hook has a unique id and must be explicitly referenced by a prompt or agent to execute.
Type Definition
Parameters
The hook type. Determines when the hook runs and what parameters
execute receives.Unique identifier for this hook. Must be snake_case (lowercase letters, numbers, underscores, starting with a letter). Referenced by prompts and agents in their
hooks array.Async function that executes when the hook is triggered. Parameters are automatically typed based on the
hook type.Hook Scoping
Hooks must be referenced by a prompt or agent to execute:- If the current prompt declares
hooks, only those run - If the prompt has no
hooksbut the agent does, agent hooks run - If neither declares hooks, no hooks execute
Hook Types
Transformation Hooks
These hooks receive data, can modify it, and return the modified version.filter_messages
Runs before messages are transformed into chat completion format.Current execution context
Messages from storage
Message[] - Filtered/modified messages
prefilter_llm_history
Runs before messages are sent to the LLM.Current execution context
Messages about to be sent to LLM
LLMMessage[] - Modified messages
before_create_message
Runs before a message is inserted into the database.Current execution context
Message about to be created
Record<string, unknown> - 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
Record<string, unknown> - Modified updates
before_store_tool_result
Runs before a tool result is stored in the database.Current execution context
The tool call that was executed
The result to be stored
Record<string, unknown> - Modified tool result
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 result
ToolResult | null - Modified error result or null to remove
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
The updated message
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 names can be anything (hook is identified by its
id) - Default export required
- Multiple hooks of the same type are supported (each with a unique
id)
Quick Reference
| Hook | Type | When It Runs |
|---|---|---|
filter_messages | Transform | Before message transformation |
prefilter_llm_history | Transform | Before sending to LLM |
before_create_message | Transform | Before INSERT |
before_update_message | Transform | Before UPDATE |
before_store_tool_result | Transform | Before storing tool result |
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 |