Skip to main content

Rules versus Hooks

Why This Matters

A rule in CLAUDE.md is a request. Usually the agent follows it; sometimes, deep into a long session, it does not. A hook is a command that runs automatically at a fixed moment in the agent’s lifecycle. It does not depend on the model remembering anything. Anything that must always happen belongs in a hook or in CI.

Pre-commit checks first

Before agent-specific hooks, make sure the classic gates exist, because they protect against everyone, human or agent:

# package.json scripts
"lint": "eslint .",
"typecheck": "tsc --noEmit",
"check": "npm run lint && npm run typecheck && npm test"

Run npm run check in a git pre-commit hook (for example with husky or lefthook) and again in CI.

Agent hooks

Claude Code can run your commands at lifecycle events, configured in .claude/settings.json. The most useful events:

EventRunsTypical use
PreToolUseBefore a tool callBlock dangerous commands or edits to protected paths
PostToolUseAfter a tool callFormat or lint the file that was just edited
StopWhen the agent wants to finishRun the check; send it back to work if red
SessionStartWhen a session startsLoad extra context, check the environment

.claude/settings.json with a PostToolUse hook that runs eslint after Edit or Write, and a PreToolUse hook on Bash that runs a block-dangerous script

A hook that blocks dangerous commands

A PreToolUse hook receives the planned tool call as JSON on stdin. Exiting with code 2 blocks the call and sends your message on stderr back to the agent:

#!/usr/bin/env bash
# .claude/hooks/block-dangerous.sh
cmd=$(jq -r '.tool_input.command // ""')
if echo "$cmd" | grep -Eq 'git push|rm -rf|--no-verify'; then
  echo "Blocked by project policy: $cmd. Ask the user instead." >&2
  exit 2
fi
exit 0

The example repository implements its hooks in Node instead of bash and jq, so they run the same on macOS, Linux and Windows: see .claude/hooks/block-dangerous.mjs, protect-paths.mjs and lint-changed-file.mjs. It also blocks npm install <package>, because adding dependencies needs approval.

This is what the agent actually receives when the TestRun Board hooks block it (real output):

Real output of the TestRun Board hooks: git push and npm install dayjs are blocked with a reason, an edit to src/generated is blocked with the instruction to change docs/api.yaml, and an ESLint no-explicit-any error is sent back after an edit, each with exit code 2

Every message says what was blocked, why, and what to do instead. That last part matters: the agent reads it and usually adapts on the first try.

The same pattern protects test files during a fix task, blocks edits to src/generated/, or prevents reading .env files. Make the script executable and commit it with the settings.

Rules versus hooks: what goes where

Put it in a rule when…Put it in a hook (or CI) when…
It is guidance or styleBreaking it causes damage
Judgement is neededA script can decide it
An occasional miss is acceptableIt must hold every single time
Example: “prefer small functions”Example: “never push”, “lint must pass”

Keep hooks fast (a few seconds), deterministic and with clear error messages: the agent reads them and acts on them.

Hands-on

  1. Add a PostToolUse hook that runs your linter after Edit|Write.
  2. Add the block-dangerous.sh hook and test it by asking the agent to push.
  3. Pick one rule from your CLAUDE.md that was broken in the last week and turn it into a hook.

Walkthrough in TestRun Board

  1. Read .claude/settings.json: PreToolUse runs block-dangerous.mjs for Bash and protect-paths.mjs for edits; PostToolUse runs lint-changed-file.mjs after every edit.
  2. In a session, try each one:
    • “Push this branch.” → blocked: pushing is done by a human after review.
    • “Add dayjs to format the dates.” → npm install dayjs is blocked: dependencies need approval.
    • “Fix the type in src/generated/api-types.ts.” → blocked, with the instruction to change docs/api.yaml and run npm run generate:api.
    • “Add a helper that sums durations, typed as any[].” → the edit happens, then ESLint reports no-explicit-any and the agent fixes it.
  3. Test a hook without the agent, the way the course screenshots were made:
    echo '{"tool_input":{"command":"git push"}}' | node .claude/hooks/block-dangerous.mjs; echo "exit $?"
    
  4. Also look at .githooks/pre-commit: it runs npm run check on every commit (installed by npm install through the prepare script), for humans and agents alike.

Key Takeaways

  • Rules ask; hooks and CI enforce.
  • Start with classic pre-commit and CI gates; they cover humans and agents.
  • PreToolUse with exit code 2 blocks an action and tells the agent why.
  • Keep hooks fast, deterministic and clear in their messages.