CLAUDE.md Best Practices: A Template for Unattended Agents
CLAUDE.md best practices come down to three things. Keep the file short. Include only what Claude can't work out from the code. Write it so that a new agent with nobody around to ask can still finish the job. For unattended runs, that last point carries the most weight: the file should cover what to decide alone, what to stop on, and where to leave a note. If you only copy a style guide, the agent drifts, stalls, or keeps hitting permission denials overnight. Anthropic's own test for each line is: "Would removing this cause Claude to make mistakes?"
Which option is right for you?
Choose by how Claude will run, not by how big the project is. If you're unsure, start with the first row.
| Your situation | What to pick first | First page to open |
|---|---|---|
| Beginner, no CLAUDE.md yet | Run /init, then cut the draft down to commands, rules, and "never" lines | Best practices: Write an effective CLAUDE.md |
| Daily interactive work | A short project CLAUDE.md in git, plus skills for workflows you only need sometimes | Skills docs |
Nightly cron or claude -p job | The unattended template below, plus permission rules in settings | Headless mode |
CI with --bare | Pass the file yourself with --append-system-prompt-file | Bare mode section |
| Tight token budget | Move rarely used content into skills or @ imports that only load when needed | /claude-code-how-to-see-token-usage-and-reduce-it/ |
| Rules that must never break | A PreToolUse hook or a deny rule instead of a CLAUDE.md line | Hooks guide |
Here's why, one line each. /init gives you a draft quickly, but the draft is too long. Skills keep everyday sessions lean. Unattended runs can't ask you anything. --bare doesn't load CLAUDE.md at all. Every line in CLAUDE.md costs tokens on every run. And a hook is the only kind of rule that can't be talked out of.
What should a CLAUDE.md file example look like?
A good CLAUDE.md example is short and scoped. It covers project context, commands, conventions, and explicit rules for what the agent must never do unattended. It isn't a full style guide.
Anthropic's own sample is just two headings, "Code style" and "Workflow", with two bullets each. The docs say "There's no required format for CLAUDE.md files, but keep it short and human-readable." An unattended agent needs a few extra sections that an interactive user never thinks about, because the interactive user is the one answering questions.
Here is an example for a scheduled job. It's illustrative, so replace the commands and paths with your own:
# Project
Static blog generator (Node 22). Content in /content, build output in /dist.
# Commands
- Install: npm ci
- Test: npm test -- --run
- Build: npm run build (must exit 0 before any commit)
# Conventions
- ES modules only. No new dependencies without a note in REPORT.md.
# Autonomy: decide alone
- Fix failing tests caused by your own change.
- Retry a flaky network step once, then move on.
# Autonomy: stop and report
- Anything touching /migrations, .env, or CI config.
- A task that needs more than ~10 files changed.
- Any ambiguity you would normally ask about.
# When blocked
- Do not wait for an answer. Write the question and what you tried to BLOCKED.md, commit nothing, and end the run.
# Done means
- Tests pass, build exits 0, REPORT.md lists files changed and commands run.
# Compaction
- When compacting, always preserve the full list of modified files and any test commands.
Each section maps to a documented behavior. "Done means" comes from Anthropic's advice to give Claude a check it can run. Their wording: it's "the difference between a session you watch and one you walk away from." The compaction line is close to word for word what the best practices page suggests for keeping critical context through summarization. The "When blocked" section exists because a run started with --permission-prompts none removes tools that need a person, such as AskUserQuestion, according to the headless docs. Without that section, Claude has no planned route when it wants to ask you something.
Common pitfalls in examples:
- Putting permissions in prose. "Never run
rm -rf" in CLAUDE.md is a request, not a block. Use a deny rule or hook instead (see /claude-code-permissions-best-practices-headless-template/). - Describing the codebase file by file. Anthropic's exclude list names this directly, along with "Self-evident practices like 'write clean code'" (best practices).
- No definition of done. Without one, "Claude stops when the work looks done."
What are the core CLAUDE.md best practices?
Keep CLAUDE.md concise, include only information that changes decisions, and write it as onboarding for an agent that can't ask questions.
The reason is the context window. Anthropic says "Claude's context window fills up fast, and performance degrades as it fills," and CLAUDE.md loads at the start of every session. The memory docs add that Claude treats these files as "context, not enforced configuration." Every line costs tokens, and none of them is guaranteed to be followed.
Anthropic's include/exclude table (source), rearranged for unattended use:
| Include | Exclude |
|---|---|
| Bash commands Claude can't guess | Anything Claude can figure out by reading code |
| Code style rules that differ from defaults | Standard language conventions |
| Test instructions and preferred runner | Detailed API docs (link instead) |
| Branch naming and PR conventions | Information that changes frequently |
| Project-specific architecture decisions | Long explanations or tutorials |
| Required env vars and environment quirks | File-by-file descriptions |
| Non-obvious gotchas | "Write clean code" |
Steps to write or rewrite yours:
- In the project folder, run
claude, then type/initto generate a starter file (best practices). - Delete every line that fails the test "Would removing this cause Claude to make mistakes?"
- Add the autonomy, "when blocked", and "done means" sections from the example above.
- Run
/contextto confirm the file loaded (best practices). - For a checked-in file, run
/doctor, and Claude proposes cuts for content it can work out from the codebase (best practices). - Commit it to git.
Two failure signals tell you which way to fix the file. Anthropic puts it this way: "If Claude keeps doing something you don't want despite having a rule against it, the file is probably too long." If Claude asks about something the file already answers, the wording is probably ambiguous (best practices). Use emphasis sparingly. Mark only the one rule Claude keeps skipping with "IMPORTANT", because "If you emphasize many lines, none of them stands out."
What CLAUDE.md patterns do top GitHub repos use?
Popular GitHub repos usually use three CLAUDE.md patterns: a command-and-convention sheet, a behavior-principles file, or a shared team file updated after every mistake.
| Pattern | Public example | What it contains | Good for unattended runs? |
|---|---|---|---|
| Command and convention sheet | EDSL file in awesome-claude-code | Build/test commands, strict style (Black, mypy), testing requirements | Yes, if you add stop rules |
| Behavior principles | multica-ai/andrej-karpathy-skills | Four principles about assumptions, simplicity, edit scope, goals | Partly; it has no project commands |
| Living team file | Claude Code team's own file (Boris Cherny on X) | Corrections added whenever Claude gets something wrong | Yes; it grows from real failures |
A few structural habits show up again and again. One is splitting rules by directory or file type: Anthropic documents nested CLAUDE.md files and a .claude/rules/ folder for rules scoped to specific files (memory docs). Another is pointing to longer docs with @path/to/import instead of pasting them in (best practices). Imports help with organization. They don't make the file cheaper if the imported file loads every session anyway. If your repo already has an AGENTS.md, the memory docs say Claude "can also read a repository's AGENTS.md files in place of CLAUDE.md," so you don't need two copies.
Limitation: most public examples are written for someone sitting at the terminal. Hardly any say what the agent should do when it's stuck at 3 a.m., and that's the section you'll need to write yourself.
What does Anthropic's official guidance say about CLAUDE.md?
Anthropic's official guidance treats CLAUDE.md as a short, advisory memory file that loads every session. It covers commands, conventions, and gotchas, and you refine it like code.
The points that matter most for automation, all from Anthropic's docs as of October 2026:
- Scopes. CLAUDE.md can live at managed-policy, user (
~/.claude/CLAUDE.md), project (./CLAUDE.mdor./.claude/CLAUDE.md), and local (./CLAUDE.local.md) levels (memory docs). On a VPS, the user-level file belongs to whatever account the cron job runs as. That may not be the account you set up interactively. - Auto memory is separate. Claude writes its own notes, and only the first 200 lines or 25KB load each session (memory docs).
- Hooks vs instructions. "Unlike CLAUDE.md instructions which are advisory, hooks are deterministic and guarantee the action happens". To block an action no matter what Claude decides, the memory docs point to a PreToolUse hook (memory docs).
- Treat it like code. "Review it when things go wrong, prune it regularly, and test changes by observing whether Claude's behavior actually shifts" (best practices).
The headless page is where unattended setups most often go wrong. Adding --bare skips auto-discovery of "hooks, skills, custom commands, subagents, installed plugins, MCP servers, auto memory, and CLAUDE.md" (headless docs). The same page warns that "--bare is the recommended mode for scripted and SDK calls, and will become the default for -p in a future release." So a cron job that depends on CLAUDE.md being picked up automatically may stop following it after an update, with no error to tell you.
To make the file load whatever the default turns out to be:
- Keep the unattended rules in a file, for example
CLAUDE.mdor a separateAGENT_RULES.md. - Pass it explicitly. The headless docs list
--append-system-prompt-filefor adding to the system prompt in bare mode:
``bash claude --bare -p "Run the nightly content check" \ --append-system-prompt-file ./CLAUDE.md \ --allowedTools "Read,Edit,Bash(npm test *)" \ --permission-prompts none ``
- Remember that bare mode doesn't use your subscription login. Set
ANTHROPIC_API_KEYfirst (headless docs).
Two more defaults matter. With Claude Code v2.1.302 or later, claude -p starts in Manual mode when nothing sets a permission mode, and a call that needs approval is denied unless something approves it. --permission-prompts requires v2.1.259 or later (headless docs). This is why CLAUDE.md has to tell the agent what to do after a denial. The full flag set is covered in /claude-code-headless-mode-flags-auth-and-cost-2026/.
What do Reddit discussions say works (and fails) in CLAUDE.md?
The usual complaint on Reddit and developer forums is that long CLAUDE.md files get ignored. The fix people recommend is to trim the file to essentials and prune it regularly.
Plenty of community posts carry titles like "Why your long CLAUDE.md gets ignored, and how to fix it", and forum threads ask whether popular rule files actually help (Devtalk). Treat these as anecdotes, not measurements. The pattern does line up with Anthropic's own failure list, which names "The over-specified CLAUDE.md" and says important rules "get lost in the noise".
What community advice tends to get right:
- Add rules only after a real mistake. That matches the practice used by the Claude Code team (next sections).
- Turn rules into hooks. Anthropic's own fix for an over-specified file: "If Claude already does something correctly without the instruction, delete it or convert it to a hook."
What it tends to get wrong for unattended work:
- Shouting. Files full of "ALWAYS" and "NEVER" in capitals cancel themselves out, since emphasis only works on one line.
- Language and tone rules at the top. In a nightly job nobody reads the tone. Spend those lines on stop conditions instead.
For example, say your overnight agent keeps editing CI config even though a rule forbids it. Community advice would tell you to move the rule higher in the file. The more reliable fix is a deny rule or a PreToolUse hook on that path, with one line left in CLAUDE.md explaining why. Copy-paste hook scripts for this are in /claude-code-hooks-examples-7-copy-paste-scripts/.
What does Andrej Karpathy say about CLAUDE.md style files?
Karpathy didn't write a CLAUDE.md himself. The popular "Karpathy CLAUDE.md" is a community file built from his posts on how LLMs fail at coding, and it focuses on behavior rather than style.
The repo multica-ai/andrej-karpathy-skills showed about 223.8k stars when checked in October 2026. It turns his observations into four principles: Think Before Coding, Simplicity First, Surgical Changes, and Goal-Driven Execution. The line that matters most for unattended runs is the one the repo quotes from Karpathy: "Don't tell it what to do, give it success criteria and watch it go."
How to use it in a scheduled setup:
- Keep "Surgical Changes" and "Goal-Driven Execution". They limit drift and define when the agent is finished.
- Rewrite "Think Before Coding" for unattended runs. The original tells the agent to ask for clarification when a request is ambiguous. Overnight, nobody is there to answer, so change that to "write the question to BLOCKED.md and stop."
- Add your own commands. The file has no project-specific build or test lines.
Who it's not for: if your CLAUDE.md is already near the size where rules get lost, pasting in a generic principles file makes that worse. Take the two principles you need and leave the rest.
How is CLAUDE.md best practice evolving for 2026 (skills, headless agents)?
In 2026, CLAUDE.md is shrinking. Workflows are moving into skills, hard rules into hooks and permission settings, and the core file is turning into a statement of autonomy boundaries.
The split, according to Anthropic's docs as of October 2026:
| Content | Put it in | Why |
|---|---|---|
| Always-true commands, conventions, stop rules | CLAUDE.md | "CLAUDE.md is loaded every session, so only include things that apply broadly" |
| Occasional domain knowledge or workflows | .claude/skills/<name>/SKILL.md | Claude loads skills on demand (best practices) |
| Workflows with side effects | Skill with disable-model-invocation: true | Only runs when you trigger it (best practices) |
| Must-never-happen actions | Hooks, deny rules | Deterministic (hooks guide) |
| "Keep going until tests pass" | /goal condition or a Stop hook | Lets "an unattended run finish correctly without you" |
Skills also work headless. User-invoked skills run in -p mode if you include /skill-name in the prompt string (headless docs). A clean nightly setup looks like this:
- CLAUDE.md, about a screen long: commands, autonomy rules, "when blocked", "done means".
- A skill such as
/nightly-reportthat holds the step-by-step procedure. - Cron calls
claude -p "/nightly-report" --permission-mode dontAsk --allowedTools "...". UnderdontAsk, Claude Code "denies every call that would otherwise prompt" (headless docs). - A Stop hook runs your test command, so the run can't end while tests are red.
Auto mode is the other 2026 change. From v2.1.283 it's the starting permission mode for interactive terminal and VS Code sessions, with a classifier that blocks actions like scope escalation (best practices). It reduces prompts, but it doesn't replace stop rules. The classifier judges risk. It doesn't know your business rules, such as "never publish on Sundays". Scheduling details are in /claude-code-cron-job-setup-run-automations-overnight/.
What do practitioner write-ups (Medium, Boris Cherny) add to best practices?
Practitioners, including Claude Code creator Boris Cherny, add one habit: update CLAUDE.md after every mistake you catch. Keep the file small enough that the new rule doesn't get lost.
Cherny described the Claude Code team's setup on X in January 2026. They share one CLAUDE.md for the repo, check it into git, and "the whole team contributes multiple times a week". Whenever Claude gets something wrong, they add it to the file. In a follow-up post, he said he tags @.claude on coworkers' PRs to add CLAUDE.md lines through the Claude Code GitHub Action. For very long tasks, he mentioned having Claude verify its work or using a Stop hook (thread).
Adapted for unattended runs:
- Each morning, read the run's REPORT.md and BLOCKED.md.
- For each real mistake, decide on the fix. A one-line CLAUDE.md rule works if the mistake was a judgment call. A hook or deny rule is better if it must never happen again.
- Add the line in the same commit as the fix.
- Once a month, delete rules the agent now follows without being told.
Pitfall: a file that only grows. Adding a line after every mistake without ever pruning produces exactly the bloated file Anthropic warns about. A related mistake is reacting to a stalled run by adding more instructions. Stalls usually come from permissions or context, not missing rules. Check /why-claude-code-stops-mid-automation-and-how-to-fix-it/ before you edit the file.
Next steps
Do today (DIY):
- Open your project, run
/init(or open your existing file with/memory), and cut every line that fails "Would removing this cause Claude to make mistakes?" - Add four sections from the example: "Autonomy: decide alone", "Autonomy: stop and report", "When blocked", and "Done means".
- If your cron job uses
-p, add--append-system-prompt-file ./CLAUDE.mdnow, so the coming--baredefault doesn't silently drop the file.
If you'd rather not do it yourself: Anthropic's paid plans include Claude Code. Pro is $20/month ($17/month billed annually), and Max starts at $100/month (as of October 2026; check the official pricing page). Pro suits one person running light scheduled jobs. It's not a good fit if you need many long runs per night, because Claude Code "shares the same usage limits as the rest of your plan." Bare-mode runs bill through an API key instead, so they don't draw on a subscription. Anthropic also publishes a Claude Code 101 course on CLAUDE.md; check the page for current access terms. It suits beginners, but it won't write your stop rules for you.
Related guides on this site:
- /claude-code-permissions-best-practices-headless-template/
- /claude-code-hooks-examples-7-copy-paste-scripts/
- /claude-code-headless-mode-flags-auth-and-cost-2026/
- /claude-code-cron-job-setup-run-automations-overnight/
- /why-claude-code-stops-mid-automation-and-how-to-fix-it/
FAQ
Where can I find CLAUDE.md best practices examples?
Anthropic's best practices page has a short official sample and an include/exclude table. Community examples are collected in awesome-claude-code. The template in this guide adds the unattended sections most public examples leave out.
What are Anthropic's CLAUDE.md best practices?
Keep it short. Include only commands, rules that differ from defaults, and gotchas. Check it into git and prune it regularly. Anthropic also says CLAUDE.md is advisory, so anything that must always happen belongs in a hook (best practices).
Are there good CLAUDE.md best practices on GitHub?
Yes. The most-starred example is multica-ai/andrej-karpathy-skills, a behavior-principles file. Language-specific files such as the EDSL example in awesome-claude-code show the command-sheet pattern. Either way, add your own commands and stop rules.
What are the Karpathy CLAUDE.md best practices?
They're four principles taken from Karpathy's posts: Think Before Coding, Simplicity First, Surgical Changes, and Goal-Driven Execution. Karpathy didn't publish the file himself; the multica-ai repo did. For unattended runs, change "ask for clarification" to "log the question and stop".
Sources
- Best practices for Claude Code (Anthropic)
- How Claude remembers your project (Anthropic memory docs)
- Run Claude Code programmatically (headless docs)
- Hooks guide (Anthropic)
- Skills (Anthropic)
- Claude pricing
- Claude Code 101: The CLAUDE.md file (Anthropic Academy)
- Boris Cherny on X: shared CLAUDE.md
- Boris Cherny on X: tagging @.claude in PRs
- Boris Cherny on X: long-running tasks
- multica-ai/andrej-karpathy-skills (GitHub)
- awesome-claude-code EDSL CLAUDE.md (GitHub)
- Why your long CLAUDE.md gets ignored (DEV Community)
- Has anyone tried the Karpathy CLAUDE.md rules? (Devtalk)
- Writing the Best CLAUDE.md (DataCamp)
- Creating the Perfect CLAUDE.md (Dometrain)
Spotted something out of date or wrong? Tell us at shigoto.ai.note@gmail.com. Guides are re-checked against official sources on a rolling schedule.