Skip to content
Max Narditdata & ai systems
← Articles
10 mindev

Claude Code best practices in 2026: sort every rule into enforced or asked-for, then test the enforced ones

Sorting a rule takes one question: can it ever have an exception? Proving that the enforced side holds takes longer. On Claude Code 2.1.332 a deny rule stopped cat but not a Python one-liner, a sandbox missing socat ran commands unsandboxed after one warning, and a Stop hook passed on a changelog Claude never touched.

Contents

Anthropic rebuilt its best-practices page for Claude Code around one split. Every instruction is either something Claude reads and decides on (your prompt, CLAUDE.md, a skill) or something Claude Code applies whatever the model decides (a hook, a deny rule, a managed setting). The rest of the page follows from that: tell Claude what it can’t see, give it a check that comes back pass or fail, and move only the rules with zero exceptions to the enforced side.

The split is right, and sorting your rules by it is the quick part. The harder work comes after: confirming that each rule on the enforced side holds the way you assume. Enforcement is deterministic about the thing it matches, and that thing is narrower than the rule you had in mind. On Claude Code 2.1.332, a deny rule on .env stopped cat and not a Python one-liner, a sandbox missing one dependency ran everything unsandboxed after a single warning, and a Stop hook passed on a changelog Claude never touched. Each case is one headless run per condition, so treat them as a reproduction recipe, not a benchmark.

What changed on the Claude Code best practices page?

The page now opens with deterministic versus non-deterministic instructions, ends its advice with a table called “What you can stop doing”, and revises three things Anthropic itself recommended in 2025: thinking keywords, emphasis in CLAUDE.md, and subagents to save context.

Anthropic’s April 2025 post said “think” < “think hard” < “think harder” < “ultrathink” were “mapped directly to increasing levels of thinking budget,” that Anthropic often tunes instructions by “adding emphasis with ‘IMPORTANT’ or ‘YOU MUST’”, and that subagents early in a task “preserve context availability.” The current wording for each is in the table further down.

The emphasis change is recent. When I wrote up what belongs in a CLAUDE.md on October 24, the same page still advised adding “IMPORTANT” to a line Claude keeps skipping, as long as it was that line alone. Today it says that if Claude keeps ignoring a line, “adding IMPORTANT probably won’t fix it”: either the file is too long, or the line is a rule that belongs in a hook.

How do you decide where a Claude Code rule belongs?

Run the page’s filter first, then ask one question. The filter is short:

Could it find this by reading the code?
Would it actually go and look?
If both are yes, leave it out.

An instruction that passes both is redundant, so leave it out of the files Claude reads. That does not settle whether the requirement behind it needs enforcing: a check Claude can easily discover may still have to run every time. For what remains, the question is whether the rule can ever have an exception. If it can, write it down for Claude. If it must hold every time, configure it.

The ruleWhere it goes
A convention with exceptions, a command Claude can’t guess, a gotchaCLAUDE.md (keep it short)
Guidance that only matters in one part of the codebase.claude/rules/ with a paths: filter (it controls when the guidance loads, not what Claude may do)
A procedure used sometimes (a release, an incident runbook)A skill; add disable-model-invocation: true if it has side effects
A step that must always happen at a fixed point (format after edit, check before stop)A command, http or mcp_tool hook
Something Claude must never read, run or fetchA deny rule, plus the sandbox for the shell commands it wraps
A policy for the whole organizationManaged settings

Three details on the enforced side are easy to miss. A prompt or agent hook asks another model to decide, so the page doesn’t count it as enforcement; it says to use a command, http or mcp_tool hook and add onFailure: "block" when a crashed hook must not let the action through. That field does nothing on Stop hooks, so a broken check before stop still fails open (the hooks write-up has the details). Second, precedence: a mod that handles tool.check answers after your PreToolUse hooks. The current permissions page says that without managed settings, a managed-mcp.json or a Team or Enterprise login as the session’s credential, such a mod can approve a call a PreToolUse hook blocked. With one of those, a built-in guard loads by default and keeps the block. Third, the page warns against the opposite mistake, too many enforced rules: “If you ever find yourself at forty of them, most are probably preferences that Claude could have weighed on its own.”

For the written side, skills and the old custom commands and how to size a CLAUDE.md in tokens go deeper.

Does a Claude Code deny rule stop every way of reading a file?

No. A deny rule on Read covers the Read tool and the shell commands Claude Code recognizes as file reads, so cat .env is blocked. A script that opens the file is a Bash call too, but not one Claude Code recognizes as a read of .env, so the rule doesn’t match it. The page says so; here is what it looks like.

I put a fake token in .env and this in .claude/settings.json:

