Hooks for automation
Until now, every team rule — "run lint before commit", "run tests before push" — depended on memory: someone had to remember to run it every time, and under pressure it gets skipped. In this lesson we'll meet Hooks: scripts Claude Code runs automatically on a predefined event — before a specific too
Buildings have smoke detectors that trigger on their own the instant there's smoke, with nobody needing to remember to check every morning. The same way, you can set up a rule in advance that fires by itself at the right moment, never depending on anyone's memory.
- hook
- A script or shell command Claude Code runs automatically when a predefined event in its workflow occurs, like PreToolUse or SessionStart — never depending on the user's memory.
- PreToolUse
- A hook event that runs right before Claude Code actually invokes a given tool (like Bash). It can inspect the requested action and even block it before it runs.
- exit code 2 (block)
- When a hook script exits with code 2, Claude Code blocks the action that was about to run and receives the error message back, so it can fix the issue and retry.
- SessionStart
- A hook event that runs automatically once at the start of every new session — for example to load git status or project context, without anyone having to ask for it.