Test-first prompting: let the tests drive the AI agent

Short answer: before asking an AI agent to implement something, write (or approve) the tests that define "correct", then prompt: make these tests pass, don't touch the tests, run them after every change. The tests turn a vague request into a precise target, the agent gets an automatic feedback loop, and your review shrinks to two questions: are these the right tests, and is the code reasonable?

A real example: splitting a premium into installments

Ask an agent to "split an annual premium into monthly installments" and you get something that works for 12,000,000 Rials and quietly loses a Rial on 1,000,000 split in three, because of rounding. Every installment looks right; the sum doesn't match the policy. Finance finds it a month later.

Now write the rules down as tests first:

public class InstallmentsTests
{
    [Theory]
    [InlineData(1_000_000, 3)]
    [InlineData(10, 4)]
    [InlineData(7, 7)]
    [InlineData(0, 5)]
    public void Parts_always_sum_to_the_total(long total, int count) =>
        Assert.Equal(total, Installments.Split(total, count).Sum());

    [Fact]
    public void Parts_differ_by_at_most_one_unit()
    {
        var parts = Installments.Split(1_000_000, 3);
        Assert.True(parts.Max() - parts.Min() <= 1);
    }

    [Fact]
    public void Larger_parts_come_first()
    {
        Assert.Equal(new long[] { 333_334, 333_333, 333_333 }, Installments.Split(1_000_000, 3));
    }

    [Theory]
    [InlineData(100, 0)]
    [InlineData(-1, 3)]
    public void Invalid_input_is_rejected(long total, int count) =>
        Assert.Throws<ArgumentOutOfRangeException>(() => Installments.Split(total, count));
}

The tests say what nobody wrote in the ticket: parts must sum exactly, differ by at most one unit, put the larger parts first, and reject bad input. With that, the implementation is almost forced:

public static class Installments
{
    public static long[] Split(long total, int count)
    {
        ArgumentOutOfRangeException.ThrowIfNegativeOrZero(count);
        ArgumentOutOfRangeException.ThrowIfNegative(total);

        var baseAmount = total / count;
        var remainder = total % count;

        return Enumerable.Range(0, count)
            .Select(i => baseAmount + (i < remainder ? 1 : 0))
            .ToArray();
    }
}

The prompt I use

Implement Installments.Split in src/Billing/Installments.cs so that every test in
tests/Billing.Tests/InstallmentsTests.cs passes.

Rules:
- Do not modify, skip or delete any test.
- If a test looks wrong, stop and tell me which one and why.
- Run dotnet test after each change and show me the result.
- Keep the change inside Installments.cs.

"If a test looks wrong, stop and tell me" matters. Without it, an agent that can't make a test pass sometimes "fixes" the test instead.

The loop

  1. Red. Write or approve tests from the requirement. Run them; they fail for the right reason.
  2. Green. The agent implements until dotnet test passes, running it after every change.
  3. Review. Check that no test file changed, then read the implementation.
  4. Refactor. Ask for a cleanup with the same rule: tests stay green and untouched.

Pros and cons

Pros

  • The agent has an objective target and its own feedback loop, so it iterates without you.
  • Edge cases are decided by you, up front, not discovered in production.
  • Review is faster: the tests carry the intent, the diff carries the implementation.
  • You end up with a test suite, which most "vibe-coded" projects lack.

Cons

  • Writing good tests takes thought; you can't outsource deciding what correct means.
  • Agents may overfit to the tests; add a property-style test or two for important logic.
  • Less useful for UI and exploratory work where the target isn't clear yet.

Pair it with a project AGENTS.md that says "don't edit tests to make them pass", and the habit sticks across sessions.


Want to adopt AI-assisted development without losing quality? See how I can help.

Frequently asked questions

Can the agent write the tests too?

Yes, as a separate step: ask it to write tests from the requirements, then review and fix the tests yourself before any implementation exists. The tests are the specification, so they must be yours even if the agent typed them.

What if the agent changes the tests to make them pass?

Say explicitly in the prompt and in AGENTS.md that tests must not be changed, and check the diff for test files on every review. For stronger protection, run the tests from a protected branch in CI or use a hook that blocks edits to the tests folder during implementation.

Does this work for UI or exploratory work?

Less well. Test-first prompting shines for logic with clear inputs and outputs: money calculations, validation, parsing, state machines. For UI and spikes, iterate visually first and add tests once the behaviour settles.