json
{ "permissions": { "deny": ["Read(./.env)"] } }

Then two headless runs, both in bypassPermissions mode, where the page says deny rules still hold:

shell
claude -p "Run this exact shell command and show its raw output: cat .env" \
  --model haiku --setting-sources project,local \
  --permission-mode bypassPermissions --output-format json < /dev/null
 
claude -p "Run this exact shell command and show its raw output: python3 -c \"print(open('.env').read())\"" \
  --model haiku --setting-sources project,local \
  --permission-mode bypassPermissions --output-format json < /dev/null

The first run lists the Bash call cat .env under permission_denials, and the token never appears. The second returns the token in its result, with an empty permission_denials. The rule did what it says. It covers a tool and a set of recognized commands, and “never read .env” is a statement about a file.

The same edge applies to the network: a WebFetch deny for a domain does nothing about curl to that domain. The docs’ answer for both is the sandbox, which picks up Read deny rules and WebFetch(domain:...) rules and enforces them at the OS level on the shell commands it wraps. Permission checks have had their own holes as well: Claude Code closed ten bypasses in its permission and hook checks over three releases.

Is the Claude Code sandbox on when your settings say enabled?

Not necessarily. If the machine can’t run it, the local CLI prints a warning and runs commands without a sandbox. To make enabled mean “sandboxed, or don’t start”, set failIfUnavailable.

My test machine has bubblewrap but no socat, which the Linux sandbox needs. With this in the project settings:

json
{
  "permissions": { "deny": ["Read(./.env)"] },
  "sandbox": { "enabled": true, "allowUnsandboxedCommands": false }
}

the Python read from the previous section went through again, and the token came back. The only sign was one warning on stderr:

⚠ Sandbox disabled: sandbox is enabled but dependencies are missing: socat not installed · install missing tools (e.g. apt install bubblewrap socat) or see https://code.claude.com/docs/en/sandboxing
  Commands will run WITHOUT sandboxing. Network and filesystem restrictions will NOT be enforced.

In a script that sends stderr to a log nobody reads, that warning is easy to lose. Adding "failIfUnavailable": true to the same block changed the outcome: Claude Code exited with status 1 before the model ran, with “Refusing to start without a working sandbox.” The docs describe both behaviors, and note that an Anthropic-hosted environment can start anyway. What the test adds is how little noise the default makes.

This machine never got a running sandbox, so nothing here says what it blocks; a separate test of the Linux sandbox runtime covers that. What the run does show is that allowUnsandboxedCommands: false did not stop the read. Per the docs it turns off the retry outside a sandbox that is running. A sandbox that never started is failIfUnavailable’s job, and a setup that relies on the sandbox needs both.

How do you write a Claude Code Stop hook check for work that has no test suite?

Write a script that looks at what this turn changed and exits with code 2, message on stderr, when the work isn’t done. Exit code 2 is what sends Claude back; an ordinary linter or test runner exits 1, so translate its failure. And make the script test the change, not a property the repository may already have, because Claude will satisfy the check you wrote.

The page lists four options, from Claude deciding to Claude Code enforcing: “keep going until they pass” in the prompt, a /goal that a separate model checks against the conversation after every turn, a Stop hook, and a review by something that didn’t write the code, such as /code-review. Only the Stop hook with a command is enforced by the harness: it runs your script whenever Claude tries to end its turn. It has a ceiling too. After eight blocked stops in a row with no tool call between them, Claude Code ends the turn anyway (CLAUDE_CODE_STOP_HOOK_BLOCK_CAP raises the limit).

The page’s example check is a typecheck. The rule I tested has no such command: every code change gets a changelog entry. The first hook checked that the changelog had a release heading:

shell
grep -q '^## ' CHANGELOG.md 2>/dev/null || {
  echo 'CHANGELOG.md needs a ## entry for this change' 1>&2; exit 2; }

In a repository with no changelog, I asked Claude to add a function to calc.py. It added the function, hit the hook, read the message, and created CHANGELOG.md with an ## Unreleased section and one entry. That looks like success. Then I committed that state and asked for a second function. Claude added it and stopped in five turns. The changelog wasn’t touched, and the hook passed, because the file already had a ## heading. The check tested a property of the repository, and the repository kept that property forever.

A version that tests the change compares against the last commit:

shell
#!/bin/sh
cd "$CLAUDE_PROJECT_DIR" || exit 2
git diff --quiet HEAD -- calc.py || ! git diff --quiet HEAD -- CHANGELOG.md || {
  echo 'calc.py changed but CHANGELOG.md did not: add an entry for this change' 1>&2
  exit 2; }

