Hooks Node
Hooks run scripts at specific points in your agentβs lifecycle.Overview
Purpose: Execute scripts on lifecycle events (before/after tool use, session start, etc.) Connection: Top-left handle of main agent card Color: Yellow Icon: πͺ Required: Optional (0 to many)What are Hooks?
Hooks are scripts (shell, Python, Node.js) that run automatically when events occur:- Before tool execution (logging, validation)
- After tool execution (notifications, cleanup)
- On session start (initialization)
- On user input (filtering, preprocessing)
Hook Types
10 Event Types
| Event | When It Runs | Use Case |
|---|---|---|
| PreToolUse | Before tool execution | Logging, validation, rate limiting |
| PostToolUse | After tool execution | Notifications, cleanup, metrics |
| UserPromptSubmit | On user input | Filter profanity, preprocess |
| PermissionRequest | On permission check | Custom approval logic |
| SessionStart | Agent session begins | Initialize connections, load context |
| SessionEnd | Agent session ends | Cleanup resources, save state |
| Stop | Agent stopped by user | Cancel operations, rollback |
| SubagentStop | Sub-agent stopped | Sub-agent cleanup |
| Notification | Status message | Log to external system |
| PreCompact | Before conversation trim | Archive conversation |
Hook Structure
Hooks are shell scripts that receive event data via stdin:Creating Hooks
Via Settings β Node Managers β Hooks:- Click βAdd New Hookβ
- Enter name (e.g., βTool Loggerβ)
- Select event type (e.g., PreToolUse)
- Write script (bash, Python, Node.js)
- Save to workspace library
Hook Examples
Example 1: Tool Call Logger (PreToolUse)
Purpose: Log all tool calls to fileExample 2: Cost Tracker (PostToolUse)
Purpose: Track API costs per toolExample 3: Session Initializer (SessionStart)
Purpose: Load user preferences on session startExample 4: Profanity Filter (UserPromptSubmit)
Purpose: Filter inappropriate user inputEvent Data Structure
Each hook receives JSON event data:PreToolUse Event
PostToolUse Event
SessionStart Event
Configuration
When clicked on canvas:- Edit hook script
- Select event type
- Configure environment variables
- Test hook execution
Best Practices
β Do:- Keep hooks fast (< 100ms)
- Handle errors gracefully
- Log to files, not stdout (except results)
- Use async operations when possible
- Test hooks thoroughly
- Block agent execution with slow hooks
- Modify global state unexpectedly
- Ignore errors
- Use hooks for complex logic (use skills instead)
Common Patterns
Pattern 1: Audit Logging
Pattern 2: Rate Limiting
Pattern 3: Cost Control
Security Considerations
β οΈ Important:- Hooks run with same permissions as agent
- Can access environment variables (including secrets)
- Validate hook scripts before use
- Avoid executing untrusted code
- Use hooks for read-only operations when possible
Next Steps
- Sub-agents Node - Specialized agents