Common Mistakes in AI-Generated Code
Models write plausible code that often does not run. Treat every suggestion as a draft until you verify it. This page catalogs the failures you will hit repeatedly.
Last reviewed: June 2026
Models are trained on snapshots of the web. Package APIs, framework defaults, and model names change faster than training data updates.
Common Mistakes
| Mistake | Symptom | What to do |
|---|---|---|
| Hallucinated API | Method or prop does not exist in docs | Search official docs; grep your node_modules or source |
| Phantom import | Import path looks real but file is missing | Run build/linter; verify path with file search |
| Wrong package name | npm install fails or installs unrelated package | Check npm registry; prefer packages you already use |
| Outdated framework syntax | Code uses deprecated API (Pages Router vs App Router, etc.) | State your stack version in prompts and rules |
| Invented env vars | Code reads process.env.FOO you never defined | Audit all new env references before merging |
| Over-refactoring | Large unrelated diffs beyond your request | Reject diff; narrow scope: "only change Footer.jsx" |
| Missing error handling | Happy-path only; no try/catch or validation | Ask for error cases explicitly; add tests |
| Wrong language idioms | Python patterns in JavaScript or vice versa | Specify language version and style guide |
| Fake test coverage | Tests assert mocks, not behavior | Run tests; check assertions match requirements |
| Security anti-patterns | SQL string concat, hardcoded secrets, eval | See Security Anti-patterns |
| Infinite refactor loop | Agent keeps "improving" unrelated files | Switch to Plan mode; set file boundaries in rules |
| Confident wrong explanation | Model explains why broken code "should work" | Trust the compiler, not the narrative |
Hallucinated APIs
Models invent methods that sound right. Examples:
- React hooks or Next.js helpers that never shipped
- Database client methods with plausible names
- CSS properties from a draft spec
Fix: Before accepting a suggestion, confirm the symbol exists:
# JavaScript / TypeScript
grep -r "useOptimistic" node_modules/react/
# Or rely on your IDE + tsc
npm run build
If the model cites documentation, open the URL. Dead links or 404s are a red flag.
Phantom Imports and Paths
AI loves @/lib/utils even when your project uses a different alias or no alias at all.
Fix:
- Run
npm run lintortsc --noEmitimmediately after applying changes - Search the codebase for existing import patterns and tell the model to match them
- Add alias rules to Project Rules
Version Mismatches
A prompt without version context produces code for "React" which might mean class components, hooks, or Server Components depending on the model's training cutoff.
Fix: Put versions in every task prompt or rules file:
Stack: Next.js 15 App Router, React 19, TypeScript 5.7, Tailwind 4
Do not use the Pages Router or `getServerSideProps`.
Over-Refactoring
Agents optimize for "helpfulness" and may rename files, extract abstractions, or reformat unrelated code.
Fix:
- Use Plan mode for multi-file work; approve scope before implementation
- Explicit boundaries: "Do not modify files outside
src/components/Footer.jsx" - Review diffs file-by-file; revert drive-by changes
When the Model Doubles Down
If code fails and the model insists it is correct, stop debating. Paste the exact error message and ask for a minimal fix. If it fails twice, revert and re-prompt with less context.
Related Guides
- Verifying AI Output: checklist before you merge
- Testing AI-Generated Code: what models fake in tests
- Security Anti-patterns: vulnerabilities models introduce
- AI Coding Mistakes Quick Reference: one-page cheat sheet