How to Fix AI-Generated Code When It Breaks (3 Moves)
AI-generated code broke your app? Here's the exact playbook: paste the error back in, roll back with git, and know when a fresh chat is your fastest fix.
Sam Okafor is a fictional AI persona, not a real person. This article was written by AI and reviewed by a human editor before publishing. How we work →

Your AI just broke something. The screen is white, or nothing loads, or you're staring at red text that means nothing to you. This is not a sign you're doing it wrong — it happens to everyone. Here's what to do right now.
Why AI-Generated Code Breaks (The Short Version)
AI coding tools don't have a full picture of your project. They see what you've shown them in the current conversation, and nothing else. That gap is where most breaks happen.
It doesn't see your whole project
When you ask an AI to change the login form, it edits the login form. It doesn't automatically know that the login form shares a function with the signup page, or that you have a custom auth hook that depends on a specific prop name. It writes code that's correct in isolation but wrong in context.
It doesn't know what changed two prompts ago
The AI treats each response as a fresh answer. If you made three changes earlier in the chat and something only breaks now, the AI has no reliable sense of which change caused it. It can reason about what you've shared, but it can't rewind. That's your job — and git makes it trivial once you're in the habit.
Move 1: Paste the Error Back In (The Right Way)
This is almost always your first move. It sounds obvious but most people skip it or do it wrong.
Copy the exact error message — don't paraphrase it
Don't type "it says something about undefined." Copy the full error text — every line of it — and paste it directly into the chat. Error messages are precise for a reason. "Cannot read properties of undefined (reading 'map')" tells the AI exactly where to look. "It says undefined" tells it almost nothing.
If you're in a browser, open the developer console (F12 → Console tab) and copy from there. If you're in a terminal, copy the full output starting from where it went red.
Add one sentence of context
After the error paste, add one sentence: what you just did. Something like: "This happened after you changed the login form." or "This started after adding the new API call in the last message." That sentence cuts the AI's search space in half.
Keep it to one sentence. Don't explain your whole app — you'll muddy the signal.
What to do if there's no error message — just a broken UI
Sometimes nothing crashes, it just looks wrong. A button doesn't appear. A form doesn't submit. The layout is broken. In this case, describe exactly what you see versus what you expected:
- "The submit button is gone after your last change. It was there before."
- "The user's name no longer shows in the top right corner."
- "The page is blank after I click login."
Specific beats vague every time. "It's broken" is the least useful thing you can say. "The submit button disappears when the form has an error message" is something the AI can actually act on.
Move 2: Roll Back with Git Before It Gets Worse
If Move 1 fails — the AI tries again and makes it worse — stop. Do not keep prompting. Every new edit adds more to undo later. Roll back now.
See the full git survival guide for vibe coders if you haven't set up git yet or need a refresher on committing. What follows assumes you have at least one commit to go back to.
Check what changed: git diff or just ask your AI tool
Before rolling back, see what the AI actually touched. In your terminal:
git diff
This shows every line that changed since your last commit. Green lines were added, red lines were removed. You don't need to understand the code — you're just looking for how much changed. A small diff means a small rollback. A huge diff means the AI went deep and you should definitely roll back.
Alternatively, in Cursor you can click the file in the sidebar and it will highlight changed lines inline. Either way, look before you roll back — it confirms you're undoing the right thing.
Undo the last commit and start the broken section over
To undo your last commit but keep the files in their current state:
git reset HEAD~1
To wipe the changes entirely and go back to exactly how things were before:
git checkout .
If you committed the AI's broken changes, use git reset HEAD~1 first, then git checkout . to clear anything that's still sitting there uncommitted.
Now you're back to your last known-good state. Re-prompt the AI from there with a cleaner, narrower instruction.
Why this is faster than trying to unpick bad AI edits
When the AI makes a mistake and you ask it to fix the mistake, it often compounds it. It doesn't know what "working" looked like — it only knows what it wrote. Rolling back to a working commit and starting the section fresh almost always gets you to a fix in fewer total prompts than trying to patch an already-broken attempt.
The time it takes to learn two git commands pays back immediately the first time you use them.
Move 3: Start a Fresh Chat
If you've pasted the error, if you've tried re-prompting, and the AI is going in circles — the chat itself is the problem. Long conversations make AI tools less reliable. Context builds up, earlier instructions start conflicting with recent ones, and the model can get stuck optimizing around its own previous responses rather than solving your actual problem.
Signs the current chat is the problem (not the code)
- The AI keeps apologizing and trying variations of the same broken fix
- It's now changing things that were working before you started this conversation
- It's contradicting advice it gave you five messages ago
- The responses are getting longer and more hedged with every attempt
Any of these is a signal: start fresh.
How to carry your context into a new session without losing progress
Don't just close the tab. You'll lose the context of what you were trying to build, and you'll waste the first few messages of the new chat re-explaining your project.
Instead, before you open a new chat, copy these three things from your project:
- A short description of what the feature is supposed to do
- The relevant file(s) — paste the current code, not a description of it
- The exact error or broken behavior you're trying to fix
Then open the new chat and lead with those three things. You'll get a cleaner response than if you'd continued the old thread.
The "summary handoff" prompt — ask the AI to summarize before you nuke the thread
If you're in the middle of a complex session and don't want to lose track of what's been discussed, ask the AI to summarize before you close it:
"Before I start a new chat, give me a one-paragraph summary of what we were building, where we got stuck, and what approaches you already tried."
In Claude Code, you can also use the /clear command to reset the conversation context without starting a new terminal session — useful if you want to keep the same working directory but wipe the chat history.
In , opening a new Composer window starts with a fresh context. The previous Composer tab stays intact if you need to refer back to it.
Paste the AI's summary into the new chat as your opening context. You'll be back up to speed in one message.
Preventing the Next Break
Fixing the current break is urgent. Fixing the pattern is what saves you from doing this every day.
Commit before every significant AI change
Before you ask the AI to do anything substantial — refactor a component, add a new feature, change how auth works — run:
git add .
git commit -m "before AI: [what you're about to ask]"
That's your safety net. If the next AI response breaks something, you have a clean rollback point. This habit alone will cut your debugging time in half.
One task per chat session
Long, multi-feature sessions are where AI tools go wrong. The more context accumulates, the more the AI tries to hold together, and the more likely it is to make an edit in one place that breaks something it touched three messages ago.
Keep each chat to one task. Build the form. Add the API call. Fix the styling. When that task is done and working, commit it and start a new chat for the next thing.
How better prompts reduce break frequency
Most AI-generated code breaks because the prompt was underspecified. "Add a login form" leaves too much to interpretation. "Add a login form with email and password fields that submits to /api/auth/login and shows an inline error message if the response is not 200" gives the AI almost everything it needs.
The guide to writing better prompts for AI coding tools goes deeper on this — it's worth reading once you've got the current fire out. Better prompts mean fewer breaks, which means less time debugging and more time actually building.
Frequently asked questions
What should I do first when AI-generated code breaks?
Paste the exact error message back into the tool that wrote the code. The error text is the most useful context you can give — it usually points the model at the fix faster than describing the problem in your own words.
How do I undo changes when the AI made things worse?
Roll back with git. If you committed before the change, restoring the last working commit reverts the broken edits cleanly. This is why committing small, working steps matters — it gives you a point to return to.
When should I start a fresh chat instead of continuing to debug?
When the conversation keeps repeating failed fixes and the context is full of dead ends. Opening a clean chat with just the current code and the error often resolves it faster than pushing the same stuck conversation further.
From the comments
AI personas · answered by the authorHonestly I don't even know if it's the AI's fault or mine. How can a total beginner tell whether the code broke or I just clicked something wrong?
Start with the one thing you changed most recently, because a break right after an AI edit is almost always the edit, not you. The article's whole framing assumes the last AI change is the prime suspect, which is why noting what you just did is step one.
But what if I made like five changes in the same chat and now I have no idea which one did it?
That's exactly the trap the piece warns about: the AI can't rewind and neither can your memory once changes pile up. The fix is to keep one task per chat and commit between them, so each break has only one recent change to blame.
Pasting the error back into the same model that wrote the bug. What stops it from confidently writing the same broken thing again?
Often nothing, which is why the article treats re-prompting as Move 1, not the only move. If it loops or makes things worse, you stop and roll back with git rather than letting it dig deeper.
So the real fix is just git, and the AI is incidental. Why dress it up as three moves?
Because beginners reach for the chat first every time, and the order matters: a clean error paste fixes the easy cases, and git is the safety net for when it doesn't. Naming both keeps people from either flailing in the chat forever or rolling back changes that a single paste would have fixed.
Every failed re-prompt and fresh chat burns tokens or messages off my plan. Isn't this debugging loop quietly expensive?
It can be, and that's the hidden case for rolling back early instead of re-prompting a stuck thread. The article points out that starting fresh from a working commit usually takes fewer total prompts than patching a broken attempt, so the cheap move and the correct move are the same one.
And the summary handoff before nuking a thread, isn't asking for a summary just one more billed message?
It's one message that saves you several rounds of re-explaining your project in the new chat. The appeal is spending one prompt to avoid five, which is the cheaper path even if it feels like an extra step.
The StackBrief weekly
New reviews and the AI-coding-tool news worth knowing — with our take. One email a week, unsubscribe anytime.
Keep reading

Git Survival Guide for Vibe Coders (No Terminal Needed)
Vibe coders lose hours of work without git. Plain-English guide to init, commit, and push using Claude Code or the GitHub MCP — no terminal needed.
May 10, 2026
Build a Chrome Extension With AI (No Code Experience)
Build a Chrome extension with AI — Claude Code or Cursor scaffolds the files, you side-load it in Chrome and ship your first real browser tool.
May 10, 2026
How to Connect Claude Code to the GitHub MCP Server
Connect Claude Code to GitHub in under 5 minutes using the official GitHub MCP server. Beginner guide — manage issues, PRs, and repos by chat.
May 10, 2026