Claude Code Best Practices: Anthropic's Checklist

Short answer
Use Claude Code well by giving it context, a pass-or-fail check and a few enforced rules. Put only facts it cannot see in CLAUDE.md. Give it a test, build or lint command and ask for the output. Use a hook, deny rule or managed setting when a rule cannot have exceptions. A hook runs an action automatically, a deny rule blocks listed actions, and a managed setting locks an organization policy.
Then steer the session. Watch the first minute, press Esc when Claude drifts, and run /clear after 2 failed corrections.
Imagine a developer whose Claude Code sessions end with “done” while tests still fail. Claude once edited a generated folder that the team never changes by hand. The problem is not a missing ALL CAPS warning. The instructions are in the wrong places.
Before you start
You need Claude Code and a repository with a test or build command.
- You will create a short
CLAUDE.md. - You will add
.claude/settings.jsonwith a Stop hook and deny rules. - You will finish with a simple steering routine.
Guidelines Claude weighs, rules Claude Code enforces
Anthropic separates written guidance from rules that Claude Code enforces. Claude reads and weighs prompts, CLAUDE.md and skills. Claude Code itself applies hooks, deny rules and managed settings every time.
Where each instruction belongs
Question | Claude weighs it (guideline) | Claude Code enforces it (every time) |
|---|---|---|
What should it do? | Prompt, | Hook when the action must happen |
What can it touch? | A plain-language boundary | Deny rule or managed setting |
When is it done? | Definition of done in the prompt | Stop hook that runs the check |
Source: Anthropic, Claude Code best practices, read 3 November 2026.
If Claude keeps missing an emphasized CLAUDE.md line, Anthropic says that line belongs in a hook.
Step 1: Tell Claude what it cannot see
Write down only what Claude cannot learn by reading the repository. A good prompt names the file, describes the symptom and says what fixed looks like. When you want a fuller version, Anthropic says “be ambitious” still helps.
If the goal is unclear, ask Claude to interview you. Save the result in SPEC.md, then start a fresh session so the build has clean context and a written definition of done.
Run /init for a starting CLAUDE.md, then remove anything the code already shows. Anthropic's memory guide recommends staying below 200 lines. Run /context to confirm the file loaded.
- Put personal preferences in
CLAUDE.local.mdand add it to.gitignore. - Put area-specific rules in
.claude/rules/with apaths:filter. - Put team procedures in
.claude/skills/<name>/SKILL.md. - Use a CLI when the outside system has one. Otherwise, connect it through MCP.
Claude only knows the facts you write down; in the same way, AI engines describe your company from published information, which Xtrusio tracks across ChatGPT and Gemini.
Step 2: Give Claude a check to run
Give Claude a command that returns pass or fail. If the repository already has one, Anthropic calls it the most useful line in CLAUDE.md.
You should see: one exact test, build or lint command in the repository instructions.
Ask for the result
Ask for evidence when the task ends: the command, its output or a screenshot. Run /code-review when you want a fresh subagent to review the diff without the conversation history.
You should see: the command and its pass-or-fail output at the end of the task.
Enforce it only when needed
A Stop hook can run the check before Claude stops. Exit code 2 blocks the stop.
You should see: a failed check return its output and keep the session open.
Step 3: Enforce rules that cannot have exceptions
Use a hook for an action that must run. Use a deny rule for files or commands Claude must never touch. Deny rules hold in every permission mode, including auto and bypassPermissions.
If something has to happen every time, make it a hook, a deny rule, or a managed setting.
Official wording: Anthropic, Claude Code best practices, read 3 November 2026, code.claude.com.
For an operating-system limit on files or network access, turn on the sandbox with /sandbox. Keep the enforced set small.
Our example: a starter setup for 1 repository
Our example (illustrative): StarterRepo.
StarterRepo runs tests and type checks before work is done. Its generated client folder is never edited by hand.
CLAUDE.md
Start with the checks and the repository facts that Claude cannot infer from the code.
## Checks
- Tests: npm test
- Type check: npm run typecheck
## Convention
- Put shared validation in src/validation/
## Gotcha
- generated/client/ is generated. Never edit it by hand.This file gives Claude the checks, 1 team convention and 1 repository gotcha.
.claude/settings.json
Then enforce the rule that must hold even when the model forgets a written guideline.
{
"hooks": {
"Stop": [{
"hooks": [{
"type": "command",
"command": "node .claude/hooks/run-tests-on-stop.mjs"
}]
}]
},
"permissions": {
"deny": ["Edit(generated/client/**)", "Write(generated/client/**)"]
}
}Save the guarded script as .claude/hooks/run-tests-on-stop.mjs:
import { execSync } from "node:child_process";
let input = "";
for await (const chunk of process.stdin) input += chunk;
const hook = JSON.parse(input);
if (hook.stop_hook_active) process.exit(0);
try {
execSync("npm test", { stdio: ["ignore", "pipe", "pipe"] });
process.exit(0);
} catch (error) {
const output = [error.stdout, error.stderr]
.filter(Boolean)
.map((part) => part.toString())
.join("");
process.stderr.write(output || "npm test failed\n");
process.exit(2);
}The Stop hook prevents a clean finish when the tests fail. With exit code 2, the script sends the test command's standard output and error output back to Claude as the reason it cannot finish, so it sees which tests failed. The deny rules protect the generated folder in every permission mode. Run /context to confirm Claude loaded the repository instructions.
When it does not work
Use these checks when the hook or deny rule behaves differently from what you expect.
- Claude keeps getting blocked: point the Stop hook to the guarded script above and confirm it exits successfully when
stop_hook_activeis true. - The hook never fires: check that
settings.jsonis inside.claude/at the repository root. - Claude still edits a protected folder: open
/permissionsand check the path pattern. CLAUDE.mdis missing in/context: check the file name and location.
Step 4: Steer the session
Watch the first minute. Press Esc as soon as Claude drifts. It stops the current action but keeps the work so far. After 2 failed corrections, run /clear and write a better prompt.
Use /clear between unrelated tasks. Run /usage when a task costs more time or tokens than expected. Anthropic says Claude Opus 5.5, Sonnet 5.5 and Haiku 5.5 default to medium effort.
For parallel sessions, start work in a new worktree with:
claude -w feature-authBefore you let Claude run on its own
Make sure Claude knows what done looks like and can check its work. Limit what it can touch. Set a cap on turns or spend, then have a person approve the result.
Tell Claude what it cannot see, give it a check to run, enforce the hard rules, and steer the session.

