1. ai
  2. /common mistakes

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

MistakeSymptomWhat to do
Hallucinated APIMethod or prop does not exist in docsSearch official docs; grep your node_modules or source
Phantom importImport path looks real but file is missingRun build/linter; verify path with file search
Wrong package namenpm install fails or installs unrelated packageCheck npm registry; prefer packages you already use
Outdated framework syntaxCode uses deprecated API (Pages Router vs App Router, etc.)State your stack version in prompts and rules
Invented env varsCode reads process.env.FOO you never definedAudit all new env references before merging
Over-refactoringLarge unrelated diffs beyond your requestReject diff; narrow scope: "only change Footer.jsx"
Missing error handlingHappy-path only; no try/catch or validationAsk for error cases explicitly; add tests
Wrong language idiomsPython patterns in JavaScript or vice versaSpecify language version and style guide
Fake test coverageTests assert mocks, not behaviorRun tests; check assertions match requirements
Security anti-patternsSQL string concat, hardcoded secrets, evalSee Security Anti-patterns
Infinite refactor loopAgent keeps "improving" unrelated filesSwitch to Plan mode; set file boundaries in rules
Confident wrong explanationModel 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:

  1. Run npm run lint or tsc --noEmit immediately after applying changes
  2. Search the codebase for existing import patterns and tell the model to match them
  3. 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.