More on the topic...
Generating detailed summary...
Failed to generate summary. Please try again.
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.
Questions about this article
No questions yet.