Skip to content

All posts

AI

Does CLAUDE.md enforce anything? Advisory rules vs enforced policy

Ritesh Patel

AI: Does CLAUDE.md enforce anything? Advisory rules vs enforced policy

No. CLAUDE.md is context that Claude reads at the start of each session, and Claude Code’s memory docs say it is not enforced configuration. Deny rules, sandboxing and PreToolUse hooks are the controls that act on a tool call. A CLAUDE.md rule can make Claude less likely to run a command. Only the controls below can stop one.

What the docs say CLAUDE.md does

CLAUDE.md files are the instructions you write for Claude. The memory page calls them “instructions you write to give Claude persistent context,” and says Claude reads them at the start of every session. They can live in three places: a managed file that IT deploys, a personal file under ~/.claude, and a project file in the repository. Claude Code concatenates every file it finds into context. One file does not override another.

The docs then draw the line that matters for this post: “Claude treats CLAUDE.md files and auto memory as context. They aren’t enforced configuration. To block an action regardless of what Claude decides, use a PreToolUse hook instead.” The rest of this post follows from those three sentences.

Advisory and enforced controls side by side

The table lists the Claude Code controls the docs describe for shaping or blocking a call. The first row only advises. The other rows act on the call itself, so a rule written there holds whatever the model decides to do.

Control Where it is set Advisory or enforced Stops a tool call?
CLAUDE.md (managed, user or project) Markdown files that load as context Advisory: “context, not enforced configuration” No
permissions.deny rules Settings files, including managed settings Enforced by Claude Code, “not by the model” Yes, for the tool or pattern named
Sandboxing sandbox settings Enforced for “filesystem and network enforcement that doesn’t depend on the command text” Yes, for the filesystem and network access it covers
PreToolUse hook hooks in a settings file Enforced when the hook exits with code 2 or returns a deny decision Yes, when the hook runs and blocks

Managed settings sit above the other levels. The permissions page says “no other level, including command line arguments, can override a managed permission rule.” That is why a control that must hold belongs in managed settings, and a note in CLAUDE.md should only point to it.

Why a CLAUDE.md rule does not hold

The permissions page is direct about where enforcement lives: “Permission rules are enforced by Claude Code, not by the model. Instructions in your prompt or CLAUDE.md shape what Claude tries to do, but they don’t change these rules.” A CLAUDE.md rule is therefore a request to the model. It works most of the time for clear instructions, and the docs say so: “the more specific and concise your instructions, the more consistently Claude follows them.”

The memory page also warns that if two instructions contradict each other, “Claude may pick one arbitrarily.” It suggests keeping each file under 200 lines, because longer files consume more context and reduce adherence. None of that adds a check. It only changes how likely the model is to comply.

The permissions page shows the same limit in its own example. Its CLAUDE.md line about allowed curl patterns “shapes what Claude tries but doesn’t enforce a boundary,” so the page tells you to pair that line with a control that does.

Make a rule hold with a deny rule

Write each must-never-happen decision as a tool call, not a sentence. “Never run destructive shell commands” becomes a Bash pattern. Then pick the lightest control that matches it. A deny rule matches a tool or a pattern, and a hook reads the full command and decides. The steps in this post are checked against the Claude Code docs as of 2026-11-24.

  1. Add a deny rule to .claude/settings.json for a tool or pattern. The docs evaluate rules in order, “deny, then ask, then allow,” so a deny rule wins over an allow rule for the same call. This example from the permissions docs allows npm and git commit and denies git push:
{
  "permissions": {
    "allow": [
      "Bash(npm run *)",
      "Bash(git commit *)"
    ],
    "deny": [
      "Bash(git push *)"
    ]
  }
}

Block a call with a PreToolUse hook

A hook is the control to reach for when the decision depends on the command text, such as “block any command that starts with rm.” The hook runs before the call, and its exit code decides the outcome.

  1. For a check on the command text, register a PreToolUse hook in the same settings file. The matcher names the tool, and the command points at a script in the project.
{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Bash",
        "hooks": [
          {
            "type": "command",
            "command": "${CLAUDE_PROJECT_DIR}/.claude/hooks/no-rm.sh"
          }
        ]
      }
    ]
  }
}
  1. Write the script to exit with code 2 to block the call. The script reads the command from stdin. It uses jq, as the docs’ Bash examples do, so install jq and check it is on your PATH.
