Session 10
Automate actions with Hooks
Goal. Run your own shell commands automatically before or after Claude's actions — formatting, linting, or filtering output.
- 1Hooks run a shell command around Claude Code events. Common uses: auto-format after every file edit, run lint before a commit, or grep a huge log down to just the errors before Claude reads it.
- 2Configure them in settings.json — e.g. a
PreToolUsehook with aBashmatcher that filters test output to show only failures, cutting context from tens of thousands of tokens to hundreds. - 3Hooks make behavior deterministic where you need it — the command always runs; you're not relying on Claude to remember.
You'll see. Your formatter or linter firing automatically on every edit, with no reminder needed.
Cost. A pre-processing hook (e.g. grepping a log for ERROR) can slash the context Claude reads, directly lowering token cost.
Takeaway. Hooks turn "please remember to lint" into a guarantee — a shell command that always runs at the right moment.