Debugging AI-Generated Code
Learn to identify and fix common problems in AI-generated code. Master error reading, rollback strategies, and when to start fresh.
Things will break. That’s not a failure of vibe coding — it’s a normal part of building software. The difference between someone who gives up and someone who ships is knowing how to fix things quickly.
This lesson teaches a systematic approach to debugging AI-generated code, even if you don’t fully understand the code itself.
🔄 Quick Recall: In the previous lesson, you built a complete app step by step, committing after each feature. Those commits become your safety net in this lesson — you can always roll back to a working state.
The Debugging Mindset
When something breaks, resist two urges:
- Don’t panic and start over. The problem is usually one small thing.
- Don’t paste “fix it” without context. You’ll get a guess, not a fix.
Instead: read the error, understand what broke, and give the AI specific information.
Step 1: Read the Error
Most errors tell you exactly what’s wrong. You just need to find them.
Browser Console Errors
Open your browser’s developer tools (F12 or right-click → Inspect → Console):
| Error Type | What It Means | Example |
|---|---|---|
| TypeError | Code tried to use something that doesn’t exist | Cannot read property 'map' of undefined |
| SyntaxError | Code has a typo or structural problem | Unexpected token '}' |
| ReferenceError | Using a variable that was never defined | bookmarks is not defined |
| NetworkError | Failed to connect to a server or API | Failed to fetch |
How to Share Errors with AI
Bad:
“My app is broken”
Good:
“When I click the ‘Add Bookmark’ button, the browser console shows:
TypeError: Cannot read property 'map' of undefinedat line 42 in BookmarkGrid.jsx. The error started after I added the tag filtering feature.”
Include: what action triggers the error, the exact error message, which file and line, and what change caused it.
✅ Quick Check: The browser console shows
ReferenceError: tags is not definedat line 15. Without looking at the code, what’s likely the problem? (Answer: Line 15 uses a variable calledtagsthat was never created or imported. The AI probably referenced a variable from a different file without importing it, or renamed it during a refactor. The fix is to find wheretagsshould come from and import or define it.)
Step 2: The Rollback Strategy
If you committed after each feature (as taught in the previous lesson), you have checkpoints:
Commit 1: Layout ✓ (working)
Commit 2: Add bookmarks ✓ (working)
Commit 3: Tag filtering ✓ (working)
Commit 4: Search ✗ (broke something)
Roll back to Commit 3. Your layout, bookmarks, and tags still work. Now try the search feature again with a fresh approach.
In a code editor (Cursor/Claude Code):
git log --oneline # see your commits
git checkout [commit] # go back to a working state
In Lovable/Bolt.new: Use the built-in version history to restore a previous working version.
Rule of thumb: If you’ve spent 15 minutes debugging and it’s getting worse, roll back. It’s faster to redo one feature from a working state than to untangle cascading bugs.
Step 3: The Fresh Conversation Fix
When the AI keeps making things worse, the problem is often context poisoning:
- The AI made an error in message 5
- Messages 6-10 reference the error as if it’s correct
- Every “fix” is building on a broken foundation
Solution: Start a brand new conversation. Include:
I'm building a bookmark manager with [tech stack].
Here's my current working code: [paste relevant file or describe the state]
I need to add search functionality.
Previous attempt broke the tag filtering. Here's the error I was getting:
[paste error]
Please implement search WITHOUT modifying the tag filtering logic.
The fresh context means the AI doesn’t carry forward any previous confusion.
✅ Quick Check: You’ve been debugging in the same AI conversation for 20 messages. Each fix creates a new problem. What should you do? (Answer: Stop. Start a fresh conversation. The current conversation is poisoned — the AI is building on a chain of errors. Roll back to your last working commit, start a new chat, and describe the feature you want with only relevant context.)
Common Vibe Coding Bugs
| Symptom | Likely Cause | Fix |
|---|---|---|
| Blank page | JavaScript error preventing render | Check console, fix the specific error |
| Styling broken | CSS conflict or missing class | Ask AI to check for duplicate class names |
| Feature stopped working | New code overwrote old code | Compare current file with last commit |
| Data disappears on reload | Not saving to storage/database | Check localStorage or database writes |
| Button does nothing | Event handler not connected | Ask AI to verify the onClick/onSubmit wiring |
| Infinite loading | API call failing silently | Check Network tab in dev tools |
The Debugging Prompt Template
When asking AI to fix something, use this structure:
BUG REPORT:
What I expected: [describe desired behavior]
What actually happens: [describe the actual behavior]
Error message: [paste exact error from console]
File and line: [if visible in the error]
When it started: [what change introduced the bug]
IMPORTANT: Only modify the code needed to fix this bug.
Do not refactor or change unrelated code.
The last line is critical — without it, the AI might “helpfully” refactor working code while fixing the bug, creating new problems.
When to Ask for Help
If you’ve tried:
- Reading the error
- Rolling back
- Fresh conversation with specific context
- The bug still persists
It might be a genuine complexity issue. Options:
- Search the error message online (someone has probably encountered it)
- Ask in a community forum (r/webdev, Discord servers, Stack Overflow)
- Consider if the feature needs a different technical approach
Practice Exercise
- Build a simple counter app (button that increments a number)
- Ask the AI to add a “reset” button — but deliberately give a vague prompt
- When it produces something unexpected, practice the debugging cycle: read error → describe bug → fix
- Try breaking the app by asking for a conflicting feature, then practice rollback
- Start a fresh conversation and fix the issue cleanly
Key Takeaways
- Always read the actual error message before asking AI to fix something
- Include specific context: what you expected, what happened, the error, and what change caused it
- Roll back to your last working commit after 15 minutes of unsuccessful debugging
- Start fresh conversations when context is poisoned (multiple failed fix attempts)
- End every fix prompt with “Only modify what’s needed for this fix — don’t change unrelated code”
- The browser console and version control are your two most important debugging tools
Up Next
In the next lesson, you’ll learn to take your vibe-coded prototype and make it production-ready — handling the gap between “it works on my machine” and “it works for real users.”
Knowledge Check
Complete the quiz above first
Lesson completed!