What are Hooks?
Hooks are optional functions that run automatically at specific points during agent execution. They allow you to modify data, perform side effects, and integrate with external systems without modifying the framework code.How Hooks Work
Hooks are functions that receive execution context (ThreadState) and data, then either:- Transform data and return modified version (transformation hooks)
- Perform side effects without returning anything (event hooks)
Defining Hooks
Hooks are defined usingdefineHook with a unique identifier:
Hook ID Requirements
- IDs must be unique across all hooks in the project
- IDs must be snake_case (lowercase letters, numbers, underscores, starting with a letter)
- IDs should be descriptive of what the hook does
defineHook:
- Strict typing: Parameters automatically typed based on hook type
- IntelliSense support: Full autocomplete in your editor
- Type checking: Catch errors at compile time
Hook Scoping
Hooks must be explicitly declared on prompts or agents to execute. Undeclared hooks do not run.Prompt-Level Hooks
Agent-Level Hooks
hooks defined.
Resolution Priority
- If the current prompt declares
hooks, only those hooks run - If the prompt has no
hooksbut the agent does, agent hooks run - If neither declares hooks, no hooks execute
Multiple Hooks of Same Type
You can define multiple hooks of the same hook type with different IDs:hooks array, they execute in the order listed:
Hook Types
Transformation Hooks
These hooks receive data, modify it, and return the modified version:after_system_message- Modify the rendered system message for the current requestfilter_messages- Filter message rows before transformationprefilter_llm_history- Modify messages before sending to LLMbefore_create_message- Modify message before database insertbefore_update_message- Modify updates before applyingbefore_store_tool_result- Modify tool result before storageafter_tool_call_success- Modify or remove successful tool resultsafter_tool_call_failure- Modify or remove failed tool results
Event Hooks
These hooks run after an event occurs and don’t return anything:after_create_message- After message insertedafter_update_message- After message updated
Common Use Cases
Message Filtering
Filter out unwanted messages before they’re sent to the LLM:External Logging
Send events to external analytics services:Tool Call Transformation
Convert tool calls to user messages:Message Enrichment
Add context to messages before storage:Sanitize Tool Results
Clean sensitive data from tool results before storage:Available Hooks Quick Reference
File Organization
Hooks are auto-discovered from theagents/hooks/ directory:
- File names can be anything (hook is identified by its
id) - Default export required
- Use
defineHookfor type safety - Multiple hooks of the same type are supported (each with a unique
id)
Error Handling
All hooks are wrapped in error handling that:- Catches exceptions without breaking execution
- Logs errors with
[Hooks] ✗prefix - Returns original data as fallback
Best Practices
Keep Hooks Fast
Keep Hooks Fast
Hooks run in the critical execution path. Keep them fast (< 100ms ideal):
Handle Errors Gracefully
Handle Errors Gracefully
Wrap risky operations in try-catch:
Validate Input Data
Validate Input Data
Check data structure before processing:
Scope Hooks Intentionally
Scope Hooks Intentionally
Only add hooks to prompts/agents that need them:
ThreadState Context
All hooks receive aThreadState object containing execution context:
- Check which agent is executing (
state.agentId) - Read invocation or subagent creation arguments (
state.arguments) - Access execution state (
state.execution?.stepCount) - Get/inject messages
- Emit events to frontend
- Access the file system
Next Steps
Hooks API Reference
Complete hooks specification
ThreadState
Learn about ThreadState interface
Tools
Create custom tools
Examples
See hooks in action