38.3 Test-driven agent behavior

Overview and links for this section of the guide.

TDD for Agents

Force the agent to write the test before the implementation.

  1. Write a failing test that reproduces the bug or asserts the feature.
  2. Run test -> FAIL (confirmed).
  3. Write implementation.
  4. Run test -> PASS (success).

If step 4 fails, the agent can see the error message and retry step 3. This self-healing loop is the "superpower" of vibe coding.

The Loop

max_retries = 3
while (test_fails and retries > 0):
  error = run_tests()
  code = model.fix(code, error)
  retries -= 1

Where to go next