How to Write a Project Rules File That Keeps AI Coding Assistants on Track
A practical guide to project-level rules files for AI coding assistants. It explains why these files matter, how to verify that the assistant can read them, which sections to include, a reusable template, and how to maintain the file as the codebase and team conventions evolve.
1. Why a Project Rules File Is Needed
AI coding assistants are good at reading code, editing code, and running commands, but they start from zero every time they enter a project. They need to infer the technology stack, test commands, directories that should not be changed casually, and preferred code style from the directory structure, configuration files, and historical code. This inference process consumes context window space, and the results are often inaccurate.
A project rules file captures this long-term, stable knowledge in text, places it in the project root, and commits it with the code. Tools read it at session startup and treat its contents as background information, so subsequent operations stay aligned with team conventions. These files are also often called rules files or project-level configuration.
Different tools may use different file names and loading locations. This article uses “rules file” to refer to the file that a tool reads at the start of a session. For the exact file name and priority, refer to the official documentation for your tool.
A rules file provides three main benefits:
- It reduces repeated exploration and shortens task startup time.
- It unifies commands and conventions, lowering the rework rate.
- It defines operational boundaries, helping avoid accidental changes to sensitive files or dangerous operations.
A rules file also has limits. It cannot replace code comments or formal documentation. Its content should stay concise, and it should not duplicate details already stated in the code.
2. Preparation Before Writing
Before writing a rules file, confirm that the assistant can read it. The simplest check is to start a new session and ask the assistant to restate the project overview and test commands from the rules file. If it cannot answer, the file name is usually wrong, or the file is not in a location the tool reads.
Also note that the rules file will be committed to the code repository, so it must not contain any access credentials, keys, or internal addresses. Model service access configuration is a user-level setting and should be stored locally, not written into project files. For specific fields and storage locations, refer to the official documentation for the tool.
3. Core Structure of a Rules File
A usable rules file usually contains the following modules. Each module should be written around one question: what does the assistant need to know when performing a task?
1. Project Overview
Use two or three sentences to explain the project’s purpose, main modules, and main users. This helps the assistant quickly determine which business area a task belongs to.
- Should include: what the project does and what the core directories are responsible for.
- Should not include: company history, marketing slogans, or other content unrelated to coding.
2. Tech Stack and Common Commands
List the languages, frameworks, and runtime versions, as well as the commands for installing dependencies, starting the development environment, running tests, linting, and building. Version numbers should follow the official documentation. The team should align on versions; otherwise, the same command may produce different results on different machines.
- Should include: complete commands that can be copied and executed directly.
- Should not include: steps that require human judgment, such as “run relevant tests as needed.”
3. Directory Boundaries
Explain which directories can be modified, which are read-only, and which contain generated files that should not be edited manually.
- Should include: explicit rules for generated directories, third-party code directories, migration history directories, and similar areas.
- Should not include: vague requirements for the entire repository, such as “pay attention to code quality.”
4. Code Conventions
Clearly state naming conventions, formatting tools, error handling, logging conventions, and approval requirements for adding dependencies. If code style is already enforced by a formatter or linter, the rules file only needs to specify the tool name and configuration file location.
5. Testing and Acceptance
Explain which tests must be run after code changes, what results count as passing, and whether new features must include tests. The more specific the acceptance criteria, the easier it is for the assistant to self-check.
6. Commit Requirements
Explain the commit message format, branch naming rules, and whether the assistant is allowed to commit automatically. If the team requires human review before merging, state that clearly as well.
7. Security Boundaries
Write these in two categories. The first category is forbidden operations, such as deleting production data, modifying deployment configuration, force-pushing to shared branches, or reading secret files. The second category is operations that require confirmation before execution, such as upgrading major dependencies, running database migrations, or changing access permissions.
4. Reusable Template
Below is a complete template. Replace the content in angle brackets with actual information for your project.
# Project Rules## Project Overview<Two or three sentences explaining the project's purpose, main modules, and main users>## Tech Stack and Common Commands- Language and runtime: <language and version, per team convention>- Install dependencies: <command>- Start development environment: <command>- Run tests: <command>- Lint: <command>- Build: <command>## Directory Boundaries- Modifiable: <directory list>- Read-only: <directory list>- Generated files, do not edit manually: <directory or file list>## Code Conventions- Formatter and config: <tool name and config file path>- Naming conventions: <brief description>- Error handling: <brief description>- New dependencies: <approval requirements>## Testing and Acceptance- Must run after changes: <test command>- Pass criteria: <description>- New features: <whether tests are mandatory>## Commit Requirements- Commit message format: <format example>- Branch rules: <description>- Auto-commit allowed: <yes or no, with conditions>## Security Boundaries- Forbidden: <operation list>- Confirm before execution: <operation list>
Every item in the template should have actual content. If there is no convention for an item yet, write “None yet” rather than leaving it blank; otherwise, the assistant will infer one on its own.
5. Continuous Iteration and Maintenance
A rules file is rarely correct the first time. Teams should maintain it as part of the code.
First, define the triggers for updates. Common situations include: the assistant repeatedly makes incorrect changes to the same type of files, a new module is added, test or build commands change, or team conventions are adjusted. When these situations occur, update the relevant sections promptly.
Second, record changes. You can keep a short change log at the end of the rules file, stating the date, what changed, and why. This helps later readers understand why the rules are written as they are.
Third, clean up regularly. Review the rules file once a quarter, remove outdated items, and remove duplicate items that are already enforced by the linter or CI. Fewer, more precise rules are easier for the assistant to follow.
Fourth, control length. The rules file is read at every session, so longer content consumes more context. Detailed explanations longer than one screen should be split into separate documents, with only key points and document links kept in the rules file.
Fifth, write rules based on incorrect-change cases. Whenever the assistant makes an unexpected change, first analyze the cause: is a rule missing, is the wording vague, or are multiple rules conflicting? Then add a clear rule, ideally with a positive or negative example. For example, change “Be careful not to modify generated files” to “Do not manually edit any file under src/gen/. To make changes, edit the corresponding schema file and regenerate.” After several rounds of iteration, the rules file will increasingly fit the team’s actual workflow, and the assistant’s rework will decrease accordingly.
