Don't rely on instructions, use Agent Hooks to enforce guardrails
The article shows how to use agent hooks in Claude Code to enforce strict, 100% reliable rules during code generation. It walks through a PreToolUse hook to block raw <input> tags and a Stop hook to prevent finishing until a design-system ratchet test passes.
- A PreToolUse hook can grep tool_input.content/new_string for banned patterns like raw `<input>` tags and exit code 2 to block the write entirely, guaranteeing consistent component usage instead of relying on ignorable CLAUDE.md instructions.
- A Stop hook can run a real test (e.g. a design-system ratchet test) before letting the agent end its turn, exiting 2 to force continued work if the test fails, preventing premature "done" claims.
- These hooks intercept actions at the exact moment they happen, making enforcement deterministic rather than dependent on written rules or later lint/CI catches.
- Silent failures are a real risk—an incorrect jq path returns null and lets bad code through unnoticed, so hooks must be validated against actual payloads.
Agent hooks let you slam the brakes on an AI agent’s actions the moment it’s about to do something you don’t want, instead of hoping it follows your written rules or catching mistakes in a later lint or CI step. Unlike git hooks that run at commit time, Claude Code exposes “PreToolUse” and “Stop” hooks you can wire into your .claude/settings.json so you intercept the JSON payload an agent sends every time it tries to write or finish its job. You get full access to the blob on stdin, inspect the fields with jq (or a Python script), and then exit with an error code to block actions or force more work.
For example, to ban raw <input> tags and enforce your <.cinput> component, you add a PreToolUse hook that grabs either .tool_input.content (for Write) or .tool_input.new_string (for Edit) and greps for “<input”. If it finds one, it writes an error to stderr and exits with code 2. That code tells Claude Code, “Not allowed—try again.” The agent never actually writes a file containing a raw <input>, so you’re guaranteed to get consistent component usage instead of reminders buried in CLAUDE.md that it’ll ignore half the time.
On the other end, you can attach a Stop hook that refuses to let the agent end its turn until a specific test passes. In the author’s case, they maintain a design_system_ratchet_test.exs. The Stop hook checks .stop_hook_active to avoid infinite loops, then runs mix test test/amplify_web/design_system_ratchet_test.exs. If that ratchet test fails, the hook exits 2 with an error message, forcing the agent to keep working. Unlike post-tool or CI checks, this only blocks at the exact moment the agent thinks it’s done—no more premature “All set!” announcements when your critical test is red.
A final warning: if your jq path is wrong, it returns null and your checks silently go nowhere. Always validate your hook against a real payload so you know .tool_input.content or .stop_hook_active matches what you expect. With just these two hooks in place, you get a deterministic guardrail that never lets your AI agent ignore the rules.