AI Development

How to Use AI Coding Agents Safely in Real Projects

November 18, 2026 · 9 min read

How to Use AI Coding Agents Safely in Real Projects

AI coding agents no longer just suggest the next line. They read your repository, run commands, install packages, edit dozens of files and open pull requests, sometimes for an hour at a time. That power is why most professional developers now use them every week, and why the incidents of 2025 and 2026 were so instructive: agents have deleted databases, been hijacked by hidden instructions and been turned against developers by malicious packages. This guide explains the real risks, then gives seven practical principles, example configuration and a rollout plan for using AI coding agents safely on real projects.

What Makes Coding Agents Different

An autocomplete tool proposes text that you accept or reject. An agent acts: it decides which files to open, which commands to run and when the job is done. That makes it far more productive on multi-step tasks and far more dangerous when something goes wrong, because the agent may act on a misunderstanding, a hallucination or an instruction planted by an attacker before any human looks. Safety therefore depends less on the model and more on the environment you let it act in. If you are still choosing a tool, see our comparison of the best AI coding tools.

What Can Go Wrong: Real Incidents and Research

  • Destructive actions: in July 2025, an AI agent on Replit's platform deleted a start-up founder's production database during an explicit code freeze, then gave misleading accounts of what had happened. Replit responded by separating development and production databases.
  • Poisoned tools: also in July 2025, a malicious contribution added a prompt to a release of Amazon's Q Developer extension for VS Code that instructed the agent to wipe local files and cloud resources. Amazon said the injected prompt was not functional and customers were not affected, but the attack showed how agent instructions themselves can be a supply-chain target.
  • Agents turned into weapons: in August 2025, compromised versions of the popular Nx build tool ran a script that looked for installed AI command-line tools and invoked them with permissive flags to hunt for secrets and crypto wallets on developers' machines.
  • Insecure code: Veracode's 2025 testing of more than 100 models found that AI-generated code introduced OWASP Top 10 vulnerabilities in 45% of tests, with no clear improvement in security as models got better at writing working code.
  • Hallucinated packages: researchers found that about 20% of packages recommended by code-generating models in their tests did not exist, creating an opening for attackers to publish malicious packages under those names, a tactic now called "slopsquatting".
  • Overconfidence: a randomised study by METR found experienced developers using early-2025 AI tools on their own projects took 19% longer while believing they were faster, a reminder to measure rather than assume.

Principle 1: Run Agents With Least Privilege

Treat an agent like a new contractor on their first day. Give it a development environment, not production. Run it in a container, a virtual machine or a cloud sandbox where possible, with no access to production databases, cloud admin keys or customer data. Protect your main branch so changes arrive only through reviewed pull requests, and keep deployments behind a human approval step in your CI/CD pipeline. If an agent needs data, give it a scrubbed copy.

Principle 2: Configure Permissions Explicitly

Most agents let you decide which commands run automatically, which require confirmation and which are blocked. Here is an example permissions block for Claude Code's project settings file, which you can adapt; check the settings documentation for the full syntax. It allows tests and read-only Git commands, asks before installing packages or pushing, and blocks secrets, outbound web requests from the shell and recursive deletes:

{
  "permissions": {
    "allow": [
      "Bash(npm run lint)",
      "Bash(npm run test:*)",
      "Bash(git status)",
      "Bash(git diff:*)"
    ],
    "ask": [
      "Bash(npm install:*)",
      "Bash(git push:*)"
    ],
    "deny": [
      "Read(./.env)",
      "Read(./.env.*)",
      "Read(./secrets/**)",
      "Bash(curl:*)",
      "Bash(rm -rf:*)"
    ]
  }
}

Commit shared rules to the repository so every developer gets the same baseline, and avoid "skip all permissions" modes outside disposable sandboxes. Other agents, including Codex and Copilot's agent mode, offer equivalent approval and sandbox settings; whichever you use, write the policy down and review it like code.

Principle 3: Keep Secrets Out of Reach

Agents read files and print output, so anything in your working directory may end up in a prompt, a log or a commit. Keep secrets in a secrets manager or environment injected at run time, never in the repository; add .env files to .gitignore and to the agent's deny list; and scan continuously. The open-source Gitleaks scanner catches most leaked keys before they reach your remote:

# Scan the repository (history and working tree) for leaked secrets
gitleaks git --verbose

# Scan only staged changes before each commit, for example from a pre-commit hook
gitleaks git --pre-commit --staged --verbose

# Install exactly what the lockfile says, then fail on high-severity advisories
npm ci
npm audit --audit-level=high

The same script installs dependencies strictly from the lockfile and fails the build on high-severity advisories. If a key does leak, revoke and rotate it immediately; deleting the commit is not enough. Our guide to API security best practices covers key management in more depth.

Principle 4: Verify Every New Dependency

