AI coding agents now write patches, open pull requests, and update dependencies. That speeds up work, but it also means code review is now a security control, not just a quality check.

You do not need to ban them. You need to decide what they are allowed to touch, what needs a human, and what gets logged.

Quick Answer: How Do You Keep AI Coding Agents Safe?

Agents work on branches, never main. Humans review auth, crypto, IAM, Dockerfiles, workflows, and dependency manifests. Tokens are short-lived and PR-scoped, environments are throwaway containers, CI must pass with no bypasses, and prompts plus tool calls are logged alongside diffs. Code review is now a security control.

Key takeaways:

  • Big generated diffs get skimmed, and skimmed diffs leak vulnerabilities. Shrink diffs and protect sensitive paths instead.
  • Log the process, not just the result. Prompts, model versions, and tool calls explain how a bad change happened.
  • Never let agents approve their own pull requests. That rule alone stops a whole class of quiet failures.

What changed in the workflow

Before: a human wrote code, tests ran, a human reviewed. Now: an agent proposes large diffs, sometimes across many files, including lockfiles and CI configs. Reviewers skim because the diff is big. That is where vulnerabilities slip in: overly broad permissions, new dependencies, disabled checks, secrets in code.

OpenAI has described automated AI research as a planned milestone (Research acceleration: view inside OpenAI). Even if you do not use their tools, the pattern applies to any coding agent: more generated code means review must be stricter, not looser.

Rules that keep coding agents useful and safe

  • Agents work on branches, never main. No direct pushes. Every agent diff gets a pull request and CI run.
  • Humans approve sensitive files. Require manual review for auth code, crypto, IAM policies, Dockerfiles, CI workflows, and dependency manifests.
  • Scope tokens. Give the agent a short-lived token that can open PRs but cannot publish packages, change repo settings, or add secrets. Details in How to Protect Git Repositories From AI Agent Abuse.
  • Run agents in throwaway environments. Use the sandboxed Docker setup so a malicious dependency cannot reach your laptop credentials.
# Review agent PRs like this, every time
git fetch origin agent/feature-x
git diff main...origin/agent/feature-x --stat
git diff main...origin/agent/feature-x -- package.json Dockerfile .github/workflows/
npm audit --omit=dev
# Merge only after CI passes and a human approves

Set up branch protection in ten minutes

This is the highest-value ten minutes in the whole article. In your repository settings, require pull requests for the default branch, require status checks to pass, and require at least one human review. Then add a CODEOWNERS file so sensitive paths automatically demand the right reviewers:

# CODEOWNERS - sensitive paths need named human reviewers
/auth/**                @your-team/security
/crypto/**              @your-team/security
/.github/workflows/     @your-team/platform
/Dockerfile*            @your-team/platform
/package.json           @your-team/security
package-lock.json       @your-team/security
*.pem *.key             @your-team/security

Replace the team names with yours, commit the file, and enforce it in branch protection so it cannot be bypassed with an admin merge. From that moment, no agent diff touching auth, pipelines, or dependencies can land without the humans you named. Everything else can flow fast.

What to watch in agent diffs

  • New dependencies without lockfile updates or with unfamiliar maintainers.
  • Disabled tests, skipped lint, or --no-verify pushes.
  • Hardcoded keys, tokens, or internal URLs.
  • Broadened IAM, sudo, or container privileges to make something work.

Log what the agent did, not just what it wrote

Code diffs show the result, not the process. Also store the agent prompt, model version, tool calls, and file paths it read. When a bad dependency ships, that trace tells you whether the agent ignored a warning, hallucinated a package name, or copied a malicious snippet from a web page. Keep those traces for at least as long as you keep CI logs. If you run local models for triage, Ollama on Ubuntu keeps that analysis on your own host instead of a third-party API.

FAQ

Should juniors use coding agents? Yes, with guardrails. They are great for boilerplate, but a senior should review security-sensitive changes.

Do AI-generated commits need different CI? No, the same CI, but enforce it. No merging on red, no bypassing branch protection.

What is the one policy to start with? Branch protection plus required review for the sensitive paths listed above. It takes 10 minutes to enable.

Can agents merge their own pull requests? No. Require a human approval on every agent-authored PR, and never let the agent account satisfy its own review requirement. Auto-merge is acceptable only for non-sensitive paths after green CI, and even there a human should own the policy.

How do I review a 4,000-line agent diff without skimming? Ask the agent to split it by concern into stacked PRs first, then review the lockfile, workflow, and permission changes before the application code. If it cannot be split, treat that as a smell and send it back.

What to read next

Pair this with How AI Agents Can Attack Supply Chains and How to Secure AI Agents Running on Linux. For local-model triage of agent logs, see AI-Powered Linux Security Monitor and Run Ollama on Ubuntu.

External resources

Related Tools

Try these companion tools on DevStackIO Tools: