Stop telling your AI agent to 'fix the bug'. Make it reproduce it first
A bug prompt in three parts, a failing test you can read in ten seconds, and a reusable command so you never skip the step. About fifteen minutes to set up.

“Fix the login bug” is a terrible prompt for a coding agent, and a frustrating one to debug when it goes wrong. The agent will read some code, find something plausible, change it, and tell you it’s fixed. Sometimes it is. Sometimes it has patched a bug you didn’t have.
The cure is boring and old: reproduce the failure before changing anything. It works for agents for the same reason it works for people. A bug you can trigger on demand can be checked. A bug you can only describe can only be argued about.
The three-part bug prompt
Anthropic’s Claude Code best-practices guide recommends giving the symptom, the likely location and what “fixed” looks like. Its own example ends with “write a failing test that reproduces the issue, then fix it.” That’s the shape to copy, in any agent:
Users report that login fails after the session times out.
Symptom: after 30 minutes idle, the next request is rejected with
"session expired" instead of getting a refreshed token.
Where to look: SessionAuthenticator.Authenticate() in src/Auth/SessionAuthenticator.cs.
First, write a failing test that reproduces this. Run it and show me
the failure output. Don't change any source code until the test fails
for the reason described above. Then make the smallest fix that turns
the test green, and run the full auth test suite.
Each piece does something. The symptom stops the agent guessing at the problem. The location keeps it from reading half the repo. The failing test is the check the agent can run, which matters because, as the same guide says, Claude stops when the work looks done, and without something it can run, “looks done” is the only signal available.
What the red test looks like
Here’s the bug behind that prompt, as a toy. A session module rejects anyone idle for more than thirty minutes, when it should hand them a fresh token:
public static string Authenticate(Session session, DateTimeOffset now)
{
var idle = now - session.LastSeen;
if (idle > IdleLimit)
throw new UnauthorizedException("session expired");
session.LastSeen = now;
return session.Token;
}
The test the agent should write first is short enough to read in one glance:
[Fact]
public void Idle_session_gets_a_fresh_token_not_a_401()
{
var start = new DateTimeOffset(2026, 10, 9, 9, 0, 0, TimeSpan.Zero);
var session = new Session { Token = "tok-old", RefreshToken = "r1", LastSeen = start };
var token = SessionAuthenticator.Authenticate(session, start.AddMinutes(31));
Assert.NotEqual("tok-old", token);
}
Run it with dotnet test and xUnit reports the failure below (paths trimmed):
Failed Auth.Tests.SessionAuthenticatorTests.Idle_session_gets_a_fresh_token_not_a_401 [187 ms]
Error Message:
Auth.UnauthorizedException : session expired
Stack Trace:
at Auth.SessionAuthenticator.Authenticate(Session session, DateTimeOffset now) in SessionAuthenticator.cs:line 20
at Auth.Tests.SessionAuthenticatorTests.Idle_session_gets_a_fresh_token_not_a_401() in SessionAuthenticatorTests.cs:line 13
Failed! - Failed: 1, Passed: 0, Skipped: 0, Total: 1, Duration: 206 ms - Auth.Tests.dll (net10.0)
Read the error message before anything else. It says UnauthorizedException : session expired, which is exactly the symptom in the bug report. That is the check that matters: the test fails for the reason you described, not because of a typo or a missing fixture.
Then the smallest fix, which is to refresh instead of raising:
if (idle > IdleLimit)
{
Refresh(session, now);
return session.Token;
}
Rerun, and the same test prints Passed! - Failed: 0, Passed: 1. Now the agent has run a check that went red and then green, and you have two pieces of evidence to read instead of one claim to trust.
What goes wrong at the red step
Watch the first failure output closely. It answers a question you’d otherwise take on trust: is the agent looking at the bug you’re looking at?
Three things tend to go wrong, and all of them are cheap to catch before any fix exists:
- The test fails for a different reason than the bug, such as an import error or a missing fixture. The agent then fixes the test setup and calls it done.
- The test passes immediately. Either the bug isn’t where you thought, or the test doesn’t exercise it. Both are useful to know.
- The agent can’t reproduce it at all and proposes a fix anyway. That one is a refusal to accept evidence, and you should stop it.
A fourth shows up later, in the fix. When a test is red, an agent has an easy way out: change the test. Say in the prompt that the test is fixed once you’ve seen it fail, and read the diff for edits to the test file.
Ask for the root cause, not the quiet
A failing build gives agents a second easy way out: make the error stop. The guide’s advice is to ask for the fix to address the root cause and not suppress the error. If a diff adds a try/catch, a skipped test, or a loosened assertion, read it twice.
In the toy above, the quiet fix would be catching UnauthorizedException one level up and returning a default. The test would pass for the wrong reason, and a real user would still be logged out.
When you can’t reproduce it
Some bugs won’t give you a clean failing test: intermittent failures, anything that needs production data. Say so in the prompt and make finding the reproduction the task: “Find the smallest input that triggers this and show it. Don’t change any source files.”
Scope that tightly. The same guide warns that unscoped “investigate” requests make Claude read hundreds of files and fill its context. Pointing at a directory and a symptom keeps the search small, and a subagent keeps the reading out of your main session.
Make the step automatic
Typing the three-part prompt every time is how it stops happening. Claude Code skills turn it into a command. A skill is a SKILL.md file with a name and description, and disable-model-invocation: true keeps it as something only you can run. Save this as .claude/skills/repro-first/SKILL.md:
---
name: repro-first
description: Reproduce a bug with a failing test before fixing it
disable-model-invocation: true
---
Bug report: $ARGUMENTS
1. Find the smallest place in the code the report points to. Read only that.
2. Write a test that reproduces the report. Run it and show me the output.
3. Stop and tell me if the test passes, or fails for a different reason
than the report describes. Do not edit source files yet.
4. Once the test fails for the right reason, make the smallest fix that
turns it green. Do not edit the test.
5. Run the surrounding test suite and show me the result.
Then /repro-first login fails after 30 minutes idle runs the whole routine. The skill format and the $ARGUMENTS placeholder come from the Claude Code docs. This file hasn’t been run against an agent, so treat the wording as a first draft and tune it after a few real bugs.
If you want the check enforced instead of requested, the best-practices guide lists stronger options: set the test command as a /goal condition so a separate evaluator keeps the run going until it passes, or use a Stop hook that blocks the turn from ending until it does. For a second opinion on the fix, have a subagent review the diff in a fresh context. Each step costs more setup and needs less of your attention.
Know when to start over
If you’ve corrected the agent more than twice on the same issue, the session is full of failed approaches, and they bias what comes next. Anthropic’s guidance is to run /clear and write a better opening prompt that includes what you’ve learned. In practice that means pasting in the reproduction, the two things that didn’t work, and why.
A clean session with a sharper prompt usually beats a long one with accumulated corrections. It feels wasteful to throw away forty turns. It’s still faster than the forty-first. If you only want to back out the last attempt, /rewind restores the conversation and the code to an earlier point.
When not to bother
Skip all of this for a typo, a missing null check on a line you can see, or anything where the fix fits in one sentence. The same guide says that if you could describe the diff in one sentence, you can skip the plan, and the same logic applies to the reproduction. The three-part prompt pays for itself on bugs where you aren’t sure of the cause.
Try it today
- Pick a bug from your tracker that nobody has understood yet.
- Paste the three-part prompt into your agent with the real symptom and file.
- Read the first failure output before you read anything else.
- If you liked it, save the skill above and use it on the next one.
If you can’t write the failing test, that’s the part of the bug you hadn’t understood yet.