Building with agents, lesson 5.2
Hooks: lint gates, test runs, guardrails
An agent usually follows your instructions. A hook makes sure. This lesson shows the moments in a session where a small script can block an action, check an edit or refuse to let the work finish.
Every example below changes to fit what you pick. The businesses are made up for teaching.
The picture, one step at a time
Each box is one stage. The picture lights up as you scroll.
The moments in a session where a hook can run
1The session starts
A hook here can add notes for the agent to read. It cannot block anything.
2You send a message
A hook here runs before your message reaches the agent, and can stop it.
3Before a tool runs
A hook here sees the edit or command first, and can block it.
4After a tool runs
The action has happened. A hook here can tidy up, or tell the agent what is wrong.
5It says it is done
A hook here can run your checks, and send it back to work if they fail.
Five moments in a session where a hook can run.
Step 1 of 6
A rule it reads, or a rule that runs
Lines in CLAUDE.md are advice. The agent reads them and usually follows them, but nothing makes it. A hook is different. It is a small script that Claude Code itself runs at a set moment in the session, every time, whatever the agent decides.
Anthropic's docs say hooks make certain actions "always happen rather than relying on the LLM to choose to run them" (as of November 2026).
Step 2 of 6
Before a tool runs: the guardrail
A hook here sees the action first: which file it is about to edit, or which command it is about to run. Your script checks it against your list. To say no, the script ends with exit code 2 (a number a script hands back when it finishes) and writes its reason. The action is blocked, and the agent reads the reason and changes course.
Your guardrail script ends with exit code 2 when the agent tries to edit the payment keys file. What happens?
Step 3 of 6
After a tool runs: the lint gate
A linter is a program that checks code for mistakes and messy style. A hook after each edit runs the linter, or a formatter, on the file just changed. The edit has already happened, so this hook can't undo it. But if it ends with exit code 2, its message goes to the agent, which fixes the problem next. Keep it fast, because it runs after every edit.
Anthropic's hooks guide gives this as its first example: run the formatter on every file the agent edits.
Step 4 of 6
When it says it is done: the test run
An agent stops when the work looks done. A hook at this moment runs your tests or type check first. If they fail, exit code 2 sends it back to work. So for the checkout, "done" now means the checks passed, not that it looked finished.
Claude Code lets the agent stop anyway if this hook blocks eight times in a row with no work in between. Your script is told when it has already sent the agent back, so it can avoid a loop (as of November 2026).
Step 5 of 6
At the start, and before your message
Two more moments. A hook when the session starts can print notes the agent reads, such as reminders after a long chat has been squeezed down to save space. A hook when you send a message runs before the agent sees it, and can stop it. Both are useful, and both are easy to overdo.
Step 6 of 6
Only for rules with no exceptions
A hook has no judgement. It does the same thing every time, so use it only for never and always. Hooks go in a settings file: .claude/settings.json in the repo is shared with your team. One gap to know: a hook on edits does not see a shell command that changes the same file, so add a deny rule, a setting that refuses that file outright.
Anthropic's docs: if you keep adding emphasis to a line in CLAUDE.md and it is still missed, it belongs in a hook. If you have hooks for mere preferences, delete them.
Which of these should be a hook?
Which moment does this rule need?
Here are four rules for the agent in your business. Sort each one into the moment a hook should run, or leave it as advice.
1Tidy the layout of every file it edits, the way your formatter likes it.
2Never touch the file that holds the live payment keys.
3Do not finish until the checkout tests pass.
4Keep product descriptions short.
0 of 4 right so far, 4 to go.
Four hook scripts
This is what you keep: what each of the four scripts does, in plain words, ready to write for your own repo.
Four hook scripts
- The guardrail: before any edit, check the file against a list of protected files. If it matches, end with exit code 2 and say why.
- The command block: before any shell command, refuse ones that delete whole folders or wipe data, with exit code 2 and a reason.
- The lint gate: after any edit, run the formatter and linter on that one file, and pass any failure back to the agent.
- The test run: when it tries to finish, run the type check and the tests. If they fail, exit code 2 sends it back, unless it has already been sent back.
- Put all four in the shared settings file, add deny rules for protected files, and test each one by asking the agent to break the rule.
Make a hook only for a rule with no exceptions. Everything else stays a line in CLAUDE.md.
Next: Lesson 5.3: Subagents: planner, implementer, reviewer. How to split one job across helpers that each start with a clean slate.