Advice you can stop following
Anthropic's current guide drops two habits that older guides still repeat.
Old advice | What to do now |
|---|---|
Describe the repository architecture in | Let Claude read the repository. Write only what it cannot find there. |
Add IMPORTANT or ALL CAPS to missed rules. | Write the rule plainly. If it must always hold, enforce it. |
Check what AI engines say about your company
Get your free AI visibility check to see where ChatGPT and Gemini mention your business and which competitors appear instead.
Frequently asked questions
How long should CLAUDE.md be?
Anthropic's memory guide recommends staying under 200 lines. Shorter is better when the repository already shows the answer.
When should a rule become a hook?
Use a hook when the action must happen every time or Claude keeps missing the written guideline.
How do I make Claude check its work?
Give it a test, build or lint command that returns pass or fail. Ask for the command and output as evidence.
What should I do when Claude drifts?
Press Esc immediately. After 2 failed corrections, run /clear and improve the prompt.
Which old tips can I drop?
Stop filling CLAUDE.md with repository architecture. Stop piling on ALL CAPS emphasis.
Sources
- Anthropic, Claude Code best practices, read 3 November 2026.
- Xtrusio product page, read 3 November 2026.
Topics
- claude code best practices
Xtrusio
AI visibility research
Xtrusio is the Perception Engine for B2B brands.
Keep reading
Technical SEO · 15 November 2026 · 5 min readBuilding AI Agents: A Plain Guide to Your First Agent
Build a bounded AI agent with one safe tool and run four local Python tests before you connect a real model.
Technical SEO · 15 November 2026 · 4 min readDuck.ai: How Private Is DuckDuckGo’s AI Chat?
See what Duck.ai shares with AI providers, how chat history and sync work, and what changes when you upload a file.
Technical SEO · 15 November 2026 · 7 min read