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
HookName
required
The hook type. Determines when the hook runs and what parameters
execute receives.string
required
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.Function
required
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.ThreadState
Current execution context
Message[]
Messages from storage
Message[] - Filtered/modified messages
prefilter_llm_history
Runs before messages are sent to the LLM.ThreadState
Current execution context
LLMMessage[]
Messages about to be sent to LLM
LLMMessage[] - Modified messages
before_create_message
Runs before a message is inserted into the database.ThreadState
Current execution context
Record<string, unknown>
Message about to be created
Record<string, unknown> - Modified message
before_update_message
Runs before a message is updated in the database.ThreadState
Current execution context
string
ID of message being updated
Record<string, unknown>
Updates being applied
Record<string, unknown> - Modified updates
before_store_tool_result
Runs before a tool result is stored in the database.ThreadState
Current execution context
Record<string, unknown>
The tool call that was executed
Record<string, unknown>
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.ThreadState
Current execution context
ToolCall
The tool call that was executed
ToolResult
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.ThreadState
Current execution context
ToolCall
The tool call that failed
ToolResult
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.ThreadState
Current execution context
Record<string, unknown>
The created message
void
after_update_message
Runs after a message is updated. Use to track status changes.ThreadState
Current execution context
Message
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)