The agent hooks starter kit (auto-format, auto-test, auto-block)
The short version
Every coding agent can run your own commands at fixed moments: when it starts, when it finishes, before it uses a tool, after it edits a file. Those are hooks. They are not AI. They are small shell commands you write once, and they run every single time, whether or not the agent remembered your instructions.
That is the whole point. A rule in your prompt is a suggestion the agent can drop halfway through a long session. A rule in a hook is a command that always fires. Formatting, linting, tests on every change, and a hard block on a dangerous command are all two-minute setups, and after that you stop babysitting.
Copy these prompts
Paste them into your coding agent, in this order. The first one is the one that changes your day.
1. Find the hook file and show me the moments
Find where hooks are configured for the coding agent I am using in this project, and show me the full list of moments a hook can fire on: session start, session end, before a tool runs, after a tool runs, and when you finish a turn. For each moment, tell me in one plain sentence what it is good for. Do not change anything yet.
2. Auto-format and auto-test on every file the agent touches
Add two hooks to this project. One: after you edit or write any file, run this project's formatter on it. Two: after you edit any source file, run the test command for that file only, and show me the failure if it fails. Use the project's real formatter and test commands, not generic ones. Then tell me exactly how to trigger each hook so I can watch it fire.
3. Block the dangerous command before it runs
Add a hook that runs before you execute any shell command and refuses the destructive ones: recursive force deletes, force pushes, dropping a database, and anything that rewrites git history. It must exit with the status code that blocks the call and prints the reason back to you, not just warn. Then show me it blocking a test command safely.
Hooks are the part of the agent people scroll past in the settings, and they are the part that changes how the work feels. The agent proposes; the hook enforces.
The moments, and what each one is actually for
| Moment | Fires when | Use it for |
|---|---|---|
| Session start | you open a session | print the branch, the current task, the state of the working tree |
| Before a tool runs | the agent is about to act | refuse a destructive command; refuse edits to files it should not touch |
| After a tool runs | the edit or command is done | format, lint, typecheck, run the one test that covers the change |
| Turn finished | the agent says it is done | run the real suite; say out loud whether it passed |
| Session end | you close it | write the session note, clean the scratch files |
The shape of a hook
A hook is a matcher plus a command. In a Claude Code project it lives in .claude/settings.json:
{
"hooks": {
"PostToolUse": [
{
"matcher": "Edit|Write",
"hooks": [
{ "type": "command", "command": "npm run format -- --write $CLAUDE_FILE_PATHS" }
]
}
]
}
}
That is the entire auto-formatter. No agent instruction, no reminder, no drift.
The blocking half is one detail: a PreToolUse hook that exits 2 stops the tool call and hands your message back to the agent as the reason. Any other non-zero exit is just noise in the log.
#!/usr/bin/env bash
# .claude/hooks/no-destructive.sh (chmod +x)
cmd=$(cat) # the tool call arrives on stdin as JSON
case "$cmd" in
*"rm -rf"*|*"push --force"*|*"DROP TABLE"*|*"reset --hard"*)
echo "Blocked by project policy. Ask me first." >&2
exit 2 ;;
esac
exit 0
The four I would set up first
- Format after every edit. Ends the "please run the formatter" line in your prompt forever.
- Test the file it just changed. Not the whole suite, the one file. Fast enough to run every time, which is the only reason it will keep running.
- Block the destructive command. The list is short and you know it already: force delete, force push, drop, hard reset.
- Full suite when the agent says it is done. This is the one that catches the confident wrong answer, because the agent has to read its own red output before you ever see the message.
Two things that bite
- A slow hook taxes every single turn. If the after-edit hook takes eight seconds, you pay eight seconds forever. Keep the per-edit hooks under a second and push the slow work to the turn-finished hook.
- A hook that always passes teaches you nothing. Prove each one by breaking something on purpose and watching it fire. An unverified hook is a comfort blanket, not a guardrail.
Steal this for your app
- Anything you have typed at your agent twice is a hook, not a reminder.
- Standards belong in commands, not in prose. Prose degrades over a long session; a command does not.
- Start with two hooks, not ten. Format and test. Add the blocker the first time the agent scares you.
Run this on your codebase
Paste this into an agent session in your repo:
Audit this project for hook coverage. Report before changing anything:
1. Which hooks are configured right now, at which moments, and what does each one run?
2. List the standards in my agent instructions that are enforced only by asking. Which of them could be a hook instead?
3. Time each configured hook. Flag any per-edit hook slower than one second.
4. Is there a hook that blocks destructive shell commands? If not, write it and show me it blocking a safe test case.
5. Name the one hook that would save me the most repeated typing, and add only that one.