Debugging AI-Generated Code (and Unsticking Stuck Agents)
What to do when the app breaks and the agent's fixes make it worse: a triage playbook for reading errors, isolating causes, rolling back, and getting the agent working for you again.
Key takeaways
- The error message is data โ feed the whole thing to the agent
- Make the agent prove its fix: 'run it and show me the output'
- Two failed fixes in a row = revert and re-approach, don't keep patching
- Ask the agent to add logging or tests when a bug is invisible
Triage: what kind of broken is it?
Bugs in AI-built apps come in three flavors, each with a different move. Loud failures (error screens, crashed builds) are the easy ones โ copy the full error into the agent. Quiet failures (the button does nothing, data doesn't save) need observability first: ask the agent to add console logging around the failing path, then report what the logs say. Wrong-behavior failures (it works, but not how you wanted) are specification problems โ the fix is a clearer description of the intended behavior, not more debugging.
Make the agent verify, not just claim
Agents optimize for looking done. 'Fixed!' is a claim, not evidence. Close the gap by demanding verification in the same breath as the fix: 'Fix the signup error, then run the app, submit the form, and show me the server output.' Agentic tools that can run commands will actually do this โ and catch their own failed fixes before you have to.
The two-strike revert rule
If two consecutive fix attempts fail, stop the patch spiral. Each failed fix layers new code on a wrong theory, and the codebase drifts further from working. Revert to your last good commit and re-approach fresh: describe the original goal plus what you now know ('the crash happens only for logged-out users'), and let the agent try a clean path. This feels slower; it is dramatically faster.
Shrink the haystack
When the agent can't find a bug, narrow where it looks. Good narrowing prompts: 'The bug appeared after the last commit โ diff it and look there.' 'It works in Chrome but not Safari.' 'It fails only with an empty cart.' Each constraint eliminates most of the codebase from suspicion. You're not writing the fix โ you're doing the detective work only someone who can see the app running can do.
Let tests do the remembering
The bug you fixed has a habit of coming back three features later. Once anything important works โ signup, checkout, saving โ ask the agent to write automated tests for it, and to run the test suite after every change. You don't need to read the tests. You need the agent to get caught the moment it breaks something that used to work.