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.
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.
And 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.
Sound familiar? Today’s fix is very satisfying: CLAUDE.md keeps one rule per line, and the detail lives in files it points to.
The 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."
Every line, every task, relevant or not! A long file does not only cost tokens. It makes each rule easier to miss.
And 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.
The 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.
Write 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:
Your 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.
And number the rules. Then a detail file or a commit can cite “rule 4” without copying it. And never renumber.
The rest has four places to go.
Why 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.
Now 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.
Capitals? 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.
A 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.
The 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.
Here is the new root file. It goes at the root of your repository:
It 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.
The detail file it links to, docs/rules_in_detail.md:
And a rule scoped to one part of the code, in .claude/rules/api.md:
The prompt that does the cut. Run it on a branch, then read the diff of the detail file:
Here 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:
The 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:
Exit code 0. And on an old-style file:
Exit code 1. Run it before each commit that touches CLAUDE.md, or in CI.
The day your CLAUDE.md fits on one screen again, you will feel it!
How to Write a Good CLAUDE.md File was originally published in Towards AI on Medium, where people are continuing the conversation by highlighting and responding to this story.
原文来源:pub.towardsai.net