AI-generated code rarely fails in an obvious way. It compiles, the happy path works, and the diff looks tidy. The trouble shows up later, in an edge case, a dependency audit, or someone else's access to your data.

Developers seem to know this. In Stack Overflow's 2025 Developer Survey, 66% of respondents said one frustration with AI tools was getting solutions that were almost right but not quite. Another 45% said debugging AI-generated code takes more time.

The 2026 survey asked a different question about trust. 48% said they trust AI output when they can easily verify it. Only 6.6% said they trust it for many tasks, including important work decisions. These figures measure different things, so they should not be treated as a year-to-year comparison.

The practical lesson is simple: don't judge generated code by whether it runs once. Review what it assumes, what it exposes, and what happens when things go wrong.

Here are ten checks worth making before you use it.

1. Check that the output is correct, not just present

Take a bill splitter. It looks fine:

def split_bill(total, people): return round(total / people, 2) shares = [split_bill(100.00, 3)] * 3 print(shares, sum(shares)) # [33.33, 33.33, 33.33] 99.99 

A cent has disappeared. The immediate problem is that each share is rounded independently and nobody receives the remainder. Binary floating-point arithmetic is another reason to avoid using floats for money.

A safer approach is to work in integer cents and distribute the remainder:

def split_bill(total_cents, people): if people <= 0: raise ValueError("people must be greater than zero") base, extra = divmod(total_cents, people) return [base + (1 if i < extra else 0) for i in range(people)] print(split_bill(10000, 3)) # [3334, 3333, 3333] 

Before accepting a function, test three inputs yourself: an empty or zero case, a boundary, and one where you know the correct answer. Then add an invariant. Here, it is sum(shares) == total.

2. Check that every dependency exists and deserves trust

Models sometimes recommend packages that don't exist. That matters because anyone can register an available package name.

A USENIX Security 2025 paper generated 576,000 Python and JavaScript code samples using 16 models. It found average hallucinated-package rates of at least 5.2% for commercial models and 21.7% for open-source models, with 205,474 unique hallucinated package names.

There is also a real-world demonstration. Researcher Bar Lanyado noticed models repeatedly suggesting a Python package called huggingface-cli. He registered an empty, harmless package under that name as an experiment. The Register reported more than 15,000 genuine downloads in three months. Lanyado said he had not identified an actual exploit using the technique.

Before installing a suggested package, check its registry page, official documentation, repository, maintainers and release history. Look for signs that it is the package the model claims it is.

For npm projects, these commands help:

npm view some-package time.created maintainers repository.url npm audit npm audit signatures 

For Python projects:

pip-audit -r requirements.txt 

npm audit and pip-audit check for known security advisories. A clean result does not prove that a package is safe or that it is not malicious. npm audit signatures checks registry signatures and provenance attestations for downloaded packages. That helps verify origin and integrity, not the package author's intentions.

Don't install an unfamiliar package just because an AI suggested it. If you need to inspect it, use an isolated environment.

3. Check for hardcoded secrets

AI assistants often reproduce quickstart examples, and quickstarts sometimes show credentials directly in code:

client = PaymentClient(api_key="sk_live_EXAMPLE_DO_NOT_USE") 

Use an environment variable instead:

import os client = PaymentClient(api_key=os.environ["PAYMENT_API_KEY"]) 

Accessing a missing environment variable this way raises an error rather than silently continuing without a key.

GitGuardian's 2026 State of Secrets Sprawl report found 28.65 million new hardcoded secrets in public GitHub commits during 2025. It also reported a 3.2% secret-leak rate in Claude Code-assisted commits, compared with a 1.5% baseline across all public GitHub commits. GitGuardian cautions that this should not be read as a simple measure of tool failure. Developers still decide what to accept and push.

Scan your project before committing:

gitleaks dir -v . gitleaks git -v 

The first scans files in the working directory. The second scans Git history. See the Gitleaks documentation for options.

You can also enable GitHub push protection, which blocks pushes containing supported secret types. If a real key has already been committed, rotate it. Deleting the line in a later commit does not remove it from the earlier history.

4. Check authorization, not just authentication

