Skip to main content

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:
  1. Click “Add New Hook”
  2. Enter name (e.g., “Tool Logger”)
  3. Select event type (e.g., PreToolUse)
  4. Write script (bash, Python, Node.js)
  5. Save to workspace library
Hook appears in sidebar → drag to canvas.

Hook Examples

Example 1: Tool Call Logger (PreToolUse)

Purpose: Log all tool calls to file
Use case: Audit trail of agent actions

Example 2: Cost Tracker (PostToolUse)

Purpose: Track API costs per tool
Use case: Monitor spending

Example 3: Session Initializer (SessionStart)

Purpose: Load user preferences on session start
Use case: Personalized agent behavior

Example 4: Profanity Filter (UserPromptSubmit)

Purpose: Filter inappropriate user input
Use case: Content filtering

Event 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
❌ Don’t:
  • 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

Use case: Compliance, debugging

Pattern 2: Rate Limiting

Use case: Prevent API abuse

Pattern 3: Cost Control

Use case: Budget management

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