Skip to main content

What Are Hooks?

Hooks are shell commands that execute automatically in response to agent lifecycle events. They enable:
  • Session initialization - Set up context at start
  • Tool interception - Validate or modify tool calls
  • Event reactions - Respond to specific actions

Hook Types

Session Hooks

Execute when a session starts or ends:

Tool Hooks

Execute before or after specific tool calls:

Configuration

Location

Hooks are configured in .claude/settings.json:

Schema

Event Types

Examples

Auto-Format on Write

Squad Status on Session Start

Git Commit Validation

Notify on Completion

Environment Variables

Hooks have access to context via environment variables:

Best Practices

  • Keep hooks fast (< 10 seconds typical)
  • Use timeouts to prevent hangs
  • Log hook failures for debugging
  • Test hooks in isolation before deploying
  • Use matchers to target specific tools
Common pitfalls:
  • Infinite loops (hook triggers itself)
  • Missing error handling
  • Blocking hooks that slow agent work
  • Hardcoded paths that break across machines

Debugging Hooks

Check Hook Execution

Test Commands Manually

Validate JSON Syntax

MCP Configuration

Connect external tools and services

Secrets Management

Handle sensitive configuration