[KongchangAI]
· 3 min read· 1,604 words

Claude Code Tutorial: Splitting Project Rules with .claude/rules

Claude Code Tutorial: Splitting Project Rules with .claude/rules

Split project rules into multiple files under .claude/rules to avoid single-file bloat and conflicts.

The article explains how to improve Claude Code's usability by splitting project rules. When rules in the root CLAUDE.md grow, the single file becomes bloated, lookup and editing efficiency drops, and adjacent content is easily modified by mistake. The solution is to split rules by category into multiple Markdown files placed in the `.claude/rules` hidden folder, with each file responsible for one category, such as code style and naming conventions. After creating them, exit and reopen the Claude Code session to ensure the rules are loaded. CLAUDE.md and the rules directory can coexist, but content conflicts must be avoided, otherwise Claude may arbitrarily pick one to execute. Official guidance recommends keeping CLAUDE.md under 200 lines and splitting when rules exceed 20, for easier maintenance and collaboration.

As project rules pile up and get crammed into the root-level CLAUDE.md, finding and editing them becomes time-consuming, and it's easy to accidentally modify adjacent content. This pain point grows rapidly as the project scales. In section 2.3 of the tutorial, the Bilibili creator offers a clear solution: split rules into multiple Markdown files, place them all in the .claude/rules hidden folder at the project root, and have each file handle only one category of rules.

Why Project Rules Need to Be Split

When a project first starts, a few rules in the root CLAUDE.md are more than enough. But as rules for code style, testing requirements, security standards, API design, and so on gradually accumulate, a single file becomes bloated. The creator explains it plainly in the video: when a rule file mixes too much content, looking up a specific category of rules is inefficient, and edits can easily hit adjacent content by mistake.

The splitting approach is similar to splitting CSS stylesheets in front-end development—break detailed specifications into multiple Markdown files, with each file responsible for just one category of rules. The tutorial's goal is to create two files: code-style.md for code style and naming.md for naming conventions.

The first letter of each word is required to be uppercase

CLAUDE.md is the official Claude Code project-level instruction file, typically placed in the project root directory. Claude Code automatically reads this file at the start of each session and injects its contents as part of the system prompt into the context, so the model follows project-specific coding style, testing requirements, and other conventions. A similar mechanism is called .cursorrules in Cursor and .github/copilot-instructions.md in GitHub Copilot. The advantage of CLAUDE.md is that it's simple and direct—all rules in one file, easy to look up quickly. But the downside is obvious: as rule entries grow, the file balloons rapidly, becoming not only hard to maintain but also consuming too much of the context window and reducing the model's attention to each rule. For this reason, the official guidance is to keep it under 200 lines and to split fine-grained rules into multiple files under the .claude/rules directory, organized by category—convenient for humans to read and for the model to load on demand.

Creating Rule Files in Practice

In the tutorial demo, the creator first creates the 2.3 working directory, launches VSCode with the code command, then opens the Claude Code terminal. Next, via a single natural language instruction, Claude Code is asked to create the .claude hidden folder and the rules directory inside it, and to generate two rule files there.

The content of code-style.md is: use two spaces for indentation, and use single quotes uniformly for strings. The content of naming.md is: variable names start with a lowercase letter, with each subsequent word capitalized when multiple words are concatenated (lower camel case); function names start with a verb; class names start with an uppercase letter.

The large model creates the folder and files almost simultaneously. In the project root you can see the .claude hidden folder, and the contents of the two Markdown files under its rules directory match the requirements.

Verifying That the Rules Actually Take Effect

Writing the rule files doesn't mean Claude Code has loaded them. The creator emphasizes a key step: you must exit Claude Code in the current session and reopen the terminal to ensure the rule files are re-read.

To verify, use a prompt to have Claude Code create a hello.js file with a greeting function. Then check each item one by one: the lower-right corner of VSCode shows an indentation of two spaces, the strings are indeed single-quoted, the variable name userName follows lower camel case, and the function name sayHello starts with a verb. The class-name rule is verified by creating another class—the class name starts with an uppercase letter, and the functions inside also start with verbs.

This u is lowercase

Context window is the maximum number of tokens a large language model can process in a single inference, including the system prompt, conversation history, tool call results, and injected rule files. When Claude Code starts a session, it concatenates the contents of CLAUDE.md and the rule files under .claude/rules into the system prompt, and this content continuously occupies the context window. When rule files are too long, they not only squeeze out token space needed for understanding user requests and code, but also cause the model's attention to distant rules to decay during generation—this is why the official guidance recommends keeping CLAUDE.md under 200 lines. In addition, the context window is a limited and expensive resource; overly long rule files significantly increase token consumption per request and raise usage costs. Therefore, splitting rules and keeping each file short and focused is essentially about optimizing context usage efficiency, so the model concentrates its limited attention on the most relevant instructions.