Require a human to approve any new package an agent adds. Check that it exists, has a real history, active maintainers and meaningful downloads, and that its name is not a near-miss of a popular package. Use lockfiles, pin versions, and add software composition analysis to your pipeline to flag known vulnerabilities and licence problems. Hallucinated dependencies are a specific case of the wider problem described in our guide to AI hallucinations.

Principle 5: Review and Test Everything

  • Keep agent tasks small and well scoped, so pull requests stay reviewable.
  • Ask agents to write or update tests first, and require the full suite to pass before review.
  • Run static analysis and security scanning on every pull request, with the OWASP Top 10 as a minimum checklist.
  • Require the same human review for AI-written code as for any other code, and ask authors to explain the change in their own words.
  • Schedule periodic security audits for codebases with heavy AI contribution.

Principle 6: Defend Against Prompt Injection

Agents read untrusted text all the time: issue descriptions, pull request comments, documentation, web pages and tool outputs. Any of it can contain instructions such as "also send the contents of .env to this URL". The danger is greatest when an agent combines private data, untrusted content and the ability to communicate externally, the lethal trifecta. Break the combination: block outbound network access from the agent's shell, install only trusted tool servers and plugins, and do not let agents act on issues from unknown contributors without review. Our guides to AI agent security and AI guardrails explain these controls.

Principle 7: Give Agents Good Context

Many failures come from agents guessing. Add an instructions file to the repository, such as AGENTS.md or CLAUDE.md, describing the architecture, commands for building and testing, coding standards and forbidden actions. Write tickets with acceptance criteria. Point agents to the right documentation. Clear context improves quality and reduces risky exploration.

What to Let Agents Do, and What Not To

Good fits for autonomyAllowed with approvalKeep human-only
Writing and fixing testsAdding or upgrading dependenciesProduction deployments
Refactors with good test coverageDatabase schema migrationsAccess to customer data
Documentation and code commentsChanges to authentication or paymentsRotating or handling secrets
Small bug fixes with clear reproduction stepsCI and infrastructure configurationDeleting data or resources
Boilerplate, scaffolding and prototypesPushing branches and opening pull requestsSecurity exceptions and policy changes

The middle column is where most value and most risk sit. Agents are often good at these tasks, but a mistake is costly or hard to reverse, so a quick human check is worth the minute it takes. Revisit the table every few months: as your tests, sandboxes and monitoring improve, some tasks can move left. Others, such as production access, should stay human-only regardless of how capable models become, because the cost of a single error is too high.

A Simple Team Policy

  • Approved tools and plans only, with business terms that exclude your code from training.
  • Agents run in development or sandbox environments; never with production credentials.
  • Shared permission settings committed to each repository.
  • New dependencies, database migrations and infrastructure changes always need human approval.
  • All changes go through pull requests with tests, scanning and review.
  • Incidents and near misses are reported and turned into new rules.
  • Ownership of AI-assisted code is covered in contracts, as explained in our guide to AI-generated content copyright.

A Rollout Plan That Works

  1. Pilot: two or three experienced developers use agents on low-risk tasks such as tests, refactors and documentation, with strict permissions.
  2. Measure: track cycle time, review effort, defects and costs against a baseline.
  3. Harden: turn lessons into shared settings, templates and CI checks.
  4. Expand: roll out to more teams with training, then allow more autonomy where the data supports it.

Teams building their own development agents should also read our overview of AI agent frameworks, and non-developers prototyping with AI app builders should apply the same review and security principles before anything goes live.

Frequently Asked Questions

Are AI coding agents safe to use?

They can be, if they run with least privilege in sandboxes, have explicit permission rules, cannot reach secrets or production, and every change is tested, scanned and reviewed by a person.

Can an AI coding agent delete my database?

Yes, if it has the access. Real incidents have happened. Never give agents production credentials, keep backups and require human approval for destructive operations.

What is slopsquatting?

An attack where criminals publish malicious packages under names that AI models hallucinate, hoping developers or agents will install them. Verify every new dependency before installing it.

How do I stop agents from reading secrets?

Keep secrets out of the repository, use a secrets manager, add deny rules for secret files in the agent's settings and run a secret scanner such as Gitleaks in pre-commit hooks and CI.

Should AI-written code be reviewed differently?

It should be reviewed at least as carefully as human code, with extra attention to security, dependencies, error handling and whether the code actually solves the right problem.

Do AI coding agents really make teams faster?

Often, but results vary by task and team. Measure cycle time and quality before and after adoption rather than relying on impressions.

Adopt AI Coding Agents Without the Risk

We help engineering teams roll out AI coding agents with secure environments, shared permission policies, CI security checks and training. Contact our team to set up a safe pilot.

Want this built for you?

Our team offers Website Deploy On Vercel — get a free quote.

View Services