Sub topic 8
Project instruction files for stable conventions
This chapter assumes you have used an AI coding agent on a real repository, can read a Git diff, and know the earlier chapters' vocabulary: spec, requirements, design, tasks, and acceptance criteria. You don't need experience with any particular tool.
What a project instruction file is
A project instruction file is a plain text file, kept in the repository, that the agent reads at the start of a session. It holds rules that stay true across many tasks: where code lives, which commands verify work, which behaviors are off limits, and how to work through a spec.
A spec describes one feature. An instruction file describes the project. If you put "run pytest before reporting done" in every spec, you repeat yourself and the copies drift apart. Putting that rule in one instruction file means every task inherits it.
Common file names differ by tool.
| Tool or convention | Typical location |
|---|---|
| AGENTS.md convention (read by many agents) | `AGENTS.md` at the repository root |
| Claude Code | `CLAUDE.md` |
| GitHub Copilot | `.github/copilot-instructions.md` |
| Cursor | `.cursor/rules/` |
| Kiro | `.kiro/steering/` (steering files) |
Names and loading rules change between versions, so check your tool's documentation. The practices below do not depend on the name.
Why this file matters in spec-driven development
An agent has no memory of your project beyond what is in its context window. Each session starts fresh. Without standing instructions, the agent guesses at conventions, and different sessions guess differently. That is one source of the drift described in the earlier chapter on why prompts alone are unreliable.
The instruction file fixes three things that a spec cannot:
- Where to look. It tells the agent that specs live in
specs/<feature>/, so it reads the requirements before touching code. - How to proceed. It sets the working rhythm: one task at a time, then stop for review.
- How to check. It names the command that proves a task is done.
A working example
Here is the file from the spec-driven workflow, shown as the agent would read it.
# AGENTS.md
## Workflow
- Read the relevant spec in `specs/<feature>/` before changing code.
- Work on one task from `tasks.md` at a time and stop for review after each.
- Do not change requirements. If one looks wrong, say so and ask.
## Conventions
- Run `pytest` before reporting a task as done.
- Keep new modules under `src/` and tests under `tests/`.Each line does a specific job.
- The first workflow rule makes the spec the agent's source of truth.
- The second rule sets the size of one unit of work and creates a review gate.
- The third rule protects requirements. Without it, an agent that finds a requirement hard to satisfy may quietly rewrite it. Spec drift often starts this way.
- The two conventions give a verification command and a layout, so the agent does not invent either.
What belongs in the file and what does not
Write down what the agent cannot infer from reading the code, and what it would otherwise get wrong repeatedly.
| Belongs | Does not belong |
|---|---|
| Commands to build, test, and lint | Feature requirements (they go in the spec) |
| Directory layout and naming rules | A full tour of the codebase the agent can read itself |
| Workflow rules such as one task, then review | Style rules a linter already enforces |
| Hard constraints: no new dependencies without asking | Long explanations of why a rule exists |
| Pointers to specs and design docs | Secrets, tokens, or credentials |
The second column deserves attention. If a formatter fixes spacing, run the formatter and leave the rule out. Spend the agent's limited attention on rules that only a human decision can set.
Writing rules an agent can follow
Vague rules produce vague behavior. Compare these pairs.
- Weak: "Write good tests." Better: "Every acceptance criterion in
requirements.mdneeds at least one test that names the criterion in its docstring or test name." - Weak: "Be careful with the database." Better: "Never edit files in
migrations/that are already committed. Add a new migration instead." - Weak: "Keep changes small." Better: "A task's diff should touch only the files that task lists. Report any other file you needed to change."
A good rule is specific, observable in a diff, and states the action to take. Positive instructions ("add a new migration") work better than bare prohibitions, because a prohibition leaves the agent without an alternative.
State priorities when rules can conflict. For example: "If a task in tasks.md conflicts with a requirement, the requirement wins. Stop and ask."
Layering: global, project, and directory scope
Most tools support more than one level of instructions.
- User-level rules apply to every project you open. Keep personal preferences here.
- Project-level rules live in the repository and are shared by the team through Git.
- Directory-level rules apply only inside a subtree, such as
frontend/orservices/billing/. Monorepos need these because a Python service and a TypeScript client have different test commands.
Keep each fact in one place. If the root file says "tests use pytest" and a subdirectory file says "tests use unittest", the agent sees both when working in that subtree and may follow either. When rules must differ, say which scope wins: "In frontend/, use npm test; the root pytest rule does not apply there."
Treat the file as code
The instruction file lives in version control, so review it the way you review code.
- Change it in a pull request, with a reason in the description.
- When an agent makes the same mistake twice, add a rule. When a rule never matters, remove it.
- Remove anything that is no longer true. A stale rule is worse than none, because the agent follows it with full confidence.
You can also test the file lightly. The script below checks that every path the file mentions actually exists. It skips placeholders such as specs/<feature>/.
import re
import sys
from pathlib import Path
def referenced_paths(text):
"""Return backtick-quoted paths that contain a slash."""
return re.findall(r"`([\w./<>-]+/[\w./<>-]*)`", text)
def main():
root = Path(sys.argv[1]) if len(sys.argv) > 1 else Path(".")
agents = root / "AGENTS.md"
if not agents.exists():
print("AGENTS.md not found")
return 1
text = agents.read_text(encoding="utf-8")
missing = [
p for p in referenced_paths(text)
if "<" not in p and not (root / p).exists()
]
for p in missing:
print(f"AGENTS.md references a missing path: {p}")
return 1 if missing else 0
if __name__ == "__main__":
sys.exit(main())Run it in CI with python check_agents_md.py. It catches one kind of rot: a renamed directory that the file still names. It cannot tell whether a rule is still wise, so human review stays necessary.
Common failure modes
The file grows without limit. Every incident adds a rule, and after a year the file runs to several hundred lines. Long files dilute the important rules and cost context on every task. Set a rough budget, such as one screen per scope, and prune on a schedule.
Rules duplicate the spec. If the file restates requirements, there are now two copies to keep in sync. Link to the spec and leave the requirement text there.
Rules contradict each other. This usually happens across layers. Read the user-level, project-level, and directory-level files together when you debug odd behavior.
The file describes an aspiration. A line like "all code has 90% coverage" is false in a repository at 55%. The agent cannot satisfy it, so it may pad tests to hit the number. Write what is true and enforced today.
No verification command. Without a named command, the agent decides for itself what "done" means. Always give it one that exits nonzero on failure.
Sensitive content. The file is committed, copied into prompts, and sent to a model provider. Keep credentials, internal hostnames, and customer data out of it.
Connecting the file to the rest of the workflow
The instruction file touches every phase.
- In requirements, it says that requirements are not to be edited by the agent.
- In design, it points to the design document and any architecture decision records.
- In tasks, it sets the one-task-then-stop rhythm that creates review gates.
- In verification, it names the test command and where tests live, so acceptance-criteria tests land in a predictable place.
- In drift handling, it tells the agent to report a mismatch between spec and code instead of silently resolving it.
A short, accurate file lets you write shorter prompts. A prompt such as "do task 2 of the password reset spec" works because the file already says where the spec is, how to verify, and when to stop.
Exercise
An agent keeps adding a new HTTP client library to each feature, even though the project already has one. Your instruction file currently says "Use good dependency hygiene." Rewrite the rule so the agent can follow it, and name one check outside the file that would catch a violation anyway.
Show answer
A specific rule: "Use the existing HTTP client in src/http/client.py. Do not add a new dependency without asking first. If a task seems to need one, stop and explain why." An outside check: a CI step that fails when the dependency manifest changes without a matching approval, or a review gate where the diff of pyproject.toml or requirements.txt is inspected before merge. The file makes the behavior likely, and the CI step or reviewer makes a violation visible.
Exercise
Your repository's root AGENTS.md says "Run pytest before reporting a task as done." The frontend/ directory contains a JavaScript app tested with npm test. Describe the problem and give a fix.
Show answer
The root rule applies inside frontend/ too, so the agent may run pytest, find nothing relevant, and report success without running the JavaScript tests. Fix it by adding a directory-level instruction file in frontend/ that says "Run npm test before reporting a task as done" and states that it overrides the root rule within that directory. Optionally reword the root rule to "Run pytest for Python code under src/" so the two scopes do not overlap.