CLAUDE.md and the rules Directory Can Coexist

The tutorial explicitly states that the CLAUDE.md file created in sections 2.1 and 2.2 is allowed to coexist with the .claude/rules directory. The creator offers a memorable analogy: CLAUDE.md is like a company profile, while the rule files in .claude/rules are like an employee handbook—the former concisely summarizes core project-level information, the latter specifically constrains each category of conventions.

But coexistence has a prerequisite: there must be no conflicts. The creator runs a controlled experiment, deliberately requiring double quotes uniformly in CLAUDE.md, contradicting the single-quote rule in code-style.md. As a result, Claude Code uses double quotes only for the newly created addition function, while the strings in hello.js are still single-quoted.

But never let conflicts arise

Consequences of Rule Conflicts and Official Recommendations

This experiment confirms what the official Claude Code documentation says: if two instructions contradict each other, Claude may arbitrarily pick one, with no fixed priority. In the video, the creator checks the "Store instructions and memories" section of the official documentation, which recommends regularly reviewing CLAUDE.md and rule files nested in subdirectories to maintain consistency.

Another key data point from the official documentation: CLAUDE.md is analyzed by line count, and it's recommended to keep it under 200 lines. Exceeding 200 lines consumes more context and reduces the model's compliance. This is precisely the fundamental motivation for splitting rules—as the project grows, rule files easily exceed 200 lines, and only by splitting can you ensure Claude Code is more obedient and hallucinates less.

So this is exactly why we need to learn about splitting rules today

User memory is a cross-project persistence mechanism provided by Claude Code that allows users to save personal preferences or general rules to user-level configuration files rather than the project-level CLAUDE.md. User memory is typically stored in the ~/.claude/ directory (such as ~/.claude/CLAUDE.md) and applies to all projects; project memory is stored in the project root's CLAUDE.md and .claude/rules and applies only to the current project. When both exist, Claude Code reads them merged, but if their contents conflict, the model may arbitrarily choose one to execute, with unpredictable results. The memory command lets you view and edit user-level memory files, delete entries no longer needed, and avoid leftover rules interfering with the current project. Understanding the hierarchical relationship between user memory and project memory helps maintain rule consistency and maintainability during multi-person collaboration or cross-project development.

When Is It Worth Splitting

The creator offers an experience-based threshold: for small projects with 20 rules or fewer, usually a single CLAUDE.md is enough; with more than 20 rules, it's recommended to split by content for easier maintenance and multi-person collaboration. After splitting, different people can write their own rule files—for example, one person handles code style and another handles naming conventions—with clear division of labor and less chance of conflict.

Another case worth splitting is when conventions are distinguished by file. Naming like code-style.md and naming.md already conveys the file's responsibility—you can understand the content just by reading the filename.

Also Cleaning Up Leftover User Memory Rules

At the end of the video, the creator addresses a leftover issue: during the demo in section 2.2, a rule was set in user memory saying "start every answer with 'received'", causing Claude Code to append "received" to every reply. The cleanup method is to open Claude Code, type memory, select the user-level memory file, and delete the unneeded entries. This also demonstrates how to modify and clean up rules, reinforcing the previous section's knowledge about user memory.

The core of the whole process can be summarized as: split rules into .claude/rules, restart the session to verify they take effect, keep rules from conflicting with each other, and keep each file's line count within a reasonable range. Only when the rules are solid can Claude Code's output be stable and controllable.

One-line Summary

Split project rules into multiple files under .claude/rules to avoid single-file bloat and conflicts.

Paragraph Summary

The article explains how to improve Claude Code's usability by splitting project rules. When rules in the root CLAUDE.md grow, the single file becomes bloated, lookup and editing efficiency drops, and adjacent content is easily modified by mistake. The solution is to split rules by category into multiple Markdown files placed in the .claude/rules hidden folder, with each file responsible for one category, such as code style and naming conventions. After creating them, exit and reopen the Claude Code session to ensure the rules are loaded. CLAUDE.md and the rules directory can coexist, but content conflicts must be avoided, otherwise Claude may arbitrarily pick one to execute. Official guidance recommends keeping CLAUDE.md under 200 lines and splitting when rules exceed 20, for easier maintenance and collaboration.

#Claude Code#.claude/rules#CLAUDE.md#project rules#naming conventions#code style#context window
Share:

Related articles