{"slug": "how-to-write-a-good-claude-md-file", "title": "How to Write a Good CLAUDE.md File", "summary": "A practitioner guide recommends keeping each Claude Code CLAUDE.md file to one rule per line and under 200 lines, citing Anthropic's official best practices that every line in the file consumes context on every task and that longer files reduce adherence. The guide describes cutting a web shop's roughly 300-line CLAUDE.md in about an hour by moving architecture and folder descriptions out (the agent reads them from the repository), relocating API conventions to .claude/rules/api.md, converting a twelve-step release checklist into a skill, and turning a no-exceptions rule into a hook. It also warns that @path imports do not reduce context cost because imported files load together with the importing CLAUDE.md, and that piling on emphasis makes Claude over-apply a rule.", "body_md": "Every coding agent has a file it reads at the start of each session. In Claude Code, it’s CLAUDE.md. Other agents read a similar file, often called AGENTS.md.\n\n*I am an engineer, not a writer, and English is not my first language. The ideas, experiments, code and content of this story are mine. I use AI to help me rephrase them.*\n\nAnd because every session reads it, every lesson wants to go there! You correct the agent, and the correction becomes a line. Then the line gets a story, an example, a warning in capitals. A few weeks later, the file is several hundred lines long. And the rule that matters today? Lost between two paragraphs about code you are not touching.\n\nSound familiar? Today’s fix is very satisfying: CLAUDE.md keeps one rule per line, and the detail lives in files it points to.\n\nThe official best practices explain the cost. Claude Code loads CLAUDE.md into every session, \"which means every line in it takes a little of Claude's attention on every task, whether it's relevant or not.\"\n\nEvery line, every task, relevant or not! A long file does not only cost tokens. It makes each rule easier to miss.\n\nAnd CLAUDE.md is not configuration. The memory guide says Claude treats these files \"as context, not enforced configuration, so how you write instructions affects how reliably Claude follows them.\" A short line in a short file is easy to follow. A rule inside a long paragraph competes with everything around it.\n\nThe guide even gives a target: “target under 200 lines per CLAUDE.md file. Longer files consume more context and reduce adherence.” But one rule written as a 150-word paragraph is still one line! So count words too.\n\nWrite each rule as one plain sentence. Keep only what the agent cannot work out from the code. The best practices page lists four kinds of lines that pass that test:\n\nYour architecture? Your folder layout? The agent reads them from the code. The best practices page even lists “Describe your architecture and file layout in CLAUDE.md\" among the advice you can stop following. I love that table.\n\nAnd number the rules. Then a detail file or a commit can cite “rule 4” without copying it. And never renumber.\n\nThe rest has four places to go.\n\nWhy does the split work? The root CLAUDE.md is loaded in full at the start of every session. Files in subdirectories and path-scoped rules load only when the agent works there. So a session on the billing code never pays for the rules about the mobile app. And a plain link costs one line, until the agent follows it.\n\nNow a surprise. An @path import looks like a great way to move text out of the file, right? It is not! The memory guide says imports \"help you organize a long file but don't reduce its context cost, because imported files load together with the CLAUDE.md that imports them.\" Use an import for a file every session really needs. Use a link for detail.\n\nCapitals? The best practices page says current models “follow a plainly worded line, and piling on emphasis mostly makes Claude over-apply the rule.” If a line keeps being missed, either the file is too long, or the line is a rule with no exceptions. And then it belongs in a hook, not in capitals.\n\nA web shop with an API and a small admin front end. Its CLAUDE.md had grown to about 300 lines: the architecture and the folders, the history of three past bugs, a long section on API conventions, a release checklist of twelve steps, and a few lines in capitals that said NEVER.\n\nThe cut took an hour. The architecture and the folders went: the agent reads them in the repository. Each rule became one numbered sentence, and its old text moved, unchanged, to docs/rules_in_detail.md. The API conventions moved to .claude/rules/api.md. The release checklist became a skill. And the one rule that could not have exceptions became a hook.\n\nHere is the new root file. It goes at the root of your repository:\n\nIt fits on one screen! And all the rules are still there. The comment at the top is for humans: Claude Code strips HTML comments before reading.\n\nThe detail file it links to, docs/rules_in_detail.md:\n\nAnd a rule scoped to one part of the code, in .claude/rules/api.md:\n\nThe prompt that does the cut. Run it on a branch, then read the diff of the detail file:\n\nHere is the sad truth: a cut file grows back, one paragraph at a time! So let a script watch it. It flags long lines, capitals, folder trees, paths that don’t exist anymore, and rule numbers used twice. Save it as scripts/check_claude_md.py:\n\nThe path check is my favorite. A rule that names a deleted file sends the agent looking for it! On the short file above, in a repository where its paths exist:\n\nExit code 0. And on an old-style file:\n\nExit code 1. Run it before each commit that touches CLAUDE.md, or in CI.\n\nThe day your CLAUDE.md fits on one screen again, you will feel it!\n\n[How to Write a Good CLAUDE.md File](https://pub.towardsai.net/how-to-write-a-good-claude-md-file-ee372ab3e2fb) was originally published in [Towards AI](https://pub.towardsai.net) on Medium, where people are continuing the conversation by highlighting and responding to this story.", "url": "https://wpnews.pro/news/how-to-write-a-good-claude-md-file", "canonical_source": "https://pub.towardsai.net/how-to-write-a-good-claude-md-file-ee372ab3e2fb?source=rss----98111c9905da---4", "published_at": "2026-12-03 15:31:01+00:00", "updated_at": "2026-12-03 15:46:10.473207+00:00", "lang": "en", "topics": ["ai-agents", "ai-tools", "developer-tools"], "entities": ["Claude Code", "Anthropic"], "also_reported_by": [], "takeaway": "Keep CLAUDE.md to one numbered rule per line and under 200 lines, move detail to path-scoped files or links rather than @path imports (which load with the file and don't cut context cost), and put no-exception rules in hooks instead of capitals.", "recommendation": {"advice": "Keep each CLAUDE.md rule to one numbered sentence and the file under 200 lines, move detail to path-scoped files or links instead of @path imports, and enforce no-exception rules with hooks rather than capitalized emphasis.", "playbook": "coding-agents", "playbook_url": "https://wpnews.pro/playbooks/coding-agents"}, "alternates": {"html": "https://wpnews.pro/news/how-to-write-a-good-claude-md-file", "markdown": "https://wpnews.pro/news/how-to-write-a-good-claude-md-file.md", "text": "https://wpnews.pro/news/how-to-write-a-good-claude-md-file.txt", "jsonld": "https://wpnews.pro/news/how-to-write-a-good-claude-md-file.jsonld"}}