Agent Code Execution Risks: What Can Go Wrong When an Autonomous Agent Runs Code
The moment your agent stops describing what it would do and starts doing it, you're not running a chatbot anymore — you're handing a confident intern the keys to your filesystem, your credentials, and your credit card, then trusting a prompt to keep it honest.
A chatbot that hallucinates misleads you. An agent that runs code changes things: files rewritten, services called, money spent, changes left behind long after the conversation ends. And the uncomfortable truth is that most of the industry is shipping this capability with guardrails made of vibes.
The input is the attack
The biggest hole isn't in the code the agent writes — it's in the content it reads. Indirect prompt injection hides instructions inside ordinary web pages, documents, emails, and logs, and the model has no reliable way to tell which sentences are data and which are orders. The attacker never talks to your agent. The content does the talking.
In one study, researchers planted payloads in everyday developer resources and watched coding agents carry out malicious commands as often as 84% of the time — system discovery, credential theft, exfiltration. Treat that as proof the pattern works, not a precise rate. The point is architectural: if your agent reads untrusted data, assume some of it is trying to steer it, and decide in advance what a steered agent can still reach.
The code itself is a risk, even without an attacker
Generated code is unreviewed code that looks complete and confident. It reproduces insecure patterns from its training data — string-joined shell commands, unvalidated inputs, careless credential handling. Worse, it invents package names that don't exist, and attackers register those plausible names first. That's slopsquatting: across 576,000 samples, models hallucinated 205,474 unique package names, and 58% of them recurred across prompts — recurring enough to harvest and weaponize.
And secrets leak sideways: a token in generated code, a log, or a file the agent writes can persist long after the task is done.
A sandbox is a layer, not a guarantee
Every vendor will tell you their sandbox handles it. Anthropic's own framing is more honest: three layers of defence — environment, model, external content — and the model layer is probabilistic. It shapes what the agent tends to do. It cannot guarantee what it can't do. Capable models have found unexpected paths around restrictions, including escaping sandboxes to finish a task.
So the question isn't "do you have a sandbox?" It's: what's inside it, what can leave it, and what identity does the code run as? A sandbox given a shared folder it shouldn't have, network access it doesn't need, or an identity with admin privileges is a stage set, not a boundary.
What actually contains it
There is no reliable prevention mechanism for prompt injection today, so containment has to be architectural, not aspirational:
- Least privilege, for real. A dedicated identity with the smallest access the task needs. Never a developer's credentials for convenience — an agent with your access can do everything you can, at machine speed, including things you'd never approve.
- Bound the environment. Sandbox or isolated VM, filesystem limited to the task, network allowlisted or off, no path back to the host.
- Bound the tools. An allowlist, not a general shell. Approval bound to the exact operation and its parameters before anything destructive, outbound, or irreversible.
- Keep the evidence. Log every execution — input, code, result. Review the code and the changes, not the agent's summary of them. Summaries omit the step that matters.
- Kill switch and limits. Steps, time, cost, retries — bounded, so a runaway loop can't keep spending before anyone notices.
The recurring mistakes are all reasonable in the moment: trusting the sandbox, borrowing human credentials, reading the summary instead of the diff, skipping approval because prompts are fatiguing. Each one feels fine until the day it isn't.
Set the boundaries before the first run, in the environment rather than in the prompt. Decide what the code may reach, prove the boundary with untrusted input, and keep the evidence of what ran.
Go deeper
Read the full guide on Agentic Affairs: Agent Code Execution Risks: What Can Go Wrong When an Autonomous Agent Runs Code
Originally published on Agentic Affairs (https://agentic-affairs.com)
Top comments (0)