Tôi Là Tùng
Back to Blog

5 AI Coding Mistakes: How to Prevent Spaghetti Code

Discover 5 common mistakes that cause AI models to generate messy, outdated, or broken code. Learn practical workflows to optimize AI output.

5 AI Coding Mistakes: How to Prevent Spaghetti Code | Tôi là Tùng, toilatung, Nguyễn Thanh Tùng, Tùng Sóc Sơn

TL;DR: Discover 5 common mistakes that cause AI models to generate messy, outdated, or broken code. Learn practical workflows to optimize AI output.

AI writes code fast — but it doesn't always write code well. If you are receiving messy, deprecated, or inconsistent outputs from the AI, 90% of the cause lies on the user's side — not the AI.

After hundreds of hours working with Claude, Cursor, and various AI coding tools, I have identified 5 systematic mistakes that cause AI to generate "spaghetti code" — and the specific ways to fix each.

1. Broad Prompts Lacking Context

The Mistake: "Write me a button component."

The Problem: The AI has no idea what framework you are using, whether you prefer Tailwind or CSS modules, TypeScript or JavaScript, dark or light mode, or what sizes and variants you need.

The Solution: Provide complete technical context in every prompt.

Create a Button component for Next.js 16 with TypeScript strict mode.
Tech stack: Tailwind CSS v4, dark theme.
Variants: primary (bg-violet-600), secondary (border outline), ghost.
Props: children, variant, size (sm/md/lg), disabled, onClick.
Style: rounded-lg, hover scale-105 transition, disabled:opacity-50.

The Result: The AI has enough parameters to generate the exact component you need on the first try.

2. Operating Without a System Prompt / Rule File

The Mistake: Starting a fresh chat in your IDE and talking to the AI like a complete stranger every time.

The Problem: The AI has to guess your tech stack, folder conventions, and coding preferences. It fills the blanks with "generic best practices" — not what your specific project requires.

The Solution: Create a CLAUDE.md or .cursorrules file at the root of your project containing your tech stack details, conventions, and strict rules. The AI reads this file automatically and remembers it across chats.

Read the detailed guide: The Art of System Prompts.

3. Requesting Too Much in a Single Prompt

The Mistake: "Build me a complete blog page with a listing feed, post detail page, search, filtering, pagination, dark mode, mobile responsiveness, and SEO."

The Problem: The AI attempts to generate everything at once → resulting in bloated code, logic bugs, inconsistent architecture, and ignored edge cases.

The Solution: Slice the problem into independent, sequential tasks. One prompt = one finished feature.

Session 1: Build the basic shell layout and Header.
Session 2: Build the blog listing page with the Card component.
Session 3: Build the blog post detail page with MDX rendering.
Session 4: Build the search functionality.
Session 5: Optimize mobile responsiveness and add micro-animations.

Each session is smaller → the AI executes better → fewer bugs emerge → you review and test faster.

4. Failing to Review Code Before Accepting Patches

The Mistake: AI generates code → you hit "Accept All" → push to production.

The Problem: AI sometimes:

  • Uses deprecated APIs (e.g., using getServerSideProps instead of App Router Server Components in Next.js 16).
  • Hardcodes secrets instead of reading environment variables.
  • Skips proper error handling.
  • Introduces minor security flaws (such as un-sanitized user inputs).

The Solution: Spend 2-3 minutes scanning every file the AI modifies. You do not need to understand every function — just identify what the file does and flag anything unusual.

A quick review checklist:

  • Are secrets isolated in env variables?
  • Are TypeScript types fully declared?
  • Is there proper error handling for async operations?
  • Does it use the correct framework APIs?

5. Not Resetting the Context Window When the AI Hallucinates

The Mistake: When the AI starts outputting conflicting code or ignores rules, you attempt to "re-explain" the requirements in the same conversation thread.

The Problem: The context window is already saturated with thousands of tokens from previous back-and-forth messages. Adding more explanations only pushes more tokens in, confusing the AI further.

The Solution: Spot the warning signs of context fatigue early:

  • The AI suggests old, rejected solutions.
  • It forgets defined rules (e.g., using incorrect import paths).
  • The code output becomes shorter and less polished than usual.

When this happens: close the chat session, start a new one, copy-paste your CLAUDE.md context or pin the relevant files, and continue. A 5-minute reset saves 30 minutes of frustrated debugging.

Conclusion

Spaghetti code generated by AI is almost always the result of a poor workflow, not a poor AI. Fix these 5 points, and you will see your AI's code quality improve immediately.

Want to learn the entire Vibe Coding system from the ground up? Check out Claude Code Mastery Pro — the bootcamp teaching prompt patterns, workflows, and the mindset to make AI work for you.

💡 Read related articles:

🎁 Playbook Miễn Phí

Tải Playbook Vibe Coding: Setup Cursor & Claude Code Chuẩn

SOP hướng dẫn thiết lập Brain file (.cursorrules / CLAUDE.md) tối ưu token và cách chia nhỏ bài toán để AI sinh code không lỗi.

Nguyễn Thanh Tùng — AI System Designer
Written by Tùng
Founder, TVT Agency