A hook is a rule the agent cannot talk its way past — a script the tool runs automatically at a fixed moment in the loop, able to inspect what the agent is about to do and, for some events, veto it. Skills are advice; hooks are a fence.
Read time7 min
LevelIntermediate
SectionBuilding Blocks
01 — what it is
A Rule the Agent Cannot Talk Its Way Past
A hook is a script that runs automatically at a fixed moment in the agent's loop — just before it runs a command, just after it writes a file, when a session starts, when it stops. The agent doesn't decide whether to run it; the tool does.
A hook can read what the agent is about to do and, for some events, veto it. Put plainly: a skill teaches the agent how to do something. A hook is a rule the agent cannot talk its way past. Skills are advice; hooks are a fence.
This distinction matters because prompting can be persuaded, reinterpreted, or forgotten under context pressure. A hook fires the same way every time, regardless of what the agent currently believes about the task.
02 — why hooks exist
Closing the Gap Prompting Can't Close
Prompting and context management make the agent more likely to do the right thing. They don't make it impossible to do the wrong thing. A well-prompted agent will still, occasionally, hardcode a secret, delete the wrong folder, or force-push over history.
Hooks are the enforcement layer under the Verification foundation: instead of catching the mistake in review, a hook catches it at the moment it's attempted, every time, without anyone watching.
Use a hook when a rule must hold deterministically — "never write a secret," "always format on save," "never touch the lockfile by hand." Use prompting or a skill when you want to guide judgement, not enforce a hard line.
03 — the three shapes
Two Mechanisms and One Reactor
Almost every useful hook is one of three shapes. Learn these and you can write any hook, not just the common ones.
text
Guard the command Before a shell command runs, read the command
string and veto the dangerous ones.
(Blocks rm -rf, force-push, curl | sh.)
Guard the write Before a file write, read the path and content
and veto protected files or secrets.
(Blocks .env edits, lockfile edits, hardcoded keys.)
React to the write After a file write, do something with it —
format it, lint it, log it. Never vetoes;
it responds.
The first two run on a "before" event and can say no. The third runs on an "after" event and cleans up. Events also bookend the session: session start/stop is where heavier one-time checks — loading context, running a full test suite — belong, since they shouldn't run on every keystroke.
A veto should carry a plain-English reason the agent reads, so it self-corrects instead of retrying the identical blocked action.
04 — best practices
Rules That Keep Hooks Trustworthy
Each practice below states the principle and the failure it prevents. These hold regardless of stack or language — the mechanism is generic.
✓
One mechanism per hook
A hook is "guard the command" or "guard the write," not a grab-bag — name it by its job so it stays testable and maintainable.
✗
Don't mix veto and formatting
One script that both blocks dangerous actions and reformats files becomes untestable and unreadable over time.
✓
Fast on hot events, heavy on cold ones
Anything on a "before every tool" event must be near-instant. Run the full test suite on session-stop, not on every write.
✗
Reactors must never block
A formatter failure should never halt the agent, and running it twice should change nothing the second time.
✓
A block must explain why
Emit a plain-English reason with every veto so the agent corrects course instead of retrying the same blocked action.
✗
Guards don't replace review
A hook stops the specific mistakes you encoded — not the ones you didn't think of. "The hooks would've caught it" is never a reason to skip reading the diff.
05 — references
Further Reading
Hooks are how the Verification foundation stops being something you do by hand and becomes something the project does automatically. Prompting guides the agent, context management informs it, hooks constrain it — all three, at every stage.