Sub topic 7
Teaching the agent your project with instructions files
This chapter assumes you can run a project's build and test commands, you have used a chat-based coding assistant, and you know the basics of Git. It covers how an instructions file gives the agent standing knowledge about your project.
Why instructions files exist
The agent starts each session knowing nothing about your repository. It can read files, but it has to guess your package manager, your test command, and your style rules. Wrong guesses cost tool calls and produce edits you then have to correct.
An instructions file is a Markdown file stored in the repository. The editor adds its contents to the context of each request. You write the project rules once, and the agent receives them every time instead of you repeating them in each prompt.
Where the file lives
The location depends on the editor and the assistant.
| Tool | Common location |
|---|---|
| VS Code with GitHub Copilot | `.github/copilot-instructions.md` |
| Cursor | Files under `.cursor/rules/` |
| Other agents | Often an `AGENTS.md` file at the repository root |
File names and supported formats change between versions. Check your editor's documentation for the current convention. Commit the file so the whole team shares the same rules.
A working example
This is a repository-level file for a TypeScript project.
# Project instructions
- This is a TypeScript project using npm. Install with `npm ci`.
- Run tests with `npm test` before finishing any change.
- Use named exports. Do not add default exports.
- Keep new code under `src/` and tests next to the file as `*.test.ts`.
- Do not edit files under `dist/` or `package-lock.json` by hand.Each line does a specific job:
- Build and test commands tell the agent how to check its own work. This supports the loop of editing, running tests, and fixing failures.
- Conventions such as named exports prevent style drift.
- Boundaries such as "do not edit
dist/" keep the agent away from generated files.
What to put in, and what to leave out
The file competes with your prompt and your attached files for space in the context window. Keep it short and specific.
Good candidates:
- Commands the agent cannot infer, such as a custom test script or a required lint step.
- Rules that differ from common defaults, such as "no default exports".
- Directories the agent should treat as read-only.
- Naming and placement rules for new files.
Poor candidates:
- General advice such as "write clean code". The model already tries to.
- Long architecture essays. Link to a document or attach it when a task needs it.
- Anything that changes weekly. Stale instructions mislead the agent.
Write rules as checkable statements. "Tests live next to the file as *.test.ts" is better than "organize tests sensibly".
Common errors
- The file is ignored. The path or name is wrong, or the setting that enables the file is off. Check the editor's documentation. Many editors also show which files were added as context in the response.
- Rules conflict. Two instructions files, or an instruction and your prompt, can disagree. The model picks unpredictably. Remove the duplication.
- Commands are out of date. If
npm testbecomespnpm test, update the file in the same commit. - Secrets in the file. The file is committed and sent to the model provider. Never put tokens or passwords in it.
- Treating rules as guarantees. The model usually follows instructions but can miss them. Review the diff, and use tool approval for commands that matter.
Exercise
Your instructions file says "Run tests with npm test", but the agent keeps running yarn test and fails. Name two likely causes and one fix for each.
Show answer
First, the file may not be loaded, for example because it is at the wrong path or the feature is disabled. Fix it by moving it to the location your editor documents and checking that it appears in the request's context. Second, another instruction or your prompt may mention yarn, so the rules conflict. Fix it by finding and removing the conflicting text, and state the one correct command in a single place.
Exercise
Which of these belongs in an instructions file: "Write good code", "Do not edit files under dist/", or the full text of your database schema?
Show answer
"Do not edit files under dist/". It is short, specific, and checkable. "Write good code" is too vague to change behavior. The full schema is long, so attach it to tasks that need it instead of sending it with every request.