Skip to content

What's the Best Workflow for Debugging and Fixing AI Agent Coding Mistakes?

My AI agent just broke my entire app. I asked it to fix a simple TypeScript error. It tried. It failed. It tried again. And again. After 15 attempts, my clean 200-line component had ballooned to 600 lines of spaghetti code—and the original error was still there.

Sound familiar?

After 9 months of building applications with AI coding agents, I’ve learned that this scenario is almost inevitable. The question isn’t if your agent will make mistakes—it’s how you handle them that determines whether you ship or sink.

The Problem: Agents Don’t Know When to Stop

Here’s what typically happens:

Error: Type 'string' is not assignable to type 'number'
→ Agent fixes type
→ New error: Property 'id' does not exist on type 'User'
→ Agent adds property
→ New error: Module not found
→ Agent adds import
→ New error: Circular dependency detected
→ Agent "refactors" to fix
→ App crashes completely
→ Agent tries to fix...
→ [LOOP CONTINUES]

Each “fix” adds more code. The agent doesn’t realize it’s digging a deeper hole. It just keeps trying.

The Wrong Approach: “Let Me Try Again”

My first instinct was always: “Ask the agent to try again with more context.”

The previous fix didn't work. Please try again. Here's the error:
[error message]
Also consider [X, Y, Z constraints I forgot to mention earlier]

This almost never works. Why?

  1. Context window fills up: Each retry adds more error messages and “fixes” to the conversation
  2. Agent loses track of original intent: The code drifts further from what you wanted
  3. Technical debt compounds: Quick fixes stack on top of each other

The Right Approach: Rollback, Then Troubleshoot

After breaking more projects than I’d like to admit, I’ve settled on this workflow:

Step 1: Rollback Immediately

Terminal window
# Check what changed
git status
# If it looks bad, rollback without thinking
git checkout -- .
git clean -fd

The Rule: If an agent can’t fix something in 2-3 attempts, it won’t fix it on attempt 4 or 5. Stop the bleeding.

Step 2: Take Manual Control

Before asking the agent to fix anything, I do the debugging myself:

// Add strategic console.logs to understand the state
console.log('DEBUG: userData', userData)
console.log('DEBUG: typeof id', typeof userData.id)
// Or create a minimal reproduction
const testUser = { id: 1, name: 'test' }
processUser(testUser) // Does this work?

I need to see what’s happening before I can explain it to the agent.

Step 3: Provide Precise Context

Now I can give the agent what it actually needs:

The issue is in processUser(). The userData object comes from an API
response where id is a string "123", but our type expects number.
Here's the exact location:
src/services/user.ts line 45
The fix should parse the string to number before processing:
parseInt(userData.id, 10)

Specificity is the difference between a 30-second fix and a 30-minute loop of despair.

Why This Works

┌─────────────────────────────────────────────────────────────┐
│ DEBUGGING WORKFLOW │
├─────────────────────────────────────────────────────────────┤
│ │
│ Agent breaks code │
│ │ │
│ ▼ │
│ ┌─────────────────┐ │
│ │ Try fix 1-2x? │──YES──▶ Maybe it works, keep going │
│ └─────────────────┘ │
│ │ NO │
│ ▼ │
│ ┌─────────────────┐ │
│ │ ROLLBACK NOW │ ← This is the key step │
│ └─────────────────┘ │
│ │ │
│ ▼ │
│ Human debugs: adds logs, isolates problem │
│ │ │
│ ▼ │
│ Human gives agent precise fix instructions │
│ │ │
│ ▼ │
│ Agent applies clean fix to clean code │
│ │
└─────────────────────────────────────────────────────────────┘

The agent is great at applying fixes. It’s terrible at diagnosing complex issues through trial and error. By doing the diagnosis yourself, you let the agent do what it’s good at.

Git Checkpoints: Your Safety Net

The rollback strategy only works if you have something clean to go back to. I commit constantly when working with agents:

Terminal window
# After each successful feature or fix
git add -A
git commit -m "feat: add user authentication"
# Before asking agent to do something risky
git add -A
git commit -m "checkpoint: before refactoring database layer"

If the agent goes off the rails, I’m only losing a few minutes of work, not hours.

Frontend Debugging Is Special

I’ve noticed agents struggle more with frontend issues. Why? They can’t see.

When a backend API returns the wrong data, the agent can read the logs. When a React component renders incorrectly, the agent is flying blind.

My workaround: I take screenshots or copy the exact DOM/output and paste it into the conversation.

The component should show a dropdown, but instead it shows nothing.
Here's what the DOM looks like:
<div class="select-wrapper"></div> <!-- Empty! -->
Expected: dropdown with options visible
Actual: empty div

This gives the agent something concrete to work with.

When to Step In: The 2-Attempt Rule

I have a simple rule: Two attempts, then I take over.

Attempt CountAction
1Let agent try. Maybe it’s simple.
2Agent can try again with more context.
3+Stop. Rollback. Debug manually.

This rule has saved me countless hours of watching an agent pile garbage on top of garbage.

A Real Example

Last week, I asked an agent to add caching to a database query. Here’s how it went:

Attempt 1: Agent added caching logic, but introduced a circular import.

Attempt 2: Agent “fixed” the circular import by moving functions around. Now tests failed.

At this point, I should have rolled back. But I didn’t. I asked for Attempt 3.

Attempt 3: Agent added mock imports to make tests pass. Tests passed, but production build failed with cryptic bundler errors.

The codebase was now a mess. I spent two hours manually untangling the changes.

What I should have done:

  1. After Attempt 2: git checkout -- .
  2. Add my own caching with manual debug logs
  3. Verify it works
  4. Tell agent: “Here’s the working code. Clean it up and add proper error handling.”

The Human Remains in Charge

AI agents are incredibly useful, but they’re not autonomous developers. They’re powerful tools that need human oversight.

The best workflow isn’t about letting agents run wild—it’s about:

  1. Setting up guardrails: Clean Git history, frequent commits
  2. Knowing when to intervene: The 2-attempt rule
  3. Doing diagnosis yourself: Adding logs, isolating problems
  4. Giving precise instructions: The agent applies the fix, you define the fix

The developers who succeed with AI agents aren’t the ones who trust them the most. They’re the ones who trust them just enough—and know exactly when to take back control.

Final Words + More Resources

My intention with this article was to help others share my knowledge and experience. If you want to contact me, you can contact by email: Email me

Here are also the most important links from this article along with some further resources that will help you in this scope:

Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!

Comments