How to Review AI-Generated Code Before Merging It

How to Review AI-Generated Code Before Merging It

AI coding assistants write code fast, and that speed is the problem. A diff that looks clean can hide a wrong condition, a call to a function that does not exist, or a query that leaks data. Learning to review ai-generated code is now part of the job, not an extra step you can skip when you are busy. The goal is simple: treat every AI-written line the same way you treat code from a new teammate. You read it, you run it, and you check the parts the author could not know about your system. This guide shows a practical way to do that without turning every pull request into a full-day task.

What AI Code Gets Wrong Most Often

The most common failure is not a syntax error. The real failures are quiet: a condition flipped the wrong way, an off-by-one error in a loop, a sort that runs in the wrong direction. The code compiles, the tests you already had still pass, and the bug only shows up in production data. AI models write code that looks right because they copy patterns from code they have seen. When your case differs from the common pattern by one detail, the model often keeps the common pattern anyway.

The second failure is invented APIs. An assistant will call a library method that sounds right but does not exist, or pass an option the library removed two versions ago. This happens most with libraries that changed recently, because the model learned from older docs. The fix is boring: check every unfamiliar call against the docs or the installed version before you trust it.

// What the assistant wrote:
const user = await db.users.findByEmail(email);
if (user.isAdmin) { ... }

// What the codebase actually has:
// db.users.findByEmail does not exist. The real method is
// db.users.findOne({ where: { email } })

Then there are security holes and licensing problems. Generated code may build a query from raw user input, skip an auth check on a new route, or log a token. It may also copy a chunk of copyleft-licensed code into your closed-source project. None of this looks wrong at a glance, which is why a style-only review misses all of it.

How to Review AI-Generated Code Without Wasting Hours

Start by running the tests before you read a single line. If the tests fail, send the diff back with the failure output. If the project has no tests for the changed area, that is your first finding, not a reason to skip the review. A green run does not prove the code is right, but a red one saves you from reading code that cannot ship.

Next, read the diff, not the prompt. The prompt tells you what someone asked for. The diff tells you what actually changed. Read it line by line, including the trivial-looking parts. AI diffs often include a small change in an unrelated file, a renamed variable that quietly changes behavior, or a deleted check that looked like cleanup. A good habit from reviewing a pull request like a senior developer is to ask, for each hunk, what breaks if this line is wrong. If you cannot answer, that line needs a test or a comment.

Keep the review short but complete. Check the happy path, then check the edges: empty input, a missing record, a user without permission, a slow network call. If the code calls an outside service, check timeouts and retries. This is the same discipline behind a useful code review checklist, applied with one extra question: what did the model assume about my system that might not be true?

An AI Code Review Checklist You Can Reuse

Use this ai code review checklist on every AI-written pull request. It takes about ten minutes once it becomes habit.

  1. Run the full test suite and the linter. Do not merge on a green check from the assistant alone.
  2. Read the whole diff line by line, including deletions.
  3. Check every new dependency: it exists, the version is pinned, and the license fits your project.
  4. Search for invented APIs: confirm each new library call exists in the installed version.
  5. Test edge cases by hand: empty lists, null values, bad input, no permission.
  6. Look for secrets, tokens, and personal data in logs and error messages.
  7. Check auth on every new route and every changed permission check.
  8. Ask whether the change matches the codebase style and naming, or quietly adds a second way of doing the same thing.

If an item fails, say so in the review with the file and line number. Vague comments like “check this” waste a round trip. Point at the exact line and state what is wrong and what you expect instead.

Security Holes Worth a Second Look

Security is where ai coding assistant risks are highest, because insecure code often looks identical to secure code. The classic case is a query built by pasting user input into a string. It works in every test and leaks data in production. Parameterized queries and ORM methods exist for a reason. If you see string concatenation anywhere near a query, stop and fix it.

// Risky: user input goes straight into the query string
const q = `SELECT * FROM orders WHERE id = ${orderId}`;

// Safer: use a parameter
const q = "SELECT * FROM orders WHERE id = $1";

Check auth and permission logic twice. Assistants often add a new endpoint and forget the auth check, or copy a check from a nearby route that protects a different resource. Verify the check matches the resource being served, not just that a check exists somewhere in the function. Also look for tokens and keys: generated code sometimes hardcodes a test key, prints a secret in a log line, or stores a password in plain text. None of these fail a test.

Different assistants catch different classes of bug, so it helps to know which AI catches more bugs in each category before you rely on one for security review.

Licensing and Dependency Problems

An assistant can invent a package name that sounds real. It can also pick a real package that was abandoned three years ago, or suggest a version with a known vulnerability. Before you merge, check that each new dependency exists in the registry, that the version is pinned, and that someone still maintains it. A quick look at release history and open issues tells you more than the model ever will.

Licensing needs the same check. Generated code can include a block copied from a project under a copyleft license, or a snippet that requires attribution your project does not give. If your project is closed source, a copyleft snippet is a real problem, not a style issue. When the origin of a block is unclear, rewrite it in your own words or find a permissively licensed source.

When to Reject the Diff and When to Fix It

When you review ai-generated code, reject the diff when the approach is wrong, even if the code works. If the assistant solved the wrong problem, added a new pattern the codebase does not use, or touched five files to change one behavior, send it back with a clear note about the approach you want. Fixing a wrong approach line by line costs more than regenerating it with better instructions.

Fix small issues yourself when the approach is right. A wrong variable name, a missing null check, or a test that needs one more case are all faster to fix in the review than to send back. Note the fix in a comment so the author learns the pattern. The line between the two is simple: if you can describe the fix in one sentence, make it. If you need a paragraph, send it back.

Setting a Team Policy for AI-Generated Code

A team policy turns all of this from personal habit into shared practice. Write down three rules: every AI-written diff gets a human review, no AI-written code merges without tests, and the author of the pull request owns the code, not the model. The last rule matters most. “The AI wrote it” is never an answer to a bug report. The person who opened the pull request is responsible for every line in it.

Add one more rule: the pull request description says which parts the assistant wrote. That tells the reviewer where to look hardest and keeps the history honest. Teams that adopt AI code review tools as part of the pipeline catch routine issues before a human opens the diff, leaving human review time for logic and security.

Finally, verify ai-generated code the same way after every model update. A new model version can change style, invent different APIs, or shift what it gets strict or loose about. A checklist that worked for last month’s model still works, but only if you actually run it.

Ditulis oleh

Firdaus Safari Putra

Founder, PT FirKaizen Digital Solutions

Firdaus Safari Putra adalah founder PT FirKaizen Digital Solutions. Ia membangun dan mengoperasikan infrastruktur server di balik panduan ini, dan menulis berdasarkan apa yang benar-benar ia jalankan, uji, dan ukur — bukan teori.