Skip to main content

Lesson 5 of 10 · Own the code AI writes

AI code mistakes how to spot code that's almost right

The most common AI code mistakes aren't errors. The code runs, looks finished and is almost right. Eight shapes it comes in, and how to spot each one.

Broken code is easy. It doesn't run, you see an error, and you or the AI fix it. The code that costs money is the code that runs. It passes a quick click-through, it looks finished, and it's wrong in a way you'll only find out about when a customer does.

That's almost right. Here are the shapes it comes in, with what each one looks like on the page, so you can recognise it in your own project.

1. Hallucinated packages and functions

An AI will sometimes tell you to install a package that has never existed, or call a function a library doesn't have. It's not lying. It's predicting what a package with that job would probably be called.

This happens more than you'd think. Researchers generated 576,000 code samples and found that the average share of hallucinated packages, ones that don't exist, was "at least 5.2% for commercial models and 21.7% for open-source models". They counted 205,474 different made-up package names. That study was first published in 2024 and the models have moved on since, but the reason it happens hasn't.

A made-up function just fails, which is annoying but safe. A made-up package is a risk, because anyone can publish a real package under a name an AI keeps inventing. Install it and you run their code. Security people call that slopsquatting.

What to do: before you install anything new, look it up on npmjs.com. Check it's the package you meant, that it's maintained, and that plenty of people use it. If you can't find it, don't install it.

2. Old ways of doing things

AI learned from years of code on the web, and most of that code is old. So it often writes the way things used to be done. I wrote about this in AI builds websites like it's 2015.

Next.js is a good example, because it changed how apps are built. Older Next.js apps put pages in a pages/ folder. Newer ones use the app/ folder you met in lesson 2. They work differently, and AI mixes them up:

// In an app/ project, this is the old way and won't work
import { useRouter } from "next/router";

// This is the one the app/ folder uses
import { useRouter } from "next/navigation";

What to do: know which version of your framework you're on (it's in package.json), and when something looks unfamiliar, check the docs for that version. If the AI's code uses getServerSideProps in an app/ project, it's written for the old way.

3. Errors swallowed

This one is everywhere in AI-written code:

try {
  await saveOrder(order);
} catch (error) {
  console.log(error);
}

return { success: true };

Read it slowly. If saving the order fails, the error is printed somewhere nobody's looking, and then the function says it worked. The customer sees "Thanks for your order". There's no order.

What to do: search your project for catch. For each one, ask: when this fails, who finds out? The customer should see that something went wrong, and you should be told. "Printed to a log nobody reads" isn't an answer.

4. Checks in the browser only

You met this in lesson 3. A form that checks the email address in the browser, and a server that saves whatever it's sent. Or a price worked out in the browser:

const total = basket.reduce((sum, item) => sum + item.price * item.quantity, 0);

await fetch("/api/checkout", {
  method: "POST",
  body: JSON.stringify({ items: basket, total }),
});

The browser works out the total and sends it to the server. Anyone who knows how to open developer tools can send a different total.

What to do: the server should work out anything that matters (prices, discounts, stock, who's allowed to do what) from data the visitor can't change. The browser's sums are for showing, never for charging.

5. No check on whose data it is (IDOR)

This is the most serious one on the list, and it looks completely normal:

// app/api/orders/[id]/route.ts
export async function GET(request: Request, { params }: { params: Promise<{ id: string }> }) {
  const { id } = await params;
  const order = await db.order.findUnique({ where: { id } });
  return Response.json(order);
}

Give it an order number and it returns that order. Whose order? Anyone's. Log in as one customer, change the number in the address, and you can read another customer's order. It has a name, an insecure direct object reference, and it's one of the commonest security holes on the web.

What to do: every route that returns or changes one person's data should find out who's logged in, and only look in their data. In this case, that means getting the user from the session and adding them to the search: where: { id, userId: user.id }. If you use Supabase, this is what its Row Level Security is for, and lesson 6 covers it.

6. Only the happy path

AI writes for the case where everything goes to plan. Ask what happens when it doesn't:

  • The list is empty. Does the page say "No orders yet", or show a blank space, or crash?
  • The network is slow. Can the customer press "Pay" twice and be charged twice?
  • The other service is down. What does the visitor see when Stripe or the email provider doesn't answer?
  • The input is odd. A name with an apostrophe, an email with a plus sign, a 2,000-word message.

What to do: for each form and each button that does something, try the unhappy version on purpose. Disabling a button while it's working, as the form in lesson 3 did, is one line and stops most double clicks. The server should still refuse to charge twice for the same order: Stripe Checkout, or an idempotency key on the payment request, is how it does that.

7. Money in the wrong kind of number

Open your browser's console and type 0.1 + 0.2. You'll get 0.30000000000000004. That's not a bug in your browser. It's how computers store most decimal numbers, and it means sums of pounds and pence can come out a fraction off.

That's why payment services count in the smallest unit: Stripe takes amounts in pence, so £12.50 is 1250. AI-written code sometimes passes pounds where pence are expected, so a £50 order is charged as 50p, or it multiplies by 100 twice and charges £5,000.

What to do: wherever money is worked out, check whether the numbers are pounds or pence, and that every step agrees. Then make a real test payment in test mode and check the amount.

8. Tests that test nothing

AI is quick to write tests, and that's good. Read them anyway. Watch for:

  • A test that replaces the very thing it's testing with a pretend version, so it can only pass
  • A test that checks something trivial, like that a function exists
  • A test that was changed to pass. If the AI's fix for a failing test was to edit the test, find out why

What to do: when an AI changes a test, read the change with more care than any other. A failing test is information. Editing it away loses that information.

The almost-right checklist

Run this on anything an AI has written before you trust it:

  1. Does every package it added exist, and is it the one you meant?
  2. Does it use the current way of doing things for your framework's version?
  3. When something fails, who finds out?
  4. Is anything that matters (prices, permissions, stock) worked out on the server?
  5. Does every route that touches one person's data check who's asking?
  6. What happens on the unhappy path: empty, slow, down, odd input, pressed twice?
  7. Where money is involved, is it pounds or pence, and does every step agree?
  8. If tests changed, why?

If you built something with AI

What to check

  • Run the checklist on the part of your app that handles your three things that must never go wrong, from lesson 1.
  • Search your project for catch and read every one.
  • Log in as one test user, find an address with an ID in it (an order, a profile, an invoice), and change the ID. You should see an error, not someone else's data.
  • Look up every dependency you don't recognise on npmjs.com.

If you're learning the trade

Try it

Ask an AI tool to build something small with money in it: "a basket where you can add items, change quantities and see the total, with a checkout API route".

  1. Before reading the code, write down which of the eight mistakes you expect to find.
  2. Read the code with the checklist and mark every one you find, with its line.
  3. Ask the AI to review its own code for those eight problems. Compare its list with yours.

Then fix the worst one yourself, without asking the AI for the code. Ask it to review your fix afterwards.

Want a hand with your own code?

One to one, we go through what you built together and decide what to keep.