Hooks
Also: Claude Code hooks, PostToolUse hook, PreToolUse hook, lifecycle hooks
Hooks are shell commands that run at specific points in Claude Code's lifecycle — before a tool call, after a file edit, when you submit a prompt, or when Claude finishes a response. The key distinction: everything else in Claude Code is probabilistic (Claude will usually follow instructions). Hooks are deterministic — they always run, no exceptions. You can tell Claude in CLAUDE.md to run Prettier after every file edit. Most of the time it will. A hook makes it happen every single time. There are five events: PreToolUse (before a tool runs), PostToolUse (after a tool completes), UserPromptSubmit (when you submit a prompt, before Claude processes it), Stop (when Claude finishes responding), and Notification (when Claude sends a notification). For PreToolUse hooks, the exit code controls behavior: 0 = proceed normally, 2 = block the action (your stderr message gets fed back to Claude as context so it knows why and can adjust), anything else = non-blocking error shown to you but not blocking. This is how teams enforce hard rules: block writes to production config directories, block bash commands containing rm -rf, block direct commits to main. Configure hooks with /hooks inside a Claude Code session, or edit .claude/settings.json directly. Project-level hooks live in .claude/settings.json and should be checked into version control — your whole team gets them automatically. Use CLAUDE_PROJECT_DIR in hook commands to reference scripts in your project so they work from any working directory.
Related concepts