Sub topic 4
Telling the agent to work test-first
This chapter assumes you can write and run unit tests in one language, read a Git diff, and have used a coding agent at least once. It builds on the red-green-refactor cycle and the read, edit, run, observe loop from earlier chapters. Here you turn that workflow into written instructions the agent follows.
Why the agent needs to be told
Left alone, most agents work implementation-first. You ask for a feature, the agent writes the code, and then it may add tests that describe what the code already does. Those tests pass on the first run, so they never show that they can fail. They record the agent's guess, not your intent.
Test-first work reverses the order. A failing test comes first and states the behavior. The implementation follows, and its only job is to make that test pass. The agent will not do this reliably on its own, so you have to say so, and you have to say it precisely.
Vague instructions such as "use TDD" leave the agent to fill in the details. It may write five tests at once. It may write a test and the implementation in the same step. It may skip running the test before the change. Each of these breaks the cycle in a different way. A good instruction names the steps, the order, and the evidence you expect to see at each step.
Where the instructions live
You have two places to put them.
- A persistent instruction file in the repository. Most agents read a project-level file at the start of every session. The file name depends on the tool. Check your agent's documentation. This file holds rules that apply to every task, such as the workflow and the prohibitions.
- The prompt for a single task. This holds what is specific to one change: the behavior you want and which test file it belongs in.
Put the stable rules in the file and the changing details in the prompt. If you repeat the workflow in every prompt, the copies drift apart and you stop maintaining them. Commit the instruction file like any other file, so changes to the rules show up in review.
A working agreement
The following file is a complete example. It is short on purpose. Agents follow short, numbered, checkable rules better than long essays.
## Working agreement for this repository
1. Work test-first. Write one failing test for the requested behavior and run it.
2. Show the failure output and confirm it fails because the behavior is missing, not because of a typo or import error.
3. Write the smallest change that makes that test pass, then run the full test suite.
4. Refactor only while all tests are green, and run the suite again afterwards.
5. Do not edit, skip, or delete existing tests to make them pass. If a test seems wrong, stop and explain why.
6. Do not hardcode expected values from tests into the implementation.Each rule closes a specific gap.
Rules 1 and 2: one test, and proof that it fails
"One failing test" limits the size of the step. Without it, an agent may write a whole test file for a feature and then implement everything at once, which removes the cycle.
Rule 2 asks the agent to show the failure output and to say why the test failed. This is the most important line in the file. A test can be red for the wrong reason. A misspelled import raises ModuleNotFoundError, and a typo in a function name raises NameError. Both produce a red result, but neither says anything about the behavior. Only a failure like assert None == "hello-world" or AssertionError on the expected value shows that the test measures what you meant.
Requiring the agent to quote the output gives you something to check. You can read it in seconds.
Rule 3: smallest change, then the full suite
"Smallest" keeps the agent from building ahead of the tests. If the only test says slugify("Hello World") == "hello-world", then text.lower().replace(" ", "-") is enough. Anything more, such as punctuation handling, is untested code that nothing requires.
Running the full suite, not only the new test, catches regressions. An agent that runs only the new test can break an older behavior without noticing.
Rule 4: refactor only on green
Refactoring changes structure without changing behavior. It is only safe when tests can confirm that behavior is unchanged. The rule has two parts: start from green, and end on green with a fresh run. The second run is what turns "I tidied up" into "I tidied up and the suite still passes."
Rules 5 and 6: the prohibitions
These two target the usual ways an agent turns a red result green without doing the work: changing the test, or returning the exact value the test expects. Later chapters cover these failure modes and how to enforce the rules mechanically. In the instruction file, the key point is that the rule gives the agent a permitted alternative. "If a test seems wrong, stop and explain why" tells it what to do instead of editing the test. An agent given only a prohibition tends to look for a way around it.
Giving the agent a feedback command
The agent can only observe what it runs. Give it one exact command for the whole suite and put that command in the instruction file or the prompt.
python -m pytest -qThe -q flag shortens the output. The summary line, such as 1 failed, 2 passed, and the failure traceback are what the agent reads to decide its next step. Two variations are useful during a cycle:
python -m pytest -q -k lowercasesruns only the tests whose names containlowercases. Use it for the red check on the new test.python -m pytest -q -xstops at the first failure, which keeps the output short when a change breaks many tests.
The full command stays the reference command. Rule 3 says to run it after every green change. If your project needs extra setup, such as an activated virtual environment or a database, say so in the file. An agent that cannot run the tests will often guess that they pass.
What one cycle looks like
The sequence below shows the exchange you want. The agent writes a test, runs it, and reports the failure before it touches the implementation.
The pause after the failure is a deliberate checkpoint. You can approve it by hand while you build trust in the agent. Once you trust its judgment about failure reasons, you can drop the pause and review the transcript afterward.
Writing the per-task prompt
The instruction file covers the process. The prompt supplies the behavior. State one behavior, give an example input and output, and name where the test goes.
Add one behavior to slugify in slugify_util.py: punctuation is removed.
Example: slugify("Hello, World!") should return "hello-world".
Put the test in test_slugify.py. Follow the working agreement.
Stop after the red step and show me the failing output.Notice what the prompt leaves out. It does not describe the implementation, and it does not list five other behaviors you plan to add later. Describing the implementation tells the agent how to write the code before a test exists. Listing future behaviors invites it to build them now.
A concrete example in the prompt also matters. "Handle punctuation" can mean many things. "Hello, World!" becomes "hello-world" is a specific claim that the agent can turn into an assertion without guessing.
Common instruction mistakes
| Mistake | What the agent does | Fix |
|---|---|---|
| "Use TDD" with no steps | Writes code and tests together, or tests after the code | List the numbered steps |
| No requirement to show failure output | Reports "the test fails" without running it, or fails for an import error | Require quoted output and a stated reason |
| No test command given | Guesses the command or skips running tests | Put the exact command in the file |
| Several behaviors in one prompt | Writes many tests, then one large implementation | One behavior per prompt |
| Prohibitions without an alternative | Edits the test quietly or works around the rule | Add "stop and explain" |
| Rules buried in a long document | Follows some rules and ignores others | Keep the list short and numbered |
Limits of instructions
An instruction file is a request, not a guarantee. The agent can misread a rule, forget it in a long session, or follow the wording while missing the point. A test it deleted will not appear in the agent's own summary unless you look at the diff.
Treat the file as the first layer. It makes the right behavior the default and gives you a standard to review against. Later chapters add the other layers: reviewing diffs, protecting test files, and running tests in CI.
You can test the file itself. Give the agent a small task, such as the punctuation behavior above, and check each rule against its output. Did it show a failure? Was the reason correct? Did the full suite run afterward? A rule the agent skipped is a rule to reword, often by making it more specific.
Exercise
An agent reports "I added the test and it fails." You open the transcript and see the failure is ModuleNotFoundError: No module named 'slugify_util'. Which rule in the working agreement was not followed, and what should you ask the agent to do?
Show answer
Rule 2 was not followed. It requires confirming that the test fails because the behavior is missing, not because of an import error. The test is red for the wrong reason and tells you nothing about the behavior. Ask the agent to fix the import, for example by creating the module with a stub function, and rerun the test until it fails on the assertion.
Exercise
Rewrite this prompt so it fits test-first work: "Make slugify handle punctuation and repeated separators and non-ASCII letters, and add tests."
Show answer
Split it into one prompt per behavior, for example: "Add one behavior to slugify: punctuation is removed. Example: slugify('Hello, World!') returns 'hello-world'. Write the failing test first, show the failure output, and stop." Repeated separators and non-ASCII letters each get their own prompt and their own cycle, each with a concrete example.