Sub topic 4
System Prompt and Project Instructions
This chapter assumes you know how messages and roles are sent to a model, and that the context window is the model's entire working memory. It also assumes you can read JSON and short Python. Here you will see what a system prompt is, what goes into it, how project instruction files reach it, and what goes wrong when it is poorly managed.
What the system prompt is
A model call is stateless. The model only knows what the harness sends in that one request. The harness is the program that runs the model in a loop. The system prompt is the first block of text in that request, marked with a special role or a dedicated request field. It sets the standing rules for the whole session: who the model is, which tools it has, how it should behave, and what it must not do.
The user never types this text. The harness author writes it, and the harness sends it again on every model call. If you stop sending it, the model forgets all of it, because nothing is stored on the model side.
Two properties matter for everything below:
- The system prompt costs tokens on every call. A 3,000-token prompt in a 40-step session is 120,000 tokens of input in total, though the window only holds one copy at a time.
- Models are trained to give system-level text more weight than text that arrives later in a tool result. This is a tendency, not a guarantee. Do not treat the system prompt as a security boundary.
What goes into a system prompt
Production prompts differ, but most contain the same sections.
| Section | Purpose | Example |
|---|---|---|
| Identity and goal | Sets the role and the kind of work | "You are a coding agent working in the user's repository." |
| Behavior rules | Style, level of caution, when to ask | "Make the smallest change that solves the task." |
| Tool guidance | When to prefer one tool over another | "Search with grep before reading whole files." |
| Safety rules | Limits on actions | "Never run commands that delete files outside the workspace." |
| Environment facts | Facts the model cannot guess | Working directory, operating system, current date |
| Output format | How to report results | "End with a short list of files you changed." |
Tool definitions are usually sent in their own request field, not in the prompt text. The model still reads them as part of its context, so they count against the window just like the prompt does. The system prompt then adds guidance about how to use them, which the schema alone cannot express.
Project instructions
A generic prompt cannot know that your project runs tests with make check, that you use tabs, or that the legacy/ directory must not be edited. Project instructions fill that gap. They are a text file kept in the repository, such as AGENTS.md, CLAUDE.md, or a rules file in a tool-specific folder. The file name varies by product. The mechanism does not.
At startup, the harness looks for these files, reads them, and places their contents into the context. It usually puts them in the system prompt or in an early message, wrapped in labels so the model can tell where they came from.
from pathlib import Path
BASE_PROMPT = """You are a coding agent working in the user's repository.
Make the smallest change that solves the task.
Run the project's tests before you say a task is done."""
INSTRUCTION_FILES = ["AGENTS.md", "CLAUDE.md"]
def load_project_instructions(root, max_chars=8000):
parts = []
for name in INSTRUCTION_FILES:
path = Path(root) / name
if path.is_file():
text = path.read_text(encoding="utf-8")
if len(text) > max_chars:
text = text[:max_chars] + "\n[truncated]"
parts.append(f"<project_instructions file=\"{name}\">\n{text}\n</project_instructions>")
return "\n\n".join(parts)
def build_system_prompt(root):
instructions = load_project_instructions(root)
return BASE_PROMPT + ("\n\n" + instructions if instructions else "")
if __name__ == "__main__":
print(build_system_prompt("."))This sketch shows the full pipeline. The harness assembles the prompt once at session start, caps the size of each file, and labels each source. The harness then sends the result as the system prompt on every call.
Layers and precedence
Many harnesses load instructions from several places: a user-level file for personal preferences, a project-level file at the repository root, and sometimes files in subdirectories that apply only when the agent works there. When two layers disagree, a common convention is that the more specific layer wins. This is a convention of each harness, not a rule of the model. If two files conflict, the model has to guess which one to follow, so avoid conflicts instead of relying on precedence.
Writing instructions that work
Project instructions are read by a model with no memory of your team. Write for a capable newcomer.
- State commands exactly. "Run tests with
pytest -q tests/" is usable. "Make sure tests pass" is not, because the model must guess the command. - Give facts the model cannot infer from the code. Naming conventions are visible in the files. A rule such as "the staging database is shared, so never run migrations against it" is not.
- Prefer specific rules over general ones. "Use type hints on public functions" beats "write clean code."
- Say what to do, then what to avoid. A bare prohibition leaves the model without an alternative. "Do not edit
generated/. Change the schema inschema/and runmake generate" gives both. - Keep it short. Every line is paid for on every call and competes for attention with the task. Move rarely needed detail into separate documents and mention them by path, so the model reads them with a file tool only when needed.
Failure modes and edge cases
Instructions that are too long
The file competes with the task and the conversation for the same window. A 15,000-token instruction file leaves less room for code, and models tend to follow individual rules less reliably when there are very many of them. Symptoms include ignored rules that appear late in the file. The fix is to cut, not to add emphasis.
Conflicting or vague rules
"Always ask before changing files" and "work autonomously without interruptions" cannot both hold. The model picks one, and may pick differently on different runs. Read your combined prompt as one document and look for contradictions across layers.
Instructions lost during compaction
When the harness summarizes old messages to free space, the system prompt and project instructions should stay outside that summary and be resent in full. If instructions are placed in an early user message instead, a careless compaction step may summarize them away. If an agent obeys your rules for twenty steps and then stops, check whether the instructions survived compaction.
Stale instructions in a long session
The harness reads the file once at startup. If you edit it mid-session, the running agent keeps the old version. Restart the session, or use the harness's reload command if it has one.
Instructions from untrusted repositories
A project file is text that someone wrote, and the harness loads it with high priority. If you open a repository you did not write, its instruction file can tell the agent to do anything, including running commands or sending data out. Review an unfamiliar repository's instruction files before running an agent there, and keep approvals on. The chapter on prompt injection covers this risk in more detail.
Prompt caching
Because the system prompt is identical across calls, many providers can cache it and charge less for repeated input. Caching usually works on a shared prefix, so changing any early part of the prompt invalidates the cache for everything after it. A harness that inserts the current time at the top of the prompt can lose this benefit on every call. Keep stable text first and changing text last.
How to verify what the model sees
You cannot debug what you cannot see. Most harnesses can log the full request. Check three things: that the instruction file was loaded, that it was not truncated, and that it appears where you expect. When an agent ignores a rule, first confirm the rule was in the request. If it was, shorten or sharpen the rule. If it was not, fix the loading.
Exercise
Your agent follows the rule "run make check before finishing" for the first 15 steps of a long session, then stops running it. The instructions file was loaded correctly at the start. Give two plausible causes and one way to test each.
Show answer
Cause 1: the instructions were placed in an early message and were summarized or dropped when the harness compacted the history. Test by logging the request after compaction and searching for the rule text. Cause 2: the rule is one of many long instructions and is being followed less reliably as the context fills with tool output. Test by shortening the file or moving the rule near the end of the system prompt, then repeating the run and checking whether the behavior returns.
Exercise
A teammate wants to add the current time to the first line of the system prompt so the agent knows the date. What side effect might this have on cost, and where should the line go instead?
Show answer
The text changes on every call, so a provider's prefix cache is invalidated and the whole prompt is billed as new input each time. Put stable text first and the date at the end of the prompt, or include it only once per session when it is fixed.