Sub topic 7
Teaching the agent your project with instructions files
An instructions file is a Markdown file that your editor adds to the agent's context automatically. It holds the build commands and conventions you would otherwise retype in every prompt. This chapter covers where these files live, what to put in them, how to check that they load, and the errors that make them ineffective.
This chapter assumes you can use agent mode in an IDE, you know how the agent reads context and calls tools, and you use Git and run build and test commands in a terminal.
The working model
The model behind the agent keeps no memory between chat sessions. Each request is assembled from your prompt, attached files, the conversation so far, tool results, and any instructions files that apply. The model reads all of this as text.
An instructions file is how project knowledge survives between sessions. Without one, the agent has to guess your package manager, test command, and naming style. It may guess right after reading package.json, or it may not.
Instructions are guidance, not enforcement. The model usually follows them but can ignore or misread them, especially when the file is long or the prompt conflicts with it. Anything that must never happen belongs in tooling, such as a linter, a CI check, or tool approval settings.
Where instructions files live
File names and settings differ by editor and change between releases. The table shows common conventions. Check your editor's current documentation before relying on any of them.
| File | Scope | Typical use |
|---|---|---|
| `.github/copilot-instructions.md` | Whole repository | VS Code with GitHub Copilot, and some other IDEs with Copilot |
| `.github/instructions/*.instructions.md` | Files matching a glob in the file's header | Rules for one language, folder, or file type |
| `AGENTS.md` | Repository, sometimes nested folders | A tool-neutral convention read by several agents. VS Code may require a setting to enable it. |
| User-level instructions | All your workspaces | Personal preferences, such as response style |
| `.cursor/rules/` | Repository, with rule types | Cursor's equivalent |
| `CLAUDE.md` | Repository | Claude Code's equivalent |
Repository files are committed to Git, so the whole team shares one set of conventions. User-level files stay on your machine. Put team conventions in the repository and personal taste in your user settings.
A repository-wide file
Here is a small, useful file saved as .github/copilot-instructions.md.
# 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 one of three jobs:
- Commands.
npm ciandnpm testtell the agent how to build and verify its own work. This matters because the agent loop depends on observing results. - Conventions. Named exports and test placement are choices that cannot be inferred reliably from a few files.
- Boundaries. Generated and locked files are named explicitly.
Each rule is short, concrete, and checkable. A reviewer can look at a diff and say whether the rule was followed.
Rules for part of the codebase
A single large file wastes context when most rules apply to only one area. A path-specific file loads only when its glob matches. In VS Code these files go in .github/instructions/ and end in .instructions.md. A header at the top holds the glob.
---
applyTo: "**/*.test.ts"
---
- Use the same test runner and assertion style as the existing tests.
- Each test name states the behavior, for example "rejects an empty name".
- Do not call the network. Use the fakes in `tests/helpers/`.
- Do not use `.only` or `.skip` in committed tests.When the agent works on a file such as src/users.test.ts, these rules join the context. For src/users.ts they do not.
Header field names can vary by tool. Cursor rules, for example, use their own metadata. Copy the header format from your editor's documentation instead of guessing it.
What belongs in the file
Include what the agent cannot reliably discover and what costs you time when it gets it wrong:
- Install, build, lint, and test commands, including the exact flags your project needs.
- Architecture facts, such as "HTTP handlers live in
src/routes/, business logic insrc/services/". - Naming, export, and error-handling conventions that differ from language defaults.
- Files and folders the agent must not touch.
- How to verify work, such as "run
npm testandnpm run lint, and fix failures before stopping".
Leave out:
- Anything a linter or formatter already enforces. Run the tool instead.
- General advice such as "write clean code". It gives the model nothing to act on.
- Long copies of documentation. Link to the file or tell the agent where to read it.
- Secrets, tokens, internal hostnames, and customer data. The file is committed and sent to the model provider.
Check that the file loaded
A file in the wrong place fails silently. Verify it with these steps:
- Confirm the path and name match your editor's convention exactly, including the
.github/folder. - Open a new chat. Many editors list the instructions files used for a response in a references or context section of the answer.
- Ask a question only the file can answer, such as "Which command should you run before finishing a change?" If the answer is wrong, the file is not loading or is being overridden.
- If you use
AGENTS.mdor nested files, confirm the relevant setting is enabled.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| The agent ignores a rule | The file is long, the rule is vague, or the prompt contradicts it | Shorten the file, make the rule specific, and remove conflicts |
| The agent uses the wrong command | The file is stale after a script rename | Update the file in the same pull request as the change |
| Rules apply to the wrong files | The glob in `applyTo` is too broad or too narrow | Test the glob against real paths |
| Two files give opposite advice | A repository file and a user-level file conflict | Keep one source of truth for each rule |
| The file has no effect | Wrong path, wrong name, or a feature setting is off | Re-check the convention and the references list |
| Teammates see different behavior | The file was never committed | Commit it and review it like code |
Two further habits prevent trouble. First, keep the file short. Instructions compete for the model's attention with your prompt and the code it reads, so each extra line dilutes the others. Second, grow the file from experience. When the agent makes the same mistake twice, add one line. Do not try to predict every mistake up front.
Instructions, prompts, and enforcement
Use each layer for what it does best.
| Layer | Best for | Limit |
|---|---|---|
| Instructions file | Stable facts that apply to many tasks | Advisory, and shares context space |
| Task prompt | Goal and acceptance criteria for one change | Must be rewritten each time |
| Tool approval settings | Controlling which commands and tools can run | Only covers actions that go through tools |
| Linters, tests, CI | Hard rules | Run after the edit, not before |
An instructions file says "run npm test". The task prompt says what to build. Approval prompts stop dangerous commands. CI catches what the other layers miss.
Maintain the file like code
- Review changes to it in pull requests, because it changes how every teammate's agent behaves.
- Date-sensitive details such as version numbers go stale quickly. Prefer "see
package.json" over copying versions. - Remove rules the agent now follows without being told, and rules for code that no longer exists.
- Keep one authoritative file where you can. If your team uses several tools, a shared
AGENTS.mdthat the others point to avoids copies drifting apart. Confirm each tool supports that.
Exercise
Your team's .github/copilot-instructions.md says "Run npm test before finishing", but the agent keeps stopping without running tests. List three checks you would make, in order, before concluding that the model is simply ignoring the rule.
Show answer
First, confirm the file is loaded: check the path and name, open a new chat, and look at the references for the response. Second, ask the agent a question only the file answers, such as which command to run before finishing, to see whether it can see the rule. Third, look for conflicts and length: a contradicting rule in another instructions file or the prompt, or a very long file that buries the line. If all three are clean, make the rule more specific and put the test step in the task prompt too. For a hard requirement, enforce it in CI.
Exercise
Write a path-specific instructions file that applies only to Python files under scripts/ and tells the agent to use type hints and to run pytest scripts/ after edits. Include the header.
Show answer
---
applyTo: "scripts/*/.py"
---
- Add type hints to all function parameters and return values.
- After editing, run pytest scripts/ and fix any failures before stopping.