Being logged in is not the same as being allowed to access every record.

Broken Access Control remains A01 in the OWASP Top 10:2025. One common example is an API that lets a user change an ID in a URL and view someone else's record.

This Flask route assumes a login layer has already set g.user. It checks whether the invoice exists, but not whether the current user owns it:

@app.get("/invoices/<int:invoice_id>") def invoice(invoice_id): inv = INVOICES.get(invoice_id) if inv is None: abort(404) return jsonify(inv) 

A safer version checks ownership before returning the record:

@app.get("/invoices/<int:invoice_id>") def invoice(invoice_id): inv = INVOICES.get(invoice_id) if inv is None or inv["owner"] != g.user: abort(404) return jsonify(inv) 

Returning 404 instead of 403 is a design choice. It avoids confirming that a record exists.

A related example is CVE-2025-48757, involving insufficient Row-Level Security policies in some Lovable-generated projects. The CVE is marked as disputed: Lovable's position is that customers are responsible for protecting their applications' data. The original research write-up by Matt Palmer describes how missing or insufficient policies could expose data through direct database requests.

For Supabase projects, Row Level Security (RLS) is a key part of protecting data. A public anon key is designed to be exposed to the client; the database policies must still restrict what that client can access.

For example, this policy allows authenticated users to read only their own invoices:

alter table invoices enable row level security; create policy "owners read own invoices" on invoices for select to authenticated using ((select auth.uid()) = owner_id); 

This is a SELECT policy only. Applications that allow inserts, updates or deletes need suitable policies for those operations too. See the Supabase RLS documentation.

The test to add is simple: log in as user A, request user B's record, and expect access to be denied.

5. Check for injection and XSS

SQL built from strings is a classic security mistake. This small example uses an in-memory SQLite database:

import sqlite3 db = sqlite3.connect(":memory:") db.execute("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)") db.executemany( "INSERT INTO users(name) VALUES (?)", [("alice",), ("bob",)] ) def find_user_bad(name): return db.execute( f"SELECT id, name FROM users WHERE name = '{name}'" ).fetchall() def find_user(name): return db.execute( "SELECT id, name FROM users WHERE name = ?", (name,) ).fetchall() evil = "x' OR '1'='1" print(find_user_bad(evil)) # Returns both users print(find_user(evil)) # Returns no users 

The first query treats user input as part of the SQL command. The second passes it as data.

Veracode's 2025 GenAI Code Security Report tested more than 100 models using 80 code-completion tasks across four languages. In its original benchmark, the average security pass rate was 13.53% for cross-site scripting tasks and 12.03% for log-injection tasks. Those are results from a controlled benchmark, not failure rates for every real-world prompt.

Use parameterized queries, your template engine's auto-escaping, and textContent instead of innerHTML when inserting plain text in the browser.

For logs, use structured logging and sanitize untrusted values for control characters such as carriage returns and line feeds. A static analyzer such as Bandit can flag some risky Python patterns, but it does not replace code review.

6. Check validation and error handling

Code that never throws can be worse than code that crashes:

import json def load_config(path): try: with open(path) as f: return json.load(f) except Exception: return {} cfg = load_config("/nonexistent/config.json") print(cfg.get("timeout", 30)) # 30, with no hint that loading failed 

A typo in the path silently turns into default settings. The application may continue with configuration the developer never intended.

A better approach is to let the error reach a place where it can be handled properly:

import json def load_config(path): with open(path) as f: return json.load(f) 

Now FileNotFoundError or JSONDecodeError can be handled at startup, with a useful error message.

Validate input at the boundary too: types, ranges, lengths and allowed values for anything coming from outside your application.

OWASP added A10: Mishandling of Exceptional Conditions as a new category in its 2025 Top 10.

To find some common Python exception patterns, you can search with:

grep -rnE "except( Exception)?( as [A-Za-z_]+)?:" --include=*.py . 

This is a basic search, not a complete exception-handling audit. Review the matches and inspect other exception patterns too.

7. Check for deprecated APIs and version mismatches

Models learn from code written over many years, so they can reproduce old patterns.

