Skip to content
Yusuf Bagus Anggara
All posts

Recurring Patterns in AI-Generated Code: Why They Repeat

The same shapes keep showing up in AI-generated diffs — uninvited abstraction, optimistic errors, mock-heavy tests. A field guide.

OCT 25, 2026 · 6 MIN READ · AI, ENGINEERING PRACTICE

Patterns, not incidents

After enough months of reviewing diffs from AI agents — Opencode and Antigravity are the ones I run daily — a strange thing happened. The mistakes stopped feeling random. The same shapes kept showing up, from different sessions, on different projects, for different tickets. Not identical bugs. Identical patterns of thinking, if you can call it thinking.

I wrote the checklist companion to this piece in my last post. The checklist tells you what to look for; this one is about what keeps appearing — and why the tools gravitate toward it, which changes how you correct it.

Six patterns, ordered by how often I run into them.

1. Uninvited abstraction

The agent doesn't just solve the problem. It solves the problem plus the imagined future problem. A one-function need arrives dressed as an interface, a factory, and a config object.

// What the diff contained for "calculate the discounted price":
interface PriceCalculator {
  calculate(base: number, discountPercent: number): number
}

class StandardPriceCalculator implements PriceCalculator {
  calculate(base: number, discountPercent: number): number {
    return base * (1 - discountPercent / 100)
  }
}

export function createPriceCalculator(): PriceCalculator {
  return new StandardPriceCalculator()
}

Why it recurs: training material is full of enterprise codebases where this pattern is correct — because there are three implementations. The agent reproduces the shape without the second and third. My correction is blunt: I delete the interface and the factory, keep the function, and leave a note in the brief that abstraction needs a second consumer. One round of this usually teaches the session.

2. Optimistic error handling

The happy path works beautifully. Around it, the failure path is quietly hollowed out: empty catch blocks, errors logged and swallowed, defaults that render a failure as an empty success. The code doesn't fail loudly — it fails politely, which is worse.

// Typical shape:
try {
  const data = await fetchOrder(id)
  setOrder(data)
} catch {
  // handled — "handled" meaning: nothing happens on screen
}

// What I ask for instead:
try {
  const data = await fetchOrder(id)
  setOrder(data)
} catch (error) {
  setError(toUserMessage(error))   // the screen says something true
  throw error                      // the boundary still knows it failed
}

The root cause is prompt-shaped: nobody told the agent what failure should look like, so it defaults to code that compiles and demos well. Now every brief I write names the failure states explicitly. If I don't, I shouldn't be surprised by their absence.

3. Generic names and the utils.ts land-grab

handleData. processItems. utils.ts that grows to three hundred lines of unrelated helpers. Names that describe the act of programming instead of the domain being programmed.

This one has a compounding cost: in six months, a name like handleData is a puzzle, and the person solving it is whoever's on call. Agents default to these names because they're safe — no name can be wrong if it says nothing. I treat every generic name in a diff as a question: what is this, actually? The real name is almost always more specific than the one offered, and finding it is thirty seconds of work that pays for years.

4. Tests that assert the mock

The suite grows. Coverage climbs. Confidence doesn't. The tell: tests that mock every collaborator and then assert the mocks were called exactly as configured — a perfect mirror of the implementation, blind to whether the feature works.

Behavioral tests survive refactors; implementation tests shatter on contact with the first rename. When I see toHaveBeenCalledTimes(3) as the main assertion, I ask for the test to be rewritten against the return value or the user-visible effect. Keep the mocks where they're unavoidable — network, time — but the assertion should outlive the implementation.

5. Assumed environment

The code reads process.env.API_URL and calls .length on it. In the agent's mental model, the variable exists — of course it does; it was in the example. In production, someone forgot it in one deployment target, and the crash message points at line 47 of a file nobody suspects.

// The diff:
const timeout = Number(process.env.REQUEST_TIMEOUT_MS)

// My review note:
const raw = process.env.REQUEST_TIMEOUT_MS
if (!raw || Number.isNaN(Number(raw))) {
  throw new Error('REQUEST_TIMEOUT_MS missing or invalid — refusing to guess')
}
const timeout = Number(raw)

Related family members: timezone assumptions, default encodings, and anything that differs between a laptop and a build server. The pattern isn't the missing check. It's the trust in a context the agent never saw.

6. Validation that stops at the form

Input arrives through a form. The form validates it — min length, email shape, numbers only. The agent's diff treats the job as done. The API endpoint trusts whatever crosses the wire.

That validation is real, and it's a UI courtesy, not a security boundary. Anyone who's seen devtools can post around it in seconds. The pattern repeats because the form is where the prompt pointed. My rule stands regardless of who wrote the code: the server revalidates, always. If the diff shows validation in exactly one layer, that's the review finding — even when everything else in the file is excellent.

What the tools actually do well

Balance matters, or this turns into a rant. The same agents that produce the patterns above also produce, without supervision: correct scaffolding for configs and standard endpoints; faithful translations — class to hooks, one fetch style to another; test setup that's boring and right; and genuinely useful explanations of unfamiliar code before I touch it. The failure modes live in judgment calls — abstraction, naming, failure design, trust boundaries. The mechanical work is usually fine on the first pass.

The pattern behind the patterns

Every item above is a variant of one thing: code that looks finished but hasn't been argued with. Agents optimize for the plausible draft, because plausible drafts are what good training looks like. Review is where the argument happens.

So no, these patterns aren't a case against AI in the workflow. Mine runs on it daily. They're the case for treating review as the craft it just became — where knowing which shapes recur is worth more than reading every line at the same speed. The lines still get read. But now I know where the bodies are buried, and it turns out there are only ever about six graves.