#!/bin/bash
# .claude/hooks/no-rm.sh
input=$(cat)
command=$(jq -r '.tool_input.command' <<<"$input")
if [[ "$command" == rm* ]]; then
  echo "Blocked: rm commands are not allowed" >&2
  exit 2  # Blocking error: tool call is prevented
fi
exit 0  # No decision: the normal permission flow applies
  1. Test the block. Ask Claude to run a command the rule should stop, and confirm the call does not run. A block that you have not seen work is a guess.

Gotchas in the fine print

The docs contain several details that change whether a rule holds. Each one below comes from the page that states it.

  • Read deny rules cover Claude’s built-in file tools. The docs say they “don’t apply to a command that reads files without naming them, such as grep -r pattern . run from the directory that holds the file.”
  • A deny rule matches a program by the form written in the rule. It “doesn’t match the same program by path or inside sh -c,” so pair it with a sandbox network allowlist when a restriction must hold.
  • Exit code 1 does not block. A hook that exits with any code other than 0 or 2 is a non-blocking error. The hooks page says: “If your hook is meant to enforce a policy, use exit 2.”
  • A PreToolUse hook that times out does not block by default. The docs say “don’t count on a stalled hook to act as a gate,” and they point to onFailure set to "block" for the hooks that must stop a call. That field needs Claude Code v2.1.295 or later.
  • A managed CLAUDE.md is guidance. The memory page says to “enforce rules with managed settings such as permissions.deny.”

A checklist for rules that must hold

Split the policy in two. Guidance goes in CLAUDE.md, and every rule that must never break goes in a control the client applies.

  1. Keep CLAUDE.md for guidance you want the model to follow, such as code style, build commands and how to work in the repository. The memory page lists code style and quality guidelines as the content for a managed CLAUDE.md.
  2. For each action that must never run, add a deny rule for the tool or pattern, and check it with a command that should be refused.
  3. Where a decision depends on the command text, add a PreToolUse hook that exits with code 2, and test the exit path and the timeout path.
  4. Put organization-wide rules in managed settings. The docs say “settings rules are enforced by the client regardless of what Claude decides to do.”
  5. Test every control on the Claude Code version you deploy, and record that version next to the result, because the docs describe features by version.

Enforcing this with AIControls

This post makes no claim about AIControls. The controls above are set in Claude Code’s own settings files and hooks. For what the coding agents page covers, start there, and for the same argument applied to Cursor, read the Cursor rules vs enforced policy post. For the managed settings details, see the managed settings guide, and for which setting wins when two levels disagree, see the settings precedence post.

Frequently asked questions

Does a rule in CLAUDE.md stop Claude Code from running a command?

No. The Claude Code memory docs say Claude treats CLAUDE.md as context, not as enforced configuration. The rule shapes what Claude tries to do, so Claude will often follow it, but nothing in the client checks the command against it. A deny rule in settings, or a PreToolUse hook that exits with code 2, is what stops the call. Keep any rule that must never break in one of those controls.

Is a managed CLAUDE.md enforced on every developer machine?

It applies to every user on the machine, and a developer cannot exclude it with claudeMdExcludes. The docs still describe it as guidance. Their advice is to use the file for guidance and to enforce rules with managed settings, such as permissions.deny. If a developer starts a session with CLAUDE.md loading turned off, that session does not load the managed content either, so do not rely on it for a hard block.

Can a PreToolUse hook allow a command that a deny rule blocks?

No. The permissions docs say PreToolUse hook decisions do not bypass permission rules. Claude Code evaluates deny and ask rules whatever a PreToolUse hook returns, so a matching deny rule still blocks the call even when the hook returns allow. The reverse also holds for blocking: a hook that exits with code 2 stops the tool call before permission rules are evaluated.

What happens if a PreToolUse hook times out?

On PreToolUse, a timed-out command hook does not block the tool call unless the hook sets onFailure to block. The call then continues through the normal permission flow. The hooks docs advise not to count on a stalled hook to act as a gate. Set onFailure to block on any hook that must stop a call (the field needs Claude Code v2.1.295 or later), and test the timeout path on purpose before you depend on it.

Ritesh Patel, Founder & VP Product, Nirmata

Ritesh Patel is a founder of Nirmata and leads its products. He writes about governing AI coding agents, MCP security and Kubernetes policy as code.

LinkedIn

Govern AI agents with AIControlsControl what AI agents do, and prove it.
Share

Stay up-to-date with the latest in AI, governance, and infrastructure

Subscribe to our free monthly newsletter to get industry news, DevOps tips, and articles right to your inbox on Kyverno, containerization, Kubernetes deployment & management and more.