Test-first development
Write the tests first and confirm they fail; the agent then works until they pass.
Also called Red/green TDD, Test-driven development
Taylor et al., Cursor, Anthropic, Simon Willison, Kent Beck
The problem
An agent may write code that does not work or is never used, and stops when the work only looks done.
A significant risk with coding agents is that they might write code that doesn't work, or build code that is unnecessary and never gets used, or both.
Claude stops when the work looks done. If it has nothing to run that tells it whether the work actually is done, “looks done” is all it has to go on.
Therefore
Agents perform best when they have a clear target to iterate against. Tests allow the agent to make changes, evaluate results, and incrementally improve until it succeeds.
Ask Claude to create tests before starting work
The most disciplined form of TDD is test-first development. You write the automated tests first, confirm that they fail, then iterate on the implementation until the tests pass.
In use
Kent Beck
You can see from the first commits that I was trying to get the genie to use TDD.
Simon Willison
If an agent finds something that doesn't work through their manual testing, I like to tell them to fix it with red/green TDD.
Anthropic's Security Engineering team
The Security Engineering team transformed their workflow from "design doc → janky code → refactor → give up on tests" to asking Claude for pseudocode, guiding it through test-driven development, and checking in periodically.
Where it breaks
The agent can bend code to pass the tests or tamper with the tests, or write mock implementations unless told the work is test-first.
Claude can sometimes focus too heavily on making tests pass at the expense of more general solutions, or might use workarounds such as helper scripts for complex refactoring instead of using standard tools directly.
Be explicit that you're doing TDD so it avoids creating mock implementations for functionality that doesn't exist yet.
Reward hacking has been observed in real training runs, with coding agents learning to overwrite or tamper with test cases rather than write correct code.
Sources
- School of Reward Hacks: Hacking harmless tasks generalizes to misaligned behavior in LLMsMia Taylor, James Chua, Jan Betley, Johannes Treutlein, Owain Evans, Taylor et al.
- Best practices for coding with agentsLee Robinson, Cursor, Jan 9, 2026
- How Anthropic teams use Claude CodeAnthropic, Jul 24, 2025
- Prompting best practicesAnthropic
- Best practices for Claude CodeAnthropic
- Red/green TDD - Agentic Engineering PatternsSimon Willison
Every good model understands "red/green TDD" as a shorthand for the much longer "use test driven development, write the tests first, confirm that the tests fail before you implement the change that gets them to pass".
- Augmented Coding: Beyond the VibesKent Beck, Jun 25, 2025
- Agentic manual testing - Agentic Engineering PatternsSimon Willison
Added Nov 14, 2026