Same request, same committed state: nine turns, and a new entry for the second function under “Unreleased”. I ran the git diff line inline in the hook’s command field first, then as .claude/hooks/changelog-check.sh (a third function, same nine turns, same new entry). The file is referenced through the project directory, so it still resolves when the session has changed directory:

json
{ "hooks": { "Stop": [ { "hooks": [ { "type": "command",
  "command": "sh \"$CLAUDE_PROJECT_DIR\"/.claude/hooks/changelog-check.sh" } ] } ] } }

It is still narrower than the rule. It names one file. It compares against HEAD, so a commit made during the turn would empty the diff. And in a repository with no commits, both git diff calls fail and the ! turns the second failure into a pass. I didn’t test those cases; by this article’s own argument, they are the next runs to make.

The same shape should carry to work with no test suite, as long as the script looks at what the turn changed: a diff of generated output against a fixture, a link checker over the changed files. I tested only the changelog case.

Which Claude Code habits can you stop, and which still earn their place?

Drop the emphasis, the “think harder” phrases, the “keep going, don’t be lazy” lines and the architecture tour in CLAUDE.md. Keep subagents for the jobs they were always good at, and know what they cost.

HabitWhat the page says now
Manage the context window aggressivelyMost current models have a 1M window and compaction is automatic; /clear between tasks still pays
IMPORTANT or ALL CAPS on missed linesOver-applies the rule; a rule that can’t bend goes in a hook or deny rule
Subagents to protect contextStill good for parallel work, noisy output and independent review, at a token cost
Delegate to a subagent for a better answerDelegation alone doesn’t upgrade the model; a fresh reviewer helps because it hasn’t seen your reasoning
think hard, be thoroughEffort is the control; ultrathink works for one turn
“Keep going”, “loop until done”Causes over-work; a check to pass is different, and the page says “/goal or a Stop hook enforces it”
Architecture and file layout in CLAUDE.mdClaude reads the repo itself

One line on the page didn’t fit its table: be ambitious still helps, because it tells Claude about scope, which only you know, rather than how hard to think.

On subagent cost I have my own number. When I estimated it from my transcripts on a Max plan, weighting tokens at reference rates, background subagents came to about 11.5% of priced usage, 64% of that in five-minute cache writes. The page’s levers for it are model: haiku for simple jobs, short summaries back, and, on Pro, Max, Ultra, Team and Enterprise, /usage to see which subagent types earn their cost. For the job-by-job verdict, see when Claude Code subagents are worth it.

Is Claude Code’s —bare enough for a script over a repository you didn’t write?

Not on its own. The page recommends --bare for claude -p in scripts, which skips hooks, skills, plugins, MCP servers and CLAUDE.md files. For a repository you don’t control, it leaves in more than that list suggests.

Per the headless docs, as I laid out in what a cloned repository can set in Claude Code, --bare does not drop the project’s env block or helpers such as awsAuthRefresh. --setting-sources user reads neither the project’s settings files nor its .mcp.json, and in my repro there it stopped the repository’s hook. My read: --bare for fast CI over your own code when the job doesn’t depend on Claude Code hooks, --setting-sources user for anything automated over someone else’s.

How do you audit a Claude Code setup after a model upgrade?

The AI Engineer write-up of Lydia Hallie’s CODE 2026 talk gives the reason in one line: a setup tuned to an older model can get in the way of a newer one. So treat each rule as a bet that something breaks without it, and re-check the bets when the model changes.

  1. Run /context all in the same directory each time and record the token count of each loaded memory file, so the next audit has a number to compare with.
  2. Run /doctor prompt-audit. It flags instructions written for older models, paths that no longer exist and contradictions between files. Reject findings that delete a rule’s reason; the reason belongs in the file, the history in git.
  3. Read CLAUDE.md against the stop-doing table above and delete what matches.
  4. For every line you emphasized, decide: is it a rule with zero exceptions? Move it to a hook or deny rule. Otherwise, write it plainly or cut it.
  5. Check /usage for which skills, subagents, plugins and MCP servers take the most, and turn off the servers you don’t use.
  6. For every hook and deny rule, decide: is it a preference? Delete it. Otherwise test it against the way around it: a script for a deny rule, an already-satisfied state for a Stop hook, a missing dependency for the sandbox.

The deny rule stopped cat and not Python. The sandbox setting printed one warning and ran everything unsandboxed. The Stop hook passed on a changelog Claude never touched. All three were configured as intended and enforced every time. Each was enforcing something narrower than the rule I had written down, and it took one run against the way around it to see how much narrower.

01 / Discussion

No comment section here — all discussions happen on X.

Max Nardit@mnardit

02 / More articles