Sub topic 6
Persistent instructions: keep them short and stable
This chapter assumes you have used a coding agent in a terminal or editor, can read a short shell or Python snippet, and know that a model has a limited input size measured in tokens. Earlier chapters covered what fills the context window. Here the focus is one specific item in that list: the instructions that load at the start of every session.
What persistent instructions are
Persistent instructions are text files that the agent loads automatically at the start of each session. Different tools use different names, such as AGENTS.md, CLAUDE.md, rules files, or steering files. The name varies, but the mechanism is the same. The file's contents are placed near the top of the context before you type anything.
That placement has two consequences:
- Every token in the file is paid for in every session, whether or not the task needs it.
- The file sits at the front of the context for the whole session, so it is read on every turn.
A 3,000-token instruction file in a 200,000-token window costs 1.5% of the budget. That sounds small. It stops being small when the file grows to 15,000 tokens, or when several instruction files from different directories all load together.
The cost model
The cost of an instruction is not only its tokens. Three costs add up.
- Space. The tokens occupy the window for the whole session and cannot be removed without restarting or editing the file.
- Attention. Models follow a short list of clear rules more reliably than a long document. When a file contains 80 rules, the rules that matter for the current task compete with 79 others.
- Conflict risk. The more rules a file has, the more likely two of them disagree, or that one becomes out of date and contradicts the code.
The goal is not the shortest possible file. The goal is the smallest file that still prevents the mistakes you would otherwise correct by hand.
What belongs in the file
A useful test for each line is: "Would the agent get this wrong without being told?" If the agent can discover the fact by reading the repository in a few seconds, the line is a candidate for deletion.
| Keep | Remove |
|---|---|
| Commands the agent cannot guess, such as the exact test command for this repo | Descriptions of the directory tree that `ls` already shows |
| Conventions that differ from the language default | General advice such as "write clean code" |
| Hard constraints, such as "never edit files under `generated/`" | Long explanations of why the architecture was chosen |
| Pointers to where detailed documents live | The full text of those documents |
| Known traps, such as a flaky test or a required environment variable | Task-specific details that apply to only one piece of work |
Task-specific details are the most common mistake. A note like "we are currently migrating the settings loader to TOML" is true today and wrong in a month. Put it in a progress file instead (covered in a later chapter), where it can be deleted when the task ends.
Write for a reader with no history
The agent starts every session knowing nothing about your project. Write each line so it makes sense with no surrounding conversation. Compare these two lines:
- Vague: "Use the usual test setup."
- Specific: "Run tests with
pytest -x -q. Integration tests needDATABASE_URLset and are skipped otherwise."
The second line costs a few more tokens and removes a whole class of wrong guesses.
Prefer imperative, checkable statements. "Keep functions small" cannot be verified. "Do not add new dependencies without asking" can.
An example file
This file is about 200 words. It covers commands, one convention, one hard constraint, and pointers.
# Project instructions
## Commands
- Install: `uv sync`
- Test: `pytest -x -q` (integration tests need `DATABASE_URL`)
- Lint and format: `ruff check . && ruff format .`
## Conventions
- Python 3.12. Type hints on all public functions.
- Use `pathlib`, not `os.path`.
- Error messages start with a lowercase letter and have no trailing period.
## Constraints
- Never edit files under `src/generated/`. Regenerate with `make codegen`.
- Do not add dependencies without asking.
## Where to look
- Architecture overview: `docs/architecture.md`
- Release process: `docs/release.md`
- Current task notes: `PROGRESS.md` (if present)Notice what is absent. There is no description of what the project does, no list of modules, and no style essay. The docs/ pointers let the agent read those files only when a task needs them, which connects to targeted reading from the previous chapters.
Use pointers instead of copies
When a topic needs detail, link to a file instead of pasting its contents. A pointer costs about 20 tokens. The document it points to might cost 5,000, and the agent pays that only when it opens the file.
Many tools also support scoped instruction files. A rule file in a subdirectory loads only when the agent works there, and a rule with a file pattern loads only for matching files. Use this for rules that apply to one area, such as a frontend/ directory with its own test command. The root file stays small and the specialized rules appear only when relevant.
Keep them stable
The second half of the chapter title is about change. A stable file has two benefits.
First, you stop re-debugging behavior. If the file changes every day, you cannot tell whether a new problem comes from your edit or from the agent.
Second, many providers cache the beginning of the prompt. A cached prefix is cheaper and faster to process on later turns, but only while it stays byte-for-byte identical. Any edit near the top invalidates the cache for everything after it. A later chapter covers prompt caching in detail. For now, the practical rule is this: put slow-changing content in the instruction file, and put fast-changing content (task notes, current status) elsewhere.
Stable does not mean frozen. Edit the file when a correction repeats. If you tell the agent the same thing in three different sessions, that is a signal the fact belongs in the file. Make the edit between sessions, not in the middle of one, so the change takes effect cleanly in the next session.
Auditing the file
Instruction files drift. Run a short review every few weeks, or whenever the file passes a size you chose in advance.
- Measure the size.
- Delete lines that fail the "would the agent get this wrong?" test.
- Check each command still works.
- Look for pairs of rules that conflict.
- Move task-specific notes to a progress file.
A small script makes the size check routine. This one reports the approximate token count of an instruction file, using the common rule of thumb of about four characters per token. It is an estimate, not an exact count.
from pathlib import Path
import sys
def estimate_tokens(text: str) -> int:
return len(text) // 4
def main() -> int:
path = Path(sys.argv[1] if len(sys.argv) > 1 else "AGENTS.md")
if not path.exists():
print(f"{path} not found")
return 1
text = path.read_text(encoding="utf-8")
tokens = estimate_tokens(text)
lines = len(text.splitlines())
print(f"{path}: {lines} lines, about {tokens} tokens")
if tokens > 2000:
print("warning: over 2000 tokens; consider trimming or splitting")
return 2
return 0
if __name__ == "__main__":
sys.exit(main())Save it as check_instructions.py and run python check_instructions.py AGENTS.md. The 2,000-token threshold is a starting point. Pick a number that fits your window and tool, and keep it.
Common errors
- Writing a manual. Long files feel thorough, but they dilute the rules that matter. Cut until each line earns its place.
- Duplicating the README. The agent can read the README on demand. Copying it into the instruction file pays for it in every session.
- Using emphasis everywhere. If every rule says "CRITICAL" or "NEVER", none stands out. Reserve strong wording for the two or three rules where a violation is costly.
- Stale commands. An instruction that names a removed script wastes a turn each time the agent tries it. Check commands during the audit.
- Mixing temporary and permanent content. Status notes in the instruction file go stale and stay loaded. Keep them in a separate, deletable file.
- Assuming the file is obeyed. An instruction is guidance in the context, not an enforced rule. For constraints that must hold, such as protecting a directory, back the instruction with a permission setting, a git hook, or a CI check.
Where this fits in a session
Persistent instructions are the one part of the context you control completely before the session starts. They sit in the fixed part of the context, below the part that grows as you work.
Exercise
A teammate proposes adding the following to the root instruction file: (1) the full text of the 40-page API style guide, (2) "Run tests with pytest -x -q", (3) "This week we are renaming Client to Session", (4) "Never edit src/generated/". Which of these belong in the root file, and where should the others go?
Show answer
Keep (2) and (4). The test command is something the agent cannot reliably guess, and the generated-directory rule is a hard constraint. Item (1) should stay in a docs file, with a one-line pointer in the root file, so the agent reads it only when needed. Item (3) is task-specific and temporary, so it belongs in a progress file that is deleted when the rename is finished.
Exercise
Your instruction file is 9,000 tokens, and the agent still ignores a rule near the middle. Give two reasons the size may be the cause and two steps to fix it.
Show answer
Reasons: the rule competes for attention with many others, and the file may contain a conflicting or stale rule that overrides it in practice. Steps: delete lines that fail the "would the agent get this wrong without being told?" test, and move area-specific or long reference content into scoped rule files or docs files with pointers. Also check for conflicting pairs of rules. For a rule that must never be broken, add enforcement outside the prompt, such as a permission setting or CI check.