How to review AI-generated code
Review the behavior a change introduces, not how convincing its explanation sounds. Start with permissions, inputs and failure paths.
Key Takeaways
- Behavior Over Syntax: AI code reads cleanly; evaluate whether security boundaries and failure cases hold under hostile inputs.
- Tenancy Validation Rule: Trace parameters from HTTP request bodies into database lookups to prevent cross-account disclosures.
- Negative Testing Protocol: Require automated tests that explicitly assert rejection cases (HTTP 401/403) alongside successful responses.
- Automated Pre-Commit Defense: Use the AI-Generated Code Review Checklist on every diff before merging.
Reviewing AI-generated code is an empirical code inspection discipline that focuses on runtime behavior, authorization boundaries, and unhandled failure branches rather than syntactic cleanliness.
When human developers review human-written code, they scan for typos, syntax inconsistencies, and logical misunderstandings. When reviewing code produced by AI coding agents (Claude Code, Cursor, Copilot, Windsurf), those heuristics fail. Language models excel at producing clean, plausible code that compiles cleanly—while completely omitting authorization boundaries and error handling.
This guide provides a pragmatic framework for inspecting AI-authored code before it reaches production.
1. Define explicit intent before opening the diff
Before reading AI-generated code, write down a one-sentence behavioral contract defining what the code should do and what it must reject:
"An authenticated user can view invoices belonging strictly to their tenant organization; cross-tenant and unauthenticated lookups must return HTTP 404/403."
Reviewing without an explicit behavioral contract is dangerous: because AI code formats convincingly, you will instinctively assume the code is complete.
2. Trace external inputs to their sinks

Consider this typical AI-generated Express handler:
// AI Generated: Passes unit tests but leaks cross-tenant records
app.get('/invoices/:id', requireSession, async (req, res) => {
const invoice = await invoices.findById(req.params.id);
res.json(invoice);
});
The session middleware establishes identity. It proves who the caller is. However, it completely fails to establish authorization. It never checks whether the caller owns the invoice. If a caller submits another account's ID, the endpoint returns the data.
To secure the endpoint, the lookup query must constrain access using authenticated session attributes:
// Secured: Query enforces tenancy boundary
app.get('/invoices/:id', requireSession, async (req, res) => {
const invoice = await invoices.findOne({
id: req.params.id,
organizationId: req.session.user.organizationId
});
if (!invoice) return res.status(404).json({ error: 'Invoice not found' });
res.json(invoice);
});
Our automated OWASP security scanning engine flags missing authorization and SQL injection (CWE-89) patterns on pull requests automatically.
3. Verify negative test assertions
Generative AI tools frequently write tautological tests: tests that mirror the buggy implementation and assert whatever the function produces.
When reviewing test suites accompanying AI code, verify negative boundaries:
- A user can read their own resources.
- A user is rejected when requesting another account's resources.
- Invalid, malformed, or missing payloads receive clean error responses.
- Database timeouts trigger proper rollback mechanisms rather than corrupting state.
If an AI-generated pull request contains no rejection tests, that is an immediate review finding.
4. Inspect failure and edge branches
AI models optimize for the prompt's happy path. Walk each external dependency in the diff:
- Network calls: What occurs if third-party APIs return HTTP 500 or timeout?
- Database queries: What occurs if unique key constraints collide?
- Logging sinks: Are authentication tokens or passwords printed in error stack traces?
- Async concurrency: Are database writes wrapped in transactions, or can race conditions leave state partially written?
5. Automate the first review pass
Human reviewers should spend their energy on architecture, system boundaries, and user experience—not searching diffs for missing null checks.
Automate your first line of defense:
- Inspect changes pre-commit using the VS Code extension.
- Scan pull requests automatically using Pre-Merge Quality Gates.
- Apply automated code fixes with Auto-Fix PR Generation.
- Verify review coverage using our seeded testing corpus in How to Benchmark an AI Code Reviewer.