For example, datetime.utcnow() has been deprecated since Python 3.12:

from datetime import datetime, timezone datetime.utcnow() # Deprecated since Python 3.12 datetime.now(timezone.utc) # Timezone-aware replacement 

The replacement returns a timezone-aware datetime. Comparing it with an older timezone-naive datetime can raise a TypeError.

Make deprecation warnings visible during tests:

pytest -W error::DeprecationWarning node --trace-deprecation app.js npm outdated 

Also compare generated code with the documentation for the versions in your lockfile. The model may know a library, but it does not automatically know which version your project uses.

See the Python datetime documentation.

8. Check performance and unnecessary complexity

Slow code can look harmless, especially with small test data.

Consider membership checks on a list:

ids = list(range(20000)) wanted = list(range(0, 40000, 2)) result = [item for item in wanted if item in ids] 

For every item in wanted, Python may scan through the ids list. That makes repeated membership checks expensive as the lists grow.

A set is better for repeated membership checks:

ids = set(range(20000)) wanted = list(range(0, 40000, 2)) result = [item for item in wanted if item in ids] 

List membership takes O(n) time on average. Set membership is O(1) on average, although building the set has a cost of its own. The best choice depends on how often you check membership and how much data you have.

Test with realistic data volumes, not just ten rows. For Python profiling, use:

python -m cProfile -s cumtime script.py 

Also ask whether the generated solution is bigger than the problem. A retry framework, three abstraction layers or a new dependency for a ten-line task can add maintenance cost without adding much value.

9. Check that the tests can actually fail

A test that executes code without checking the result does not prove correctness.

Here, the discount function is broken. It divides by 10 instead of 100:

def apply_discount(price, pct): if not 0 <= pct <= 100: raise ValueError("pct must be between 0 and 100") return price - price * pct / 10 # Bug def test_apply_discount_runs(): apply_discount(200, 10) apply_discount(200, 100) def test_apply_discount_values(): assert apply_discount(200, 10) == 180 

The first test passes because it only runs the function. It makes no assertion. The second test fails because the result is 0.0, not 180.

Coverage tells you which lines ran. It does not tell you whether the results were correct.

Try a manual mutation check: deliberately change a constant or flip a comparison, then rerun the tests. If they still pass, your tests may not be checking that behavior. Tools such as mutmut can automate this idea for Python projects.

10. Check that you can explain it

If you cannot explain a block of code to a teammate, debugging it later will be harder.

Before merging, read each function and answer:

  • What does it assume about its inputs?
  • What happens if a dependency fails?
  • Which line could be removed without changing the result?

If you cannot answer those questions, ask for an explanation and verify it against the code. Rewrite parts that are unnecessarily complex.

The goal is not to understand every line of every library. It is to understand the code you are responsible for maintaining.

The AI-Generated Code Review Checklist

  1. Test three inputs you chose: an empty or zero case, a boundary, and one with a known answer. Add an invariant where one exists.
  2. Verify every new package through its registry, documentation and repository before installing it. Run dependency audits, but don't treat a clean result as proof of safety.
  3. Keep credentials in environment variables. Scan the working tree and Git history with Gitleaks. Rotate any key that reached a commit.
  4. For every route that returns user data, test whether user A can access user B's record.
  5. Check every query, HTML insertion and log entry that uses untrusted input. Parameterize, escape or sanitize appropriately.
  6. Review broad exception handlers and validate types, ranges and sizes at input boundaries.
  7. Run tests with deprecation warnings treated as errors. Check unfamiliar APIs against the documentation for your pinned version.
  8. Test with realistic data volumes. Profile slow code and avoid unnecessary complexity.
  9. Make a deliberate change that should break the code. If no test fails, improve the assertions.
  10. Explain what each important function assumes and does. Simplify anything you cannot maintain confidently.

Where this leaves you

AI tools can produce plausible code quickly. That does not remove the need to review it.

You do not need to perform every check manually on every line. Start with the checks that match your stack, automate dependency and secret scanning, and add tests for the authorization and input-handling rules that matter to your application.

The useful question is not whether AI wrote the code. It is whether you have enough evidence to trust what it does.