Vibe Coding
Vibe Debugging: How to Fix AI-Generated Code You Don't Fully Understand
A step-by-step method for fixing bugs in code an AI agent wrote: reproduce, make the agent explain, isolate, instrument, check the docs for your version, and decide when to rewrite. With copyable prompts.
Published 2026-06-18.
The app worked yesterday. Today a page is blank, a form silently fails, or a user reports something you cannot reproduce. The code was written by an AI agent, and you do not know it well enough to guess where to look.
That situation has a name now: vibe debugging. It is a different skill from writing code, closer to detective work. You do not need to understand every line. You need a method that narrows the problem down until the cause is obvious, and you need to use the agent as an investigator, not just as a fixer.
Why "just ask the agent to fix it" often fails
The natural move is to paste the error and say "fix this". Sometimes that works. When it does not, it tends to fail in a loop: the agent changes something plausible, a new error appears, it changes something else, and an hour later the code is worse and you understand it less.
The loop happens because the agent is guessing without evidence. The method below gives it evidence.
Step 1: Reproduce it reliably
You cannot fix what you cannot trigger on demand. Write down:
- The exact steps to cause the bug.
- What you expected to happen.
- What actually happened, including the exact error message.
- Where it happens: local, preview or production; which browser; signed in or not.
If it only happens in production, check whether environment variables, data or third-party settings differ. A large share of "works locally, breaks in production" bugs are configuration, not code.
Step 2: Make the agent explain before it edits
Before any changes, ask the agent to explain the code path involved. This often surfaces the bug, and it gives you the mental model you need.
Do not change any code yet.
Bug: [steps to reproduce]
Expected: [x]
Actual: [y, with the exact error]
1. Trace the code path from [the user action] to [where it fails]. List
each file and function involved, in order.
2. Explain what each step does in plain language.
3. List the three most likely causes, with the evidence for each.
4. Tell me what to log or check to confirm which cause it is.Read the trace. If the agent points to a file that has nothing to do with the feature, its understanding is off, and any fix it writes will be too.
Step 3: Isolate the problem
When you do not know the codebase, cut the search space in half, repeatedly:
- Use version control. If it worked yesterday,
git logandgit diffshow what changed.git bisectcan find the exact commit that broke it. - Disable half of the suspect feature. Does the bug persist? Then the cause is in the other half.
- Build a minimal reproduction: a single page or test that triggers the bug with as little code as possible.
Change one thing at a time and re-test after each change. Stacking five "maybe fixes" makes it impossible to know which one mattered.
Step 4: Make the invisible visible
When you cannot reason about the data flow, log it at the boundaries:
// Before a call you are investigating
console.log('[checkout] creating session with', { priceId, userId });
// After an external call (API, database)
console.log('[checkout] session result', { id: session?.id, status: session?.status });
// In every catch block
console.error('[checkout] failed', error);Also use the tools you already have:
- The browser's Network tab shows whether a request was sent, what it returned and how long it took.
- The Console shows client-side errors and failed resources.
- Your host's function logs show server-side errors that the browser never sees.
Remove or reduce noisy logs once the bug is fixed, and never log secrets or full user records.
Step 5: Check the docs for the version you actually have
Agents sometimes use an API that has changed, a method that was renamed, or a pattern from an older major version. This is especially common with fast-moving libraries and frameworks.
- Check the installed version:
npm list [package]. - Read the official docs for that version, not a blog post.
- Search the library's GitHub issues for your exact error message.
- Tell the agent the version and paste the relevant doc section into the prompt.
We are on [library] version [x.y.z]. Here is the relevant section of the
official docs for this version: [paste]. Does our code at [file:line]
match it? If not, show the minimal change.Step 6: Fix it, then prove it
Once you know the cause, ask for the smallest change that fixes it:
The cause is [confirmed cause, with evidence]. Make the smallest change
that fixes it. Do not refactor anything else. Then:
- add or update a test that would have caught this bug
- run the type check and tests and show me the output
- explain the change in two sentencesThen verify yourself by repeating the reproduction steps from step 1. A passing test is good; seeing the bug gone in the real flow is better.
When to rewrite instead of fix
Some generated code is not worth debugging. Rewrite a piece when:
- Fixing one bug keeps creating another nearby.
- The code is far longer than the job requires.
- Neither you nor the agent can explain what it does, even after step 2.
- It depends on deprecated libraries or patterns.
Rewrite small and in place: extract what the piece must do as a short spec, write tests for that behavior first, then ask the agent to reimplement it against the tests.
Patterns that break often in AI-generated code
- State spread across too many places. The same data in a context provider, a global store and component state. Simplify to one source of truth.
- Effects with wrong dependencies. In React, a
useEffectwith a missing or unstable dependency causes stale data or infinite re-renders. - Happy-path only. No handling for network failures, empty states or invalid input. Add error boundaries at route level and server-side validation.
- Client-only checks. Validation or permission checks that run only in the browser. Move them to the server; see the vibe coding security checklist.
- Duplicated logic. The agent wrote a new helper instead of using the existing one, and now they disagree.
Tools that catch bugs before users do
- Type checking: run
npx tsc --noEmitbefore every commit. Types catch a lot of agent mistakes early. - Linting with strict rules, so the codebase stays consistent even when the agent is not.
- Tests on the critical paths: sign-up, the core action, payment.
- Error tracking in production, such as Sentry1, so "it broke" arrives with a stack trace.
Put these commands in your agent's context file (AGENTS.md or CLAUDE.md) with a rule to run them before calling a task done. The Codex vs Claude Code guide has a template.
The mindset that helps
You are not the author of this code; you are its manager. Your job is to ask good questions, demand evidence and check the result. That is a learnable skill, and it gets faster with every bug.
If debugging has become the thing that keeps pushing your launch back, read launching an MVP: how to get unstuck. If the tool itself is the problem, compare Replit vs Lovable vs Bolt vs Cursor. Once the app is stable enough to show people, move on to the launch: the AI landing page generator, the AI ad creative generator and how to launch a product.
When it is ready, paste your URL at launchbuddy.ai/scan for a free launch-readiness score and the one gap to fix first.
Sources and notes
- Sentry: https://sentry.io/