Agents Skip the Red Phase
The default behavior of most AI coding agents is to generate code and tests simultaneously. Left to their own devices, agents write tests that confirm what the code already does — not tests that define what the code should do. The result is a green test suite that exercises nothing and catches nothing. [1]
This matters more than it might seem. AI-generated code introduces 1.7x to 2.7x more defects than human-written code, and 70% of engineering leaders report that quality has already degraded as AI accelerates development. [2] Teams using AI assistants without quality guardrails report a 35–40% increase in bug density within six months. The speed is real. The reliability gap is also real.
Red-green TDD — write a failing test first, then implement until it passes — is the simplest guardrail that addresses both problems at once. And it turns out to be a natural fit for how coding agents work.
The Pattern: Red-Green-Refactor for Agents
Traditional TDD follows three steps: write a failing test (red), write the minimum code to make it pass (green), then clean up the implementation (refactor). With an AI coding agent, the same cycle applies but the roles shift. You write the specification as a test. The agent writes the implementation.
Simon Willison, creator of Datasette and one of the most experienced practitioners of agentic coding, describes red-green TDD as “a pleasingly succinct four-word prompt that unlocks substantial engineering discipline already baked into the models.” The prompt is simply: Use red/green TDD. [1]
The red phase is the critical part. When the agent writes a test first and confirms it fails, it proves two things: the test actually exercises new behavior (not something that already passes), and the test harness is wired up correctly. Without the red phase, agents routinely produce tests that pass vacuously — tests that look right but assert nothing meaningful.
Why Test-First Works Better with AI Than with Humans
Developers have debated test-first development for decades. Some teams swear by it, others find it slows them down. With AI agents, the calculus changes in three ways.
First, agents are fast but imprecise. An agent can generate hundreds of lines of code in seconds, but without constraints, the output drifts from intent. Tests written first act as behavioral constraints — they define the contract before the agent starts implementing. This is the core insight behind spec-driven development: the specification, not the code, is the artifact you maintain.