Layered Configuration for AI Coding Agents: A Practical Guide to Directory Structure and Governance
Most teams do not suffer from too little agent configuration; they suffer from configuration that was never layered. This guide explains the two-layer mental model behind AI coding assistant configuration (guidance versus control), a directory layout that is enough for most projects, three rules for keeping instructions and enforcement separate, how to manage the context budget, and the hidden pitfalls of multi-worktree setups. You will learn when to use a project instruction file, settings files with permissions and hooks, path-scoped rules, and on-demand skills, plus a four-week rollout plan and a periodic review routine for conflicting rules.
1. The problem is not too little configuration, but configuration without layers
Many developers, when they first pick up a CLI-based AI coding assistant, put everything they expect into a single project instruction file: tech stack, directory conventions, commit rules, deployment steps, prohibited actions, appended one after another. This works at the beginning, because the project is small and the context is short. But once the codebase grows to hundreds of thousands of lines, the number of collaborators rises, and the build and release pipeline gets longer, the configuration itself starts to become a liability:
- The instruction file swells to thousands of lines and consumes a noticeable share of the context budget at the start of every session, squeezing the room left for actual reasoning and coding.
- Suggestions and mandates get mixed together. “Prefer writing tests” and “never connect directly to the production database” are expressed in the same tone, so the model cannot tell which one has priority.
- Conventions that only make sense for one subdirectory are loaded globally and keep generating noise in unrelated modules.
- Multiple rule sets contradict each other, and the model picks arbitrarily among conflicting instructions, so behavior is not reproducible.
The conclusion is straightforward: most people only need to maintain two files carefully and add more directories only as needed. This is also the direction recommended by the official documentation of mainstream AI coding tools. First a mental model, then a directory map and governance rules.
2. The core mental model: the guidance layer and the control layer
An AI coding assistant’s configuration essentially answers only two questions: what it needs to know, and what it is allowed to do.
| Layer | Question it answers | Typical artifacts | How it takes effect |
|---|---|---|---|
| Guidance layer | What the project is, how to collaborate, conventions and preferences | Project instruction file, topic-based rule files | Injected as context, subject to the model’s judgment |
| Control layer | Which operations are allowed, which must be blocked | Permissions and hooks in settings files | Enforced at runtime |
This distinction is the foundation of the whole approach. The guidance layer is a manual written for the model: it will consult it but keeps room for judgment. The control layer is a policy written for the runtime, and it does not depend on whether the model cooperates. Anything that “must happen” or is “absolutely forbidden” can only live in the control layer.
To decide which layer a requirement belongs to, use a simple test: if the model ignores this line, is the consequence acceptable? If yes, put it in the guidance layer. If no, put it in the control layer. For example, “prefer existing utility functions” belongs in the guidance layer, while “do not delete the data directory” and “no write operations against production” must belong in the control layer.
3. A directory map that is good enough
your-project/├── AGENT.md # Main project instructions (shared, committed)├── AGENT.local.md # Personal overrides (not committed)└── .agent/├── settings.json # Control layer: permissions, hooks, project behavior├── settings.local.json # Personal control layer (not committed)├── rules/ # Modular rules, can be scoped by path│ ├── backend.md│ └── frontend.md├── skills/ # Reusable workflows, loaded only when needed├── agents/ # Subagent definitions└── .worktreeinclude # Ignored files to carry over into a new worktree
Responsibilities of each item:
- Project instruction file: loaded at the start of a session; describes the project’s purpose, directory structure, build and test commands, and collaboration conventions. It is resident context and therefore the most expensive, which is exactly why it needs the most restraint.
- settings.json: the control layer, holding permission allow/deny lists and hooks. Committed and shared, so the whole team gets the same behavior.
- settings.local.json: the personal control layer, for local debugging preferences. Not committed.
- rules/: rules split by topic, optionally bound to a path scope, injected only when files in the relevant directories are touched.
- skills/: a complete workflow packaged as a reusable capability, loaded on demand, taking up almost no routine context.
- agents/: subagent definitions, used to isolate retrieval, review, or large-scale refactoring work into separate contexts.
- .worktreeinclude: declares which version-control-ignored files must be copied when a new worktree is created.
User-level equivalents live in .agent/ under your home directory and apply to every project on that machine; project-level configuration lives in the repository and travels with the code. The two are in an override relationship, not a parallel one. Personal preferences should be pushed up to the user level as much as possible to avoid polluting the team repository.
4. Three principles you must follow
Principle 1: Separate suggestions from enforcement completely
Keep “should” in the instruction file and push “must” down into settings and hooks. Illustrative configuration (field names should follow the official documentation of the tool you use):
{"permissions": {"allow": ["read_file", "run_unit_tests", "run_linter"],"deny": ["delete_path:data", "write_env:production"]},"hooks": {"pre_tool_use": [{ "matcher": "shell", "action": "run:scripts/guard.sh" }],"post_tool_use": [{ "matcher": "edit", "action": "run:scripts/format.sh" }]}}
The value of hooks is that they turn “format checking, pre-commit checks, blocking dangerous commands” from model self-discipline into a matter of process. The model may forget; a hook will not.
Principle 2: Keep instruction files short
The common industry recommendation is to keep a single instruction file under 200 lines. There are two reasons. First, the context budget: the longer the resident content, the less is left for the real task. Second, compliance: when instructions get long and overlap, the model’s sensitivity to any individual constraint drops.
In practice, start by generating a draft with the initialization scan command, then delete whatever the model can read straight out of the code — directory structure, dependency lists, and function signatures do not need to be restated. Content that only applies to certain directories should move into a rule file with a path declaration:
---paths: ["src/api/**"]---API layer conventions: validate inputs, use unified error codes, never access storage directly inside handlers.
Principle 3: Workflows go in skills, facts go in instructions
If a section of the instruction file has grown into a multi-step workflow (release, migration, scaffolding, large-scale refactoring), extract it into a skill. Skills are loaded only when needed, cost almost nothing the rest of the time, and can be versioned, reused, and shared across projects. Factual, stable information stays in the instruction file. The older custom command directory is still supported, but new content is better written directly as skills.
5. Context budget: where resident ends and on-demand begins
Think of configuration as a budget sheet: resident items (instruction files, global rules) are billed every time, while on-demand items (skills, subagents, path-scoped rules) are billed only when triggered. The optimization order should be: first compress resident items, then convert long workflows into on-demand items, and only then consider reducing the number of files. One concrete check: count the lines in your instruction file and the number of skills. If the instruction file exceeds 200 lines and contains numbered steps, there is almost certainly a workflow that should be extracted.
6. The hidden trap of multi-worktree setups
When you develop in parallel using the worktree mechanism of your version control system, a new worktree is a fresh checkout. Files excluded by ignore rules (local environment variables, certificates, temporary credential templates) are not carried over by default, so the assistant starts up in the new worktree with missing configuration. Add those files to the worktree include list and they will be copied automatically at creation time. Copy only templates and placeholder files; real secrets belong in a secrets manager or should be injected through the environment, and must never enter the repository.
The same principle applies in cloud development environments — container platforms, logging services, monitoring and alerting, and comparable capabilities such as those on Baidu AI Cloud: configuration travels with the code, and sensitive information is injected by the runtime environment.
7. Periodic governance of rule conflicts
As the number of rules grows, contradictions are almost inevitable: one place says “new endpoints must have tests,” another says “temporary verification may skip tests.” Faced with conflicting instructions, the model may choose either, which shows up as behavior drift that is hard to reproduce. There are three remedies: annotate each rule with its scope and priority; delete one-off conventions instead of commenting them out; and run a configuration review every iteration cycle to remove zero-reference rules, merge duplicate topics, and archive outdated workflows.
8. Rollout path
- Week 1: maintain only the instruction file and settings.json, and get the control layer baseline working.
- Week 2: compress the instruction file to under 200 lines and extract 2–3 high-frequency workflows as skills.
- Week 3: introduce path-scoped rules for multi-directory projects to strip out global noise.
- Week 4 onward: establish a quarterly review routine to handle conflicts and redundancy.
The point of layering is not more files, but putting each kind of information in the layer where it costs the least and is enforced the strongest. Once you get that right, configuration can evolve with the project for the long term instead of turning into a legacy asset nobody dares to touch.
