What Are Hooks?

Hooks are shell commands that execute automatically in response to Claude Code events. They run before or after tool calls, giving you control over Claude's behavior without manual intervention.

Configure them in .claude/settings.json:

{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Bash",
        "command": "node ./hooks/validate-command.js"
      }
    ],
    "PostToolUse": [
      {
        "matcher": "Write",
        "command": "node ./hooks/lint-written-file.js"
      }
    ]
  }
}

Hook Events

The four hook events cover the full lifecycle:

  • PreToolUse — Before any tool executes. Use for validation, injection, or blocking.
  • PostToolUse — After a tool completes. Use for linting, formatting, or notifications.
  • SessionStart — When a new conversation begins. Use for context loading.
  • Stop — Before the session ends. Use for memory management or cleanup.

Practical Examples

Auto-Lint on File Write

Every time Claude writes a file, run the project's linter:

{
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "Write|Edit",
        "command": "npx eslint --fix $CLAUDE_FILE_PATH 2>/dev/null || true"
      }
    ]
  }
}

Memory Reminder on Session End

Prompt Claude to save important learnings before the conversation ends:

{
  "hooks": {
    "Stop": [
      {
        "command": "node ./hooks/memory-reminder.js"
      }
    ]
  }
}

Context Injection on Session Start

Load project-specific context at the beginning of every session:

{
  "hooks": {
    "SessionStart": [
      {
        "command": "node ./hooks/load-context.js"
      }
    ]
  }
}

Hook Security

Hooks run with your user permissions. Treat them like any other automation — review what they do, especially if they modify files or make network requests.

The matcher field supports regex patterns, so you can target specific tools or file patterns precisely.

Conclusion

Hooks bridge the gap between Claude's AI capabilities and your project's specific requirements. They're the mechanism that turns a generic assistant into a customized development environment.