Claude Code Hooks: Automate Lint, Tests and Git Across Every Session

Hooks are the glue between Claude Code and your shell. They fire on lifecycle events (PreToolUse, PostToolUse, Stop, UserPromptSubmit) and let you enforce policy on every agent action. Used right, they turn Claude Code from a helpful assistant into a disciplined team member.

Updated: April 24, 2026

When hooks fire

PreToolUse runs before a tool call and can cancel it (useful for blocking writes to .env). PostToolUse runs after a successful tool call, perfect for running lint or formatter. Stop runs when the agent thinks it has finished; you can gate it on tests being green.

Example: run eslint after every edit

Add a PostToolUse hook matching Edit|Write and run `npm run lint --silent`. Claude Code reads the hook output and, if lint fails, automatically retries the edit. Production-grade code without manual baby-sitting.

Example: block writes outside allowed paths

Use a PreToolUse hook matching Edit|Write that greps the target path against your allowlist. Any write to /infra, /.env or /.github is rejected with an exit code 2 so the agent can course-correct on the same turn. Essential for enterprise SaaS where some files require human review.

Hooks and B2B delivery

Our Alher Tech template ships with 12 default hooks: lint, test, type-check, no-secret-in-diff, conventional-commit, no-merge-to-main-without-PR, and a handful more. Every client repo inherits these so the agent behaves consistently across all active projects.

Frequently asked questions

Can hooks cost money?

Hooks run as local shell commands: zero tokens. They can save money by catching errors early and avoiding wasted agent turns.