Sub topic 1
How coding agents read and use context
Prerequisites
This chapter assumes you have:
- Basic software development experience in at least one language.
- Familiarity with Git.
- Some experience prompting an AI coding assistant or agent.
- Basic knowledge of automated testing.
The working model
A coding agent is a language model that runs in a loop. It reads text, decides on an action such as reading a file, running a command, or editing code, and then reads the result. The model has no memory of your project beyond what is placed in front of it. That text is its context.
Context is held in a context window, a fixed limit on how much text the model can consider at once. Everything the agent knows at a given step has to fit there.
Context usually comes from four sources:
| Source | Example | Who controls it |
|---|---|---|
| Your prompt | "Add password reset" | You |
| Instruction files | `AGENTS.md`, rules files | You, once per project |
| Files the agent reads | Source files, tests, docs | The agent, guided by your pointers |
| Tool output | Test results, compiler errors, `git diff` | The agent's own actions |
The agent assembles these into one input for each step. Anything not in that input does not exist for the model at that step.
What this means in practice
Three properties of this loop explain most surprising agent behavior.
- The agent only knows what it has read. If the rule "reset links expire after 30 minutes" is in your head and not in its context, the agent will pick a value on its own. A different run may pick a different value.
- The agent fills gaps with plausible guesses. A short prompt leaves many decisions open. The model resolves each one from patterns in its training data and from whatever code it happened to open.
- Context is finite and gets crowded. In a long session, early messages may be trimmed or summarized. A constraint stated at the start can disappear by step fifty.
You can see the first property in a simple prompt:
Add a password reset feature.This prompt does not say how long a link stays valid, whether an unknown email gets a different response, or whether a link can be reused. The agent must decide all three. Compare it with a prompt that points to written requirements:
Implement task 2 in specs/password-reset/tasks.md.
Read requirements.md and design.md in the same folder first.
Run pytest before you report the task as done.The second prompt is short, but it tells the agent where the decisions already live. The agent reads those files, and their content enters its context.
Common errors
- Assuming the agent remembers earlier sessions. A new session starts with an empty context. Anything you want to persist must be in a file.
- Pasting everything. Large dumps push out useful text and make the agent weigh irrelevant details. Point to the specific files instead.
- Stating a rule once in chat. Chat messages are the first thing to be trimmed. Put stable rules in an instruction file.
- Trusting that the agent read a file. It may have skipped it or only read part of it. Ask it to name the requirements it is satisfying, then check.
Why this leads to specs
If the agent acts only on what is in its context, then the quality of its output depends on the quality of that context. A spec is a way to put the right decisions in the right place, in files the agent can read at every session. The next chapters explain why prompts alone drift and how to write that spec.
Exercise
An agent implements a feature correctly on Monday. On Tuesday, in a new session with the same short prompt, it picks a different expiry time. Give the likely cause and one fix.
Show answer
The expiry time was never in the agent's context, so it guessed both times, and a new session starts with empty context. The fix is to write the requirement in a file, such as a spec or an instruction file, and have the prompt point to it.
Exercise
Name the four sources of context in this chapter and say which one the agent controls through its own actions.
Show answer
Your prompt, instruction files, files the agent reads, and tool output. The agent controls tool output through the actions it chooses, and it also decides which files to read.