الانتقال إلى المحتوى
Claude وأدوات الـ AI للبرمجةSkills و Hooks و Subagents· 7 دقائق للقراءة

Rules That Can't Bend Belong in Hooks, Not in Louder CLAUDE.md Lines

Claude reads CLAUDE.md and decides. Claude Code runs a hook before the action, every time, whatever Claude decided.

ReadIn

مقال كل يوم يفرق معاك

21 نوفمبر 2026
  • كاتب موثّق
  • Claude Code 2.x
محتويات المقال

A payments team's CLAUDE.md says "Never edit an existing migration; add a new one." Deep in a long session about a refund bug, Claude fixes a column type by editing db/migrations/0042_add_refunds.sql, which staging applied last week. The tests rebuild their database from scratch, so they pass. Staging never re-runs 0042, and its schema quietly drifts from what the code expects. The fix someone pushes is "IMPORTANT: NEVER EDIT AN EXISTING MIGRATION!!!". The next week Claude asks before creating any file in db/migrations/, even the new migration the task needs.

The common misconception

"A rule in CLAUDE.md is a rule, and if Claude broke it, it wasn't written strongly enough." Smart teams believe this for good reasons:

  • Claude does follow CLAUDE.md nearly every time, so the rare miss looks like a wording problem.
  • Emphasis used to help. Older models sometimes needed IMPORTANT and capital letters to notice a line.
  • is the documented home for project instructions and loads into every session, so it feels like configuration.
  • Adding a word takes five seconds. Writing a script takes an afternoon, or so it seems.

The half-truth is the first point. Claude really does follow the line almost always, and for most rules almost is exactly right. But as Anthropic's best-practices guide puts it, no matter how strongly you word a line, "whether Claude follows that line on a given turn is still Claude's call."

What actually happens

Two kinds of instructions

The docs sort everything you give Claude Code into two kinds. Non-deterministic instructions are read by the model, which then decides what to do with them: your prompt, CLAUDE.md, a skill. Deterministic ones are configured, and Claude Code, the harness around the model, "has to execute" them: hooks, deny rules, managed settings. The memory guide is just as direct: Claude treats CLAUDE.md "as context, not enforced configuration", and to block an action "regardless of what Claude decides", you use a PreToolUse hook instead.

Emphasis changes the weighting, not the guarantee

Capital letters don't move a line from the first kind to the second. They make Claude weigh it more heavily, and the docs warn that piling on emphasis "mostly makes Claude over-apply the rule": the team above traded a missed rule for a nervous one. The guide's own test: if you keep adding emphasis and Claude still misses the line, "that's a sign the line belongs in a hook."

A hook runs before the action

A is your command, attached to a point in Claude's loop. A PreToolUse hook fires after Claude has prepared a tool call and before Claude Code runs it. Claude Code pipes the call to your script as JSON on stdin: tool_name, and tool_input with fields such as file_path. Your exit code decides:

  • Exit 0: no objection. The call goes through the normal permission flow; exit 0 doesn't approve it.
  • Exit 2: the call is blocked, and whatever you wrote to stderr reaches Claude as the tool's error, so it can change course instead of retrying blindly.

A hook's deny holds in every permission mode, including bypassPermissions, and hooks from settings files also fire for tool calls made inside subagents. Claude doesn't have to remember the rule, because Claude isn't the one applying it.

The same rule, as a line and as a hook

Markdown CLAUDE.md
# Database- IMPORTANT: NEVER EDIT AN EXISTING MIGRATION!!!- ALWAYS add a new one instead. NO EXCEPTIONS.

The script holds the logic. "Existing" here means committed: a migration you've committed may already run somewhere, while a new file Claude is still writing stays editable.

Bash .claude/hooks/protect-migrations.sh
#!/bin/bash# PreToolUse hook: committed migrations are immutableset -e  # a missing jq exits non-zero instead of allowing the editFILE_PATH=$(jq -r '.tool_input.file_path // empty')FILE_PATH="${FILE_PATH//\\//}"  # normalize Windows separatorsif [[ "$FILE_PATH" == */db/migrations/* ]] &&  git -C "$CLAUDE_PROJECT_DIR" ls-files --error-unmatch \    "$FILE_PATH" >/dev/null 2>&1; then  echo "Blocked: $FILE_PATH is committed and may be applied." >&2  echo "Add a new migration: pnpm db:migrate:new <name>" >&2  exit 2fiexit 0  # no decision: the normal permission flow applies

Make it executable with chmod +x, then ask Claude to change the column in 0042. The edit never happens: Claude gets the "Blocked:" message as the tool's error and writes 0043 instead. CLAUDE.md keeps one plain line, "Schema changes go in a new migration: pnpm db:migrate:new <name>", so Claude does the right thing first and rarely meets the hook.

What a hook doesn't see

A hook enforces exactly what it matches, so know its edges:

  • A matcher on Edit|Write never fires for sed -i or a > redirect run through Bash. The docs suggest also matching Bash|PowerShell and listing changed files with git status --porcelain, or a Stop hook that scans the working tree once per turn.
  • Files you @-mention in a prompt are inserted without a tool call, so no PreToolUse hook fires for them. To keep a file out entirely, use a Read deny rule.
  • A PostToolUse hook can't undo anything, because the tool already ran. Blocking belongs in PreToolUse.
  • A prompt or agent hook asks another model to decide, so it isn't deterministic either. Real enforcement is a command or http hook.

Line, hook or deny rule

CLAUDE.md linePreToolUse hook
Who applies itClaude, when it judges the line relevantClaude Code, before every matching callClaude Code, on every matching call
ExceptionsClaude may decide one appliesOnly the ones your script allowsNone
Can check stateNoYes: git, files, anything a script can runNo: tool and path patterns
In bypassPermissionsStill just textA deny still blocksStill blocks
Best forGuidelines with exceptionsRules that need logic or a reasonPaths and commands that are off-limits

How to apply it in your project

  1. Find the shouting linesSearch your CLAUDE.md files for IMPORTANT, NEVER, ALWAYS and capital letters. Each one is a rule someone found hard to get followed.
  2. Sort each lineIf the rule has exceptions, rewrite it plainly and leave it to Claude. If it has none, it needs enforcement.
  3. Pick the mechanismA path or command that's simply off-limits becomes a deny rule in .claude/settings.json. A rule that needs logic or a redirect message becomes a PreToolUse hook. A step that must follow every edit becomes a PostToolUse hook.
  4. Make it fail closed and prove itAdd onFailure: "block", pipe sample JSON through the script, then ask Claude to break the rule and check the transcript with Ctrl+O.
  5. Keep the enforced set smallEvery hook and deny rule will occasionally fire when it shouldn't, and someone maintains each one. The docs' rule of thumb: at forty of them, most are preferences Claude could have weighed itself.

Key takeaways

  • CLAUDE.md is context Claude weighs; capital letters change the weighting, not the guarantee.
  • A PreToolUse hook runs before the tool call; exit 2 blocks it and sends your stderr to Claude.
  • A hook's deny holds in every permission mode and inside subagents.
  • Hooks see only what they match: cover Bash writes, and set onFailure: "block" so a broken hook fails closed.
  • Enforce the few rules with zero exceptions; leave guidelines in CLAUDE.md, written plainly.

📚 المصادر

  1. Automate actions with hooks — Claude Code Docs
  2. Hooks reference — Claude Code Docs
  3. Best practices for Claude Code — Claude Code Docs
  4. How Claude remembers your project — Claude Code Docs
  5. Configure permissions — Claude Code Docs

التعليقات (0)