DEV Community

Cover image for Your agent just wrote code. Where does it run? (Friday AI Question)
M.T.Ramkrushna
M.T.Ramkrushna

Posted on

Your agent just wrote code. Where does it run? (Friday AI Question)

Friday AI Question. This one is about the moment your agent stops talking and starts executing: it wrote some Python, and something has to run it.

When your AI agent generates code or shell commands, where do they run?

Pick one (and tell me in the comments):

  • A) Same machine as the app. subprocess.run(...) and move on.
  • B) A Docker container.
  • C) A stronger sandbox: gVisor, a Firecracker microVM, or a managed sandbox like E2B.
  • D) C, plus no secrets inside, network egress off or allow-listed, CPU/memory/time limits, and a fresh sandbox per task.

My lean: B is the minimum, C for anything untrusted, D is the goal. Here's why.

The model is not the only author of that code

Agent-generated code is shaped by everything in the context window: the user's message, retrieved documents, web pages, tool output. Anthropic's computer use docs say it directly: in some circumstances Claude follows commands found in content even when they conflict with your instructions, for example instructions on webpages or in images.

So "the model wrote it" really means "the model plus whoever got text into the context wrote it." Treat the output like code from an untrusted stranger.

That's why the same docs list, as the first precaution, using a dedicated virtual machine or container with minimal privileges to prevent direct system attacks or accidents. Their reference implementation runs everything inside a Docker container.

Why option A is the risky default

On the app host, generated code gets whatever your app process has: environment variables with API keys, database credentials, the filesystem, the network. One bad rm, one curl with your env piped into it, and the problem is not the agent's output anymore. It's your production box.

What a container buys you (option B), and what it doesn't

Docker's security docs describe the isolation: kernel namespaces mean processes in a container can't see, let alone affect, processes in another container or the host, and each container gets its own network stack. Linux capabilities let you drop most of what root can do.

But the boundary is still the host kernel. The Docker daemon itself requires root unless you opt into Rootless mode, and Docker's docs say only trusted users should control it. A container is good isolation from accidents. For hostile code, you want another layer.

What option C adds

  • gVisor is an application kernel written in Go that runs in userspace. Its runsc runtime plugs into Docker and Kubernetes. The system interfaces normally implemented by the host kernel move into a per-sandbox application kernel, specifically "to minimize the risk of a container escape exploit."
  • Firecracker runs lightweight microVMs with KVM-based isolation. Its site lists only 5 emulated devices, boot in under 125 ms, and under 5 MiB of overhead per VM. It's the technology behind AWS Lambda.
  • Managed sandboxes like E2B give each agent an isolated Linux VM created on demand, which you can pause and resume, through an SDK.

The point: the cost of VM-level isolation is now small enough that "it's too slow" is rarely a good reason to skip it.

Why C still isn't the whole answer (option D)

A perfect sandbox with your production API key mounted inside is still a leak. My checklist for D:

  1. No secrets in the sandbox. If the code needs an API, put a proxy outside that holds the key.
  2. Network egress off by default, or an allow-list of domains. Most exfiltration needs the network.
  3. Hard limits on CPU, memory, disk and wall-clock time. Generated code loops forever more often than you'd think.
  4. Fresh sandbox per task or per user. Don't let one session's files leak into the next.
  5. Run as non-root, drop capabilities, read-only base filesystem.
  6. Log every command the agent ran, with the inputs that led to it, so you can audit after the fact.
  7. Human approval for side effects that leave the sandbox: deploys, payments, emails.

So, A, B, C or D?

I suspect a lot of internal tools and prototypes are still at A, because it worked on a laptop. If you're at D, I'd like to know how you handle network allow-lists for agents that genuinely need to install packages.

Where does your agent's code run today? And what's the scariest command you've seen an agent try?

References

Top comments (0)