Quality gates for AI-generated code
A practical, layered defense so that faster code generation doesn't mean more production incidents.
Key takeaways
- Treat AI code like code from a fast, talented, overconfident new hire.
- Layer automated gates first, then a human review that focuses on intent and risk.
- Keep changes small. Review quality drops sharply with PR size.
AI can produce code faster than any team can carefully read it. Without guardrails, you get more output and more incidents. The fix is a pipeline of quality gates every change must pass, no matter who or what wrote it.
#Gate 1: Fast automated checks
- Formatter, linter and type checker.
- Build must pass with zero warnings in changed files.
- Cost: seconds. Catches: syntax, style and type errors, unused code.
#Gate 2: Tests
- Unit tests for new logic, written or reviewed by a human.
- Coverage on changed lines, not just the whole repo.
- Mutation testing on critical modules, if you can afford it.
Warning: AI will happily write tests that pass by asserting whatever the code currently does. Review tests for intent: would this test fail if the behavior were wrong?
#Gate 3: Security and dependencies
- SAST, secret scanning and dependency checks.
- Block new dependencies unless a human approves them. AI sometimes invents package names.
#Gate 4: Human review
Focus human attention where machines are weak:
- Does it solve the right problem? Compare it to the spec.
- Is it the right design? Watch for duplication of existing utilities, a common AI habit.
- What happens when it fails? Check errors, timeouts and retries.
- Could I explain this code to a colleague? If not, don't merge it.
#Gate 5: Safe release
- Feature flags for anything user-facing.
- Canary or percentage rollouts.
- Automatic rollback on error-rate spikes.
#Keep the batch size small
Review effectiveness collapses for large changes. Aim for:
- < 400 changed lines per pull request.
- One purpose per pull request.
- Stacked PRs for bigger features.
Ask your AI assistant to work in small commits, and tell it why: "Each step must be reviewable in five minutes."
#A team policy you can copy
Note: "AI-generated code is welcome. The author who opens the PR is accountable for every line, must be able to explain it, and must ensure it passes all gates. Large unreviewable PRs will be sent back to be split."
Keep reading
What AI-DLC is, how its three phases work, and how to adopt it without losing control of quality.
The Security Development Lifecycle in plain language, plus the new threats that come with AI-generated code and LLM features.
Write the spec first, let AI plan and build from it, and keep the spec as the source of truth.