Context Governance for AI Coding Tools: How to Keep Rules Files Lean and Effective

    This article explains why Markdown rules files for AI coding agents tend to grow bloated over time, and how to govern them. It covers the two loading mechanisms (always-on versus on-demand), a cost model that treats rules as a context budget, and a layered governance approach built on a single source of truth, executable checks, structured formatting, periodic review, separation of shared and local rules, and pushing details into subdirectory files. It also outlines signals for measuring and improving governance over time. The goal is stable enforcement of critical constraints during long sessions.

    1. The Problem: Why Rules Files Get Slower to Use Over Time

    When teams work with AI coding tools (such as coding agents that can understand code and execute actions), they usually maintain a Markdown rules file at the project root. It works like a project manual: it enters the model’s context as soon as a session starts and stays resident for the entire session. Typical contents include build commands, directory layout, monorepo structure, coding standards, and team conventions.

    The problem lies in how these files evolve. In a shared repository, they often end up like unmaintained configuration: every team appends its own instructions, but almost no one deletes anything. As the file grows, costs start to stack up. Every line enters every engineer’s session, regardless of whether it is relevant to the current task. The result is twofold: it continuously consumes context budget (tokens), and it dilutes the enforcement of the instructions that actually matter.

    To understand this, it helps to be clear about two basic constraints of context: capacity is limited, and attention gets spread thin. When a rules file expands from a few dozen lines to over a thousand, the model’s adherence to hard constraints such as “you must use a specific logging component” is usually less stable than when the file is lean.

    2. Two Loading Mechanisms: Always-On and On-Demand

    Governance starts with understanding loading semantics. Rules files typically come in two loading modes.

    Always-on: Located at the project root. It can live in a shared repository as a team baseline, or locally as a personal preference (personal preferences should not be committed to the shared repository). It is loaded at the start of a session and is not lost or degraded during long sessions; when the tool compacts the conversation history, it is read again, so the constraints carry forward.

    On-demand: Located in subdirectories, for example a rules file under app/api/. It is loaded only when the agent actually reads files in that subdirectory, and it does not enter the context at session start. Its compaction behavior follows the same path-scoped rule: it reappears only after being touched.

    An illustrative directory structure:

    1. repo/
    2. ├── RULES.md # Always-on: team baseline, keep it as short as possible
    3. ├── app/
    4. │ ├── RULES.md # On-demand: loaded only when working on files under app
    5. │ └── api/
    6. │ └── RULES.md # On-demand: path-scoped, most specific
    7. └── .local/RULES.md # Always-on: personal preferences, not committed

    The core rule of thumb is simple: put global constraints that are independent of paths in the always-on file, and put conventions tied to a specific directory in on-demand files. Stuffing subdirectory standards into the root file is the most common source of bloat.

    3. A Cost Model: Treat the Rules File as a Budget

    Set an explicit budget for the always-on rules, for example keeping them around a hundred lines, and make trade-offs ordered by “does it affect correctness.” It can be roughly understood as:

    1. Per-session context cost ≈ always-on rules + system prompt + conversation history + on-demand rules actually read + code snippets

    Always-on rules are the only item that happens “unconditionally,” so their marginal cost is the highest. The cost of on-demand rules is tied to the task’s path and is usually a better deal.

    4. A Layered Governance Approach

    1. Single source of truth: Write the same convention in only one place, avoiding duplicate and conflicting descriptions across the root file and subdirectory files.
    2. Executable over descriptive: If a constraint can be expressed through build scripts, formatter configuration, type checks, tests, and pipeline gates, do not write it as natural language. Automatically verifiable rules are far more reliable than written reminders.
    3. Structured expression: Use short sentences, lists, and clear headings; avoid long narrative paragraphs; put hard constraints like “must / must not” in their own section.
    4. Periodic cleanup: Bring rules files into code review, delete outdated entries, and assign clear owners; for new entries, require an explanation of “what error occurs if this is not written down.”
    5. Separate shared and local: Keep the team baseline lean, and put personal habits in a local always-on file to avoid personalized content polluting the shared context.
    6. Use on-demand files for details: Push API directory conventions, frontend component conventions, test data notes, and similar details down into the corresponding subdirectories.

    An example of a lean always-on file:

    1. # Project Baseline
    2. - Build: use the unified script entry point; do not call internal implementations directly
    3. - Layout: monorepo; packages depend on each other through public interfaces
    4. - Quality: formatting, type checks, and unit tests must pass before committing
    5. - Prohibited: hardcoding environment configuration in business code

    5. Measurement and Continuous Improvement

    Governance needs to be observable. Three kinds of signals are worth watching: changes in the line count and size of always-on rules; whether rules are still followed after being compacted during a session; and the number of “duplicate / outdated rules” deleted during review. If an enterprise uses a cloud platform with unified model access, logging services, and monitoring and alerting capabilities (such as general capabilities from Baidu AI Cloud), changes to the rules file and adherence to it can be folded into the existing observability system to form a closed loop.

    6. Conclusion

    The value of a rules file is not in “how comprehensively it is written,” but in “whether critical constraints are enforced consistently.” Dividing layers by loading mechanism, constraining size with a budget, replacing written reminders with automation, and keeping things fresh through review are what allow an AI coding agent to consistently and accurately follow team conventions during long sessions.