Suryanand/ Bootcamp
suryanand.com ↗Let's talk
← AgentsAI guideAdvanced#agents#coding-agents#sandboxing#security
Frame · ai-engineering / agents / 11-coding-agents-and-sandboxes.mdGuide 035 / 103

Coding agents and sandboxed execution

Coding agents are the most successful agent category so far, and for a good reason: code gives the agent a fast, objective feedback signal. It edits a file, runs the tests, reads the failure, and tries again. The same property that makes them effective (they execute things) makes them risky, so this doc covers both halves: how the loop works and how to contain it.

Why it matters

A coding agent with shell access can install packages, call the network, read your SSH keys and delete your repository. Most of the time it will not, but "most of the time" is not a security model. Prompt injection makes it worse: a README, an issue comment or a dependency's docs can contain instructions the agent might follow.

Anthropic's engineering post on Claude Code sandboxing frames the requirement clearly: you need both filesystem isolation (so a compromised agent cannot modify sensitive files) and network isolation (so it cannot exfiltrate data or download malware). Either alone is insufficient, because each can be used to escape the other.

There is also a practical payoff. A well-sandboxed agent can be given more autonomy with fewer permission prompts, because the blast radius is bounded by the sandbox rather than by a human clicking "allow" on every command.

How it works

The core loop of a coding agent is ReAct with a specific toolset:

  • Read and search: list files, grep, read file ranges.
  • Edit: targeted string replacement or patch application, rather than rewriting whole files.
  • Execute: run shell commands, tests, linters, type checkers.
  • Plan and track: a to-do list so long tasks stay on course.

Benchmarks like SWE-bench (resolving real GitHub issues in real repositories) pushed the field toward this design, because success is measured by whether hidden tests pass, not by how plausible the diff looks.

Project context matters as much as tools. Many agents read a repository instruction file (for example CLAUDE.md, or the cross-tool AGENTS.md convention) with build commands, conventions and gotchas. A good instruction file saves dozens of exploratory tool calls.

Rendering diagram…

Isolation options

  • OS-level sandboxes. Claude Code's sandbox uses Seatbelt on macOS and bubblewrap on Linux to restrict which paths a command can read or write, and routes network traffic through a proxy that enforces allowed domains. Restrictions apply to child processes too. Anthropic released the runtime as an open research preview. Note its documented limits: it sandboxes shell commands, not every tool or MCP server, and it shares the host kernel.
  • Containers and dev containers. Run the whole agent process in a container with a mounted workspace, no host credentials, and an egress policy. Stronger than per-command sandboxing, a bit heavier to set up.
  • MicroVMs and hosted sandboxes. Services such as E2B start isolated cloud sandboxes on demand where the agent can run code and commands. Good for multi-tenant products where untrusted users' agents execute code.
  • Permission systems. On top of isolation, agent SDKs let you allow, deny or prompt per tool. The Claude Agent SDK, for example, exposes allowedTools, disallowedTools, permission modes and hooks.

Reference architecture

Rendering diagram…

The agent never pushes to the main branch. Its output is a branch and a pull request that goes through the same review and CI as human code.

Code

Two small pieces. First, running agent-generated code in an E2B cloud sandbox from TypeScript, so it never touches your host:

import { Sandbox } from "@e2b/code-interpreter";

const sbx = await Sandbox.create(); // reads E2B_API_KEY from the environment
try {
  await sbx.commands.run("pip install pandas");
  const execution = await sbx.runCode(`
import pandas as pd
df = pd.DataFrame({"x": [1, 2, 3]})
print(df["x"].sum())
`);
  console.log("stdout:", execution.logs.stdout.join(""));
  if (execution.error) console.error("error:", execution.error.name, execution.error.value);
} finally {
  await sbx.kill();
}

Second, driving a full coding agent with the Claude Agent SDK while restricting what it can do:

import { query } from "@anthropic-ai/claude-agent-sdk";

for await (const message of query({
  prompt: "Run the unit tests, fix any failures in src/cart, and summarize the diff.",
  options: {
    cwd: "/workspace/shop",
    allowedTools: ["Read", "Grep", "Glob", "Edit", "Bash"],
    disallowedTools: ["WebFetch"],
    permissionMode: "acceptEdits",
    maxTurns: 30,
  },
})) {
  if (message.type === "result") console.log(message.subtype, message.total_cost_usd);
}

Pair this with the OS sandbox or a container; permission rules decide what the agent may attempt, isolation decides what can actually happen if something goes wrong.

Trade-offs and pitfalls

  • Per-command sandboxes have gaps. Tools that run outside the shell sandbox, such as MCP servers or hooks, may run unconstrained. If you need a hard boundary, put the whole agent in a container or VM.
  • Shared kernel. OS sandboxes share the host kernel; a kernel exploit could escape. For untrusted multi-tenant code, prefer microVMs.
  • Network allowlists are fiddly. Package registries, git hosts and CDNs all need entries. Start strict and add domains as needed, logging denials.
  • Secrets in the environment. Agents read environment variables and dotfiles. Inject short-lived, narrowly scoped tokens and nothing else.
  • "Tests pass" is not "correct". Agents can weaken assertions or special-case tests. Review diffs, and protect test files if the task should not change them.
  • Runaway loops. Cap turns and cost; a flaky test can keep an agent busy for an hour.
  • Large diffs. Ask for small, focused changes. A 2,000-line agent PR will not get a real review.

Checklist

  • Agent runs with filesystem and network isolation, not just permission prompts.
  • Workspace is the only writable path; home directory and keys are not mounted.
  • Egress goes through a proxy with an explicit domain allowlist.
  • Credentials are short-lived and scoped to the task.
  • The repository has an instruction file with build, test and style commands.
  • Turn and cost limits are set per run.
  • Output lands as a branch and pull request with CI and human review.

Further reading

Why this exists README

I learn best by explaining things, so I record the explanations. Everything here is free, with no sign-up. If a lesson helps you ship something, I'd love to hear about it.