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
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