What I Do When AI Generates Garbage Code

I use Claude Code and Antigravity almost every day. Most of the time they’re great — they scaffold components, write boilerplate, spot bugs I missed. But sometimes? They generate absolute garbage.

Functions that call methods that don’t exist. Import statements for packages I never installed. React patterns in my Angular codebase. The kind of code that makes you whisper “what am I even looking at” to your monitor.

After months of this, I’ve developed a decision tree. Here’s what I actually do.

Step 1: Identify the Pattern

First thing I ask: is this a one-off hallucination or a pattern?

If the AI invented a function name that sounds plausible but doesn’t exist — that’s a hallucination. Common with less popular libraries. The tell: it looks right, even reads right, but it’s completely made up.

If the code uses a completely wrong approach — like generating jQuery inside an Angular component — that’s a context failure. The AI lost track of what stack I’m on.

And if the structure is fine but the logic is subtly wrong — off-by-one, inverted condition, missing edge case — that’s a reasoning error. The AI understood the shape but not the meaning.

Step 2: Fix Manual, Don't Fight the Bot

For hallucinations or context failures, I don’t argue. I just fix it manually.

Trying to get Claude Code to “correct” a hallucinated function usually makes things worse. It invents a different wrong function. Or explains confidently why the imaginary package should work. My best results: copy the working parts, paste into my editor, and write the fix myself. Thirty seconds. Arguing takes 5 minutes and leaves me frustrated.

Step 3: Give Targeted Feedback

If the logic is wrong but the shape is right, I give specific feedback. Not “this is wrong” — useless.

I say: “The filter on line 12 catches case A but misses case B where status is pending. Add an explicit check.” Specific, one line, with context. Claude Code almost always gets it on the second try with feedback like this.

Step 4: Abandon and Rewrite

Sometimes the code is so wrong that fixing it takes longer than starting over. I recognize this moment by the third back-and-forth. If I catch myself thinking “maybe if I rephrase the prompt…” for the third time — I stop.

I write it myself. The AI still gave me value: it helped me understand the problem. Even when its solution was trash.

The 30-Second Rule

If I can’t fix it in 30 seconds, I rewrite it. The AI’s job isn’t to get everything right. It’s to get me 70% of the way there fast enough that 30 seconds of cleanup is still a net win.

The days I get frustrated are the days I forget this rule and spend 5 minutes debugging AI-generated code I could have rewritten in 2. Now I just nod, delete the garbage, and move on.

If you use AI coding tools daily, give this framework a try. And if you have your own tricks for dealing with AI-generated nonsense, I’d love to hear them.

Related: Prompts Are Code Now: My Claude Opus 4.8 Playbook.

Related: Post 3: Setting Up Claude Code — The Brain Behind the Agent.


Discover more from Susiloharjo

Subscribe to get the latest posts sent to your email.

Discover more from Susiloharjo

Subscribe now to keep reading and get access to the full archive.

Continue reading