Skip to main content
Here’s the thing about CLAUDE.md: it’s a suggestion. Claude usually follows it, but nothing enforces compliance. Hooks are different. Hooks are code — they execute automatically on specific events and can block, modify, or extend Claude’s behavior. If CLAUDE.md is a polite request, hooks are a locked door.
Official reference — For the full hooks API, event types, and configuration options, see the official hooks docs. This page gives you the five hooks worth setting up first.

The Essential Five

Run your linter after every file edit so Claude sees and fixes issues immediately.
{
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "Write|Edit",
        "command": "npx eslint --fix $CLAUDE_FILE_PATH 2>&1 || true"
      }
    ]
  }
}
Why: Catches style violations in real-time instead of accumulating them. Claude sees the linter output and self-corrects on the next edit.

Where to Configure

Hooks go in your settings files:
FileScope
.claude/settings.jsonTeam-shared (commit to git)
.claude/settings.local.jsonPersonal (gitignored)
~/.claude/settings.jsonAll your projects
Team hooks (linting, test runs, security blocks) go in .claude/settings.json. Personal preferences go in local or user settings.

Beyond the Essential Five

Official reference — For the full lifecycle event taxonomy, exit code semantics, matcher syntax, and HTTP/agent hooks, see the official hooks docs. Below is the quick map of what’s available.
Hooks fire on 12 lifecycle events — not just PreToolUse and PostToolUse:
EventWhen it firesUse for
SessionStartSession begins (new, resume, or post-clear)Load context, run diagnostics
UserPromptSubmitBefore Claude processes your messageSkill activation, input validation
PreToolUseBefore a tool executesBlock dangerous actions
PermissionRequestWhen Claude requests permissionAutomated approval/denial
PostToolUseAfter a tool executesAuto-lint, auto-format, run tests
PostToolUseFailureAfter a tool failsError reporting, fallback logic
SubagentStart / SubagentStopAgent lifecycleAgent monitoring, logging
StopBefore Claude finishes respondingEnforce completion conditions (all tests pass, all tasks done)
PreCompactBefore context compactionBackup critical state
SetupDuring initial setupOnboarding automation
SessionEndSession closesCleanup, transcript backup
Exit codes: 0 = allow, 1 = block the action, 2 = force Claude to continue (useful for Stop hooks that enforce quality gates). Setup hooks can run in deterministic mode (fast, CI-friendly) or agentic mode (supervised, with diagnostics). HTTP hooks POST events to endpoints instead of running local scripts — useful for centralized logging and remote validation.

Hook Design Principles

  • Fast hooks only. Hooks run synchronously — a slow hook slows every action. Keep them under 2 seconds.
  • Fail open for warnings, fail closed for security. Lint warnings shouldn’t block work. .env protection must.
  • Use || true for non-critical hooks. A crashing linter shouldn’t halt Claude’s workflow.
  • Test your hooks manually first. Run the command yourself before adding it as a hook.