Review AI-generated code by running automated checks first (build, tests, linter, security scanner), then reading every line with three questions: does each API and package it uses actually exist, does it handle the edge cases, and did it change anything you didn't ask for. Treat it as a pull request from a fast, confident contributor who has never seen your codebase.
The checks below are ordered so the cheap, automatic ones run before you spend attention on reading.
Step 1: Run the automated checks before reading
GitHub's guide on reviewing AI-generated code starts here: "Always run automated tests and static analysis tools first." Let the tools find the mechanical problems so your review time goes to logic.
# examples for a Node project; use your project's own commands
npm run build
npm test
npm run lint
npx tsc --noEmit
Look at more than pass or fail. Read new warnings, and compare the test count to before the change. A suite that went from 212 tests to 209 and still passes is a finding.
Step 2: Read the diff, not the chat
The assistant's summary of what it did is not evidence. Review with git diff (or the pull request view), which shows what actually changed:
git diff --stat
git diff
--stat first: if a request to fix one function touched nine files, find out why before reading any of them. Unrequested edits are common: reformatted files, renamed variables, an "improved" config value, a deleted comment that explained something.
Step 3: Check every import, package and API call is real
Models produce plausible names. GitHub's guide says to "look for hallucinated APIs, ignored constraints, or incorrect logic", and OWASP's entry on improper output handling warns that "the AI may also hallucinate non-existent software packages".
The package case is measurable and dangerous. A USENIX Security 2025 study generated 576,000 code samples with 16 models and found the average share of hallucinated packages was at least 5.2% for commercial models and 21.7% for open-source models. An attacker who registers one of those names gets their code installed by anyone who trusts the suggestion. For every new dependency:
npm view <package-name> name version repository.url time.created
pip index versions <package-name>
- If npm answers
npm ERR! 404 Not Found - GET https://registry.npmjs.org/<name> - Not found, the package does not exist. Do not create it, and don't install a similar-sounding one without checking. - If it exists, check it is the one you meant: the repository link, the age (
time.created), and the maintainers. A package created last month with a name close to a well-known one deserves suspicion. - Check the licence is compatible with your project; the GitHub guide lists this too.
For functions and methods, open the official docs for the version you actually have installed (check package-lock.json, requirements.txt or go.mod). A method that existed in an older major version, or in a similar library, is a common source of code that looks right and fails at run time.
Step 4: Test the edge cases yourself
Generated code usually handles the example in the prompt. Write down the inputs that tend to break things and check each one is handled or deliberately out of scope:
- Empty input,
null/None, an empty list, a zero. - Boundary values: the first and last item, the maximum length, off-by-one ranges.
- Non-ASCII text, very long strings, unexpected types.
- Failure paths: the network call times out, the file is missing, the API returns 429 or 500.
- Concurrency: two requests at once, a retry after a partial write.
If the AI wrote the tests too, read them as carefully as the code. A test that asserts whatever the code currently returns proves nothing. Check that at least one test fails if you break the code on purpose.
Step 5: Look for weakened checks
When an assistant is asked to "make the tests pass" or "fix the error", some changes make the symptom go away without fixing anything. Search the diff for:
- Deleted or skipped tests (
.skip,@pytest.mark.skip,xit), or assertions loosened to match wrong output. GitHub's guide even includes a ready-made question for this: "What was the reasoning behind the code change to delete the failing test?" - Broad exception handlers that swallow errors:
catch (e) {},except Exception: pass. - Type-checker and linter suppressions:
// @ts-ignore,# type: ignore,eslint-disable. - Validation, authentication or rate-limit checks that were removed or bypassed "temporarily".
- TLS verification turned off (
verify=False,rejectUnauthorized: false).
A quick way to find most of these in a diff:
git diff | grep -nE "^\+.*(ts-ignore|type: ignore|eslint-disable|\.skip\(|verify=False|rejectUnauthorized|except Exception)"
Step 6: Review security like any untrusted input
OWASP's advice for LLM output is to "treat the model as any other user, adopting a zero-trust approach". For code, that means checking the same things you would in any review, with extra suspicion:
| Look for | What good looks like |
|---|---|
| SQL built with string formatting | Parameterised queries or the ORM |
| Shell commands built from strings | An argument list, no shell=True |
eval, exec, unsafe deserialisation | Removed, or limited to trusted data |
| User-supplied file paths | Normalised and checked against a base directory |
| Secrets in code, logs or examples | Read from environment or a secret store, never printed |
| HTML built from user input | Escaped by the template engine |
Run your security scanner (CodeQL, Semgrep, Bandit or your language's equivalent) on the change, and ask a pointed question of the assistant or a second model, such as GitHub's suggested "What possible vulnerabilities or security issues could this code introduce?" Then verify the answer; don't accept it.
Step 7: Check it fits the codebase
GitHub's guide asks whether the code "solve[s] the right problem" and follows the project's conventions. Things to look for:
- A new helper that duplicates one that already exists elsewhere in the repo.
- A different error-handling, logging or naming style from the surrounding code.
- A new dependency added for something the standard library or an existing dependency already does.
- Code in the wrong layer, for example a database query inside a request handler when the project has a data-access module.
Most of these come from missing context, not bad intent. Giving the assistant your conventions up front, in a project instructions file and in the prompt, prevents many of them. How to use AI for coding covers that side.
Step 8: Get a second look on risky changes
The GitHub guide recommends teammate review for complex or sensitive changes. An AI reviewer in a fresh session can be a useful extra pass: the Claude Code docs note that a fresh context "improves code review since Claude won't be biased toward code it just wrote". The same docs warn that a reviewer asked to find gaps "will usually report some, even when the work is sound", so tell it to flag only problems that affect correctness or the requirements, and check each finding before acting on it.
A short checklist to paste into a pull request
- [ ] Build, tests, linter, type check and security scan pass
- [ ] Test count did not drop; no tests skipped or loosened
- [ ] Every new package exists, is the intended one, licence OK
- [ ] Every API/method checked against docs for our installed version
- [ ] Edge cases: empty, boundary, invalid, failure paths
- [ ] No new suppressions, swallowed exceptions or disabled checks
- [ ] No secrets, no string-built SQL or shell commands
- [ ] Only the requested files and behaviour changed
- [ ] I can explain every line of this diff
The last line is the real test. If you can't explain why a line is there, ask the assistant, then check the explanation against the docs or by running the code. For why models produce confident wrong answers in the first place, see why AI hallucinates.
Frequently asked questions
- Should AI-generated code be reviewed differently from human code?
- The bar is the same, but the failure modes differ. AI code tends to look clean and confident even when it calls an API that doesn't exist, skips an edge case, or weakens a check to make a test pass. Spend less time on style, which linters handle, and more on whether each call, package and condition is real and correct.
- Can I use an AI model to review AI-generated code?
- Yes, as an extra pass, not as the final reviewer. A fresh session that only sees the diff is less biased toward the code than the session that wrote it. Ask it targeted questions, such as what could break or what security issues the change introduces, then verify its findings yourself, since a reviewer asked to find problems will usually report some.
- What should I do if an AI suggests a package that doesn't exist?
- Don't install a similarly named package as a substitute, and don't publish one yourself. Find out what the code actually needs, then pick a real, maintained package from its official registry page or use the standard library. Invented names are a known attack target: a 2025 USENIX Security study found 205,474 unique hallucinated package names across 16 models, and anyone can register one.
- How much of an AI-generated change should I read?
- All of it, if it is going to production. If a diff is too big to read carefully, it is too big to merge: ask for it in smaller pieces, one concern per change, each with its own tests.