Back to blog
Next.js Architecture Claude Code AI Development TypeScript SaaS Engineering

How to Stop AI from Vibe-Coding Your Next.js App into a Mess

By Ken B Portfolio · Published on April 23, 2026
Illustration of a developer working at a laptop

AI coding tools write working code fast. Left alone, they also build an app nobody can safely change a few months later.

The tests pass and the features ship. The mess shows up later: a small change breaks three screens, and each fix costs more than the last.

This is not about AI being bad at code. It is very good at code, and it does not care about structure unless you tell it to.

A real case: what one audit found

I audited O Sequencer, an outreach app built fast with AI help. It had 73 route files: the parts of the server that answer the screens. The audit found 11 kinds of structural problem. In plain words:

  • Business rules buried in the request code. The rules about what the app does sat inside the code that answers web requests, tied to sign-in and payments. They could not be reused, and testing them was expensive.
  • One feature, three ways in. The same data was reached three different ways, so permission checks were repeated and drifted apart.
  • No single way to check input. Only 18 of the 73 route files used the shared error helper. 49 read the raw request and checked fields by hand. Different errors, different answers.
  • Screens that knew too much. Pages knew server addresses and request details, so a server change rippled into many screens.
  • A hidden superuser, and a safety check that switched itself off. One setting created a hidden all-access user. Rate limiting turned itself off when one service was missing: open, not closed.
  • Very few tests. Core code called the payment, AI and sign-in services directly, so tests were expensive to write, and few existed.

None of it came from one bad decision. Each shortcut made sense on its own. Nobody was watching the whole.

What changed: rules the AI reads on every task

Docs and README notes sit outside the coding loop. The AI reads the code around it, plus its instructions file: CLAUDE.md for Claude Code, a rules file for Cursor. So I wrote 15 short rules into that file. The AI follows them on every task, and a review can cite “rule 12” by number.

Some rules still drifted when they were only written down: code kept reaching past a module’s front door. So the important ones are now enforced by tests. One test reads every import in the app and fails on any shortcut.

A job-alert app I built runs under these rules. At its last full run, 1,987 tests passed, and about 1,600 quick ones run in about 12 seconds before every push. A full weekly review still found four real problems, like a payment change that could get lost if the payment provider sent it twice, and admin pages that did not check who was looking. The next day, 17 fixes shipped, 13 with a new test.

Rules don’t replace review. They give the review a list to check against. How I do that review: How I review AI-written code and The weekly review skill.

The 15 rules, in plain words

  • Keep it simple (rules 1–3). Save what happened, not guesses. Use as few statuses as you can. No one-off special cases.
  • Keep the layers apart (rules 4–9). The database, the business rules and the screens each keep their own shapes. Every input is checked. The code that answers requests only passes work along. Each feature has one way in.
  • Make risky things safe (rules 10–13). No hidden shortcuts. If a safety check is down, refuse. Every change to data checks it belongs to the person making it. Two requests at once can’t both spend the same credit.
  • One front door per part of the app (rules 14–15). Other code goes through one door, and the door runs the checks before anything is saved.

The code example for each rule is in the Technical tab.

What to ask a developer who uses AI tools

  • Where are your rules written, so the AI reads them on every task? “In my head” or “in a doc” means the AI never sees them.
  • Which rules does a test enforce? A written rule drifts. A test fails.
  • How do you make sure one customer can never change another customer’s data?
  • If a safety service is down, does the app refuse, or let everything through?
  • What did your last review find, and what changed after?

You don’t need to read the code or learn the rules. Before building starts, I write the rules for your project into the AI’s instructions, and tests check the important ones on every push. Read the development approach.

The short version

AI tools write working code, not a tidy app. Write your rules where the AI reads them on every task, enforce the important ones with tests, and review against them.

What rules does the AI in your project follow today?

KEEPTHEAIINLINE

Send me one file from your app's server code and I'll tell you which of the 15 rules it breaks.

More Posts

All posts