---
title: "TDD With Agents"
description: "Rigorous test-driven development loop using executable tests as the truth referee for coding agents."
section: "concepts"
type: "concept"
created: "2026-08-22"
updated: "2026-08-24"
confidence: "high"
tags: ["tdd", "workflow", "agents", "technique"]
canonical: "https://pyweb.dev/wiki/tdd-with-agents"
---
# TDD With Agents

Test-driven development as the control mechanism for agent-produced code. Write the
test first (RED), make it pass (GREEN), then refactor. With agents, the test is the
verifiable success criteria that lets the agent loop independently.

## Why it matters now
[matt pocock](/wiki/matt-pocock): "Bad code is now the most expensive it has ever been." Agents ship volume.
Without tests as the gate, the volume is slop. TDD turns [karpathy four guidelines](/wiki/karpathy-four-guidelines) #4
(goal-driven execution) from principle into practice: "fix the bug" → "write a test that
reproduces it, then make it pass." See also [red green tdd](/wiki/red-green-tdd) and [simon willison](/wiki/simon-willison)'s [agentic engineering patterns](/wiki/agentic-engineering-patterns).

## The loop
1. RED — write a failing test that encodes the requirement.
2. GREEN — write minimum code to pass the test. Nothing speculative.
3. REFACTOR — improve structure without changing behavior. [karpathy four guidelines](/wiki/karpathy-four-guidelines)
   #3 (surgical changes) governs this step.

## How agents change it
- The test IS the spec for the agent. A good test lets the agent loop autonomously
  because it knows when it's done.
- Each [tracer bullet](/wiki/tracer-bullets) ticket should ship with its test — the slice is
  verifiable because the test exists.
- The agent can run the test and report real results — no "I think it works."

## Apply it (Agent Recipe)
- **Role:** Test-driven implementation specialist.
- **When to trigger:** Writing new functionality or fixing a reported defect.
- **Instruction:**
  1. Write an isolated test case asserting target behavior.
  2. Run the test suite and verify the test fails for the expected reason (RED).
  3. Write the minimal implementation required to pass the test (GREEN).
  4. Refactor code structure while keeping the test suite green (REFACTOR).
- **Verification:**
  ```bash
  npm test
  ```
- **Pitfalls to avoid:** Writing tests after implementation; mocking out the actual unit under test.

## Related
[karpathy four guidelines](/wiki/karpathy-four-guidelines), [tracer bullets](/wiki/tracer-bullets), [idea to ship flow](/wiki/idea-to-ship-flow), [grilling doctrine](/wiki/grilling-doctrine), [red green tdd](/wiki/red-green-tdd).

---

## Agent Navigation

cluster: person (170 pages) | betweenness: 249.1

### References (outbound)
- [Matt Pocock](https://pyweb.dev/wiki/matt-pocock.md)
- [Karpathy's Four Guidelines](https://pyweb.dev/wiki/karpathy-four-guidelines.md)
- [Red/Green TDD](https://pyweb.dev/wiki/red-green-tdd.md)
- [Tracer Bullets](https://pyweb.dev/wiki/tracer-bullets.md)

### Referenced by (inbound)
- [Agent Harness Engineering](https://pyweb.dev/wiki/agent-harness-engineering.md)
- [Agentic Manual Testing](https://pyweb.dev/wiki/agentic-manual-testing.md)
- [Automated Eval Engineering](https://pyweb.dev/wiki/automated-eval-engineering.md)
- [Conformance Suites as Fitness Functions](https://pyweb.dev/wiki/conformance-suites-as-fitness-functions.md)
- [Designing for Verifiability](https://pyweb.dev/wiki/designing-for-verifiability.md)
- [Idea-to-Ship Flow](https://pyweb.dev/wiki/idea-to-ship-flow.md)
- [Karpathy's Four Guidelines](https://pyweb.dev/wiki/karpathy-four-guidelines.md)
- [Red/Green TDD](https://pyweb.dev/wiki/red-green-tdd.md)
- [Tracer Bullets](https://pyweb.dev/wiki/tracer-bullets.md)

### Evidence (verified primary sources)
- [karpathy-guidelines-skill-2026](https://pyweb.dev/wiki/raw/articles/karpathy-guidelines-skill-2026.md) | origin: https://github.com/multica-ai/andrej-karpathy-skills/blob/main/skills/karpathy-guidelines/SKILL.md | ingested: 2026-08-22 | sha256: 3dbba31cbdb82e77622b5e9f337405d3b934498957fe88f021500746844073f4
- [aihero-dev-homepage-2026](https://pyweb.dev/wiki/raw/articles/aihero-dev-homepage-2026.md) | origin: https://www.aihero.dev/ | ingested: 2026-08-22 | sha256: 231157fed45f82922924a050e09b5b81a443891814fc1d72c874d864a6075419
- [simon-willison-red-green-tdd-2026](https://pyweb.dev/wiki/raw/articles/simon-willison-red-green-tdd-2026.md) | origin: https://simonwillison.net/guides/agentic-engineering-patterns/red-green-tdd/ | ingested: 2026-08-22 | sha256: a578765f0bee9430776c214d892afe9fd74dd1b9107a24305f7dd89b4d6e1810

### Machine endpoints
- Knowledge graph: https://pyweb.dev/api/graph.json
- Graph analysis: https://pyweb.dev/api/graph-analysis.json
- Context index: https://pyweb.dev/llms.txt
