Sub topic 6
Persistent instructions: keep them short and stable
This chapter assumes you have used a coding agent in a terminal, know that the context window is limited, and understand that every file read and tool result takes space in it. Here you will learn how to write the project instructions that load at the start of every session.
What persistent instructions are
Persistent instructions are text the agent loads at the start of every session. Different tools use different file names, such as AGENTS.md, CLAUDE.md, or a rules directory. The idea is the same. You write the file once, and the agent reads it before your first message.
The file is part of the fixed cost of your context. If it holds 3,000 tokens, those tokens are gone from every session, whether or not the task needs them. A long file also competes for attention with the code and output the agent is working on.
What belongs in the file
Include facts that the agent cannot easily infer from the code and that matter in most sessions.
- Commands to build, test, and lint, written exactly as you run them.
- Conventions that differ from the language default, such as an unusual import order.
- Boundaries, such as directories the agent must not edit.
- Pointers to deeper documents, such as "API design notes are in
docs/api.md".
Leave out anything the agent can learn by reading one file, anything that changes weekly, and long explanations. If a rule applies to only one subsystem, put it in a document and link to it. The agent reads the link only when the task needs it.
A short, stable example
# AGENTS.md
## Commands
- Test: `pytest -x -q`
- Lint: `ruff check src tests`
## Conventions
- Public functions need type hints and a one-line docstring.
- Do not edit files under `generated/`.
## Pointers
- Architecture overview: `docs/architecture.md`
- Release steps: `docs/release.md`This file is about 70 words. Each line is a fact the agent would otherwise have to guess or discover by searching.
Why stable matters
Stable means you rarely edit the file. This matters for two reasons.
- Changes do not apply to a running session in most tools. The agent keeps the version it loaded, so a mid-session edit can leave the agent and the file out of sync.
- Many providers cache a repeated prompt prefix. If the start of the context stays identical between requests, the cached copy is reused, which lowers cost and latency. Editing text near the top breaks that match. Later chapters cover this in detail.
Put task-specific details, such as the current goal or the bug you are fixing, in your messages or in a progress file. Do not put them in the persistent file.
Common errors
- Growing the file after every mistake. Adding a rule each time the agent errs produces a long list the agent follows unevenly. Review the file now and then, and delete rules that no longer matter.
- Duplicating the code. Pasting function signatures or schemas into the file makes them go stale. Point to the source file.
- Vague rules. "Write clean code" gives the agent nothing to act on. "Run
ruff checkbefore finishing" does. - Contradictions. Two rules that conflict, often written months apart, lead to unpredictable behavior.
Exercise
Your instruction file lists the current sprint's bug, the full test command, and a 40-line description of the payment module. Which parts should stay, and where should the others go?
Show answer
Keep the test command, since it applies to most sessions. Move the sprint bug to your task message or a progress file, because it changes often and breaks prefix stability. Move the payment module description to a document such as docs/payments.md, and leave a one-line pointer in the instruction file.