Skip to main content
The AG2 logo inside the boxd card. AG2 is an open-source Python framework for building AI agents, formerly AutoGen. Its sandbox tools, SandboxShellTool and SandboxCodeTool, let an agent run shell commands and code in an environment you choose. BoxdEnvironment runs those tools on an isolated boxd machine. It is part of AG2 as the ag2.extensions.boxd extension (pull request), and it ships in AG2 1.3.1 and later.

Set up

1

Install AG2 and the boxd SDK

The anthropic extra is for the example below; use your model provider’s extra instead.
2

Create an API key

Machines are created and billed in the key’s organization.
3

Give the agent a machine

Set ANTHROPIC_API_KEY for the model. The agent’s commands and code now run on the machine, in /home/boxd.

One machine per environment

The first tool call starts a machine, and every later call in the same environment reuses it, so files the agent writes stay there between calls. Leaving the async with block deletes the machine. Without async with, call await env.aclose(). SandboxCodeTool runs python and bash by default. The machine also has Node.js, so pass languages=("python", "bash", "javascript") to let the model run JavaScript too.

Configure the machine

api_key, region, name, image, env_vars and labels also take an AG2 Variable, resolved for each conversation. One environment then serves several tenants, with one machine for each set of values.

What each machine gets

  • Isolation: the machine has no boxd credentials, no metadata endpoint, and no route to your organization’s other machines, so code the model writes can’t use your boxd account. Outbound internet stays on.
  • A deadline: a delete-after timer, one hour by default, deletes the machine even if your process dies before it closes the environment.
  • Commands as the boxd user, who has passwordless sudo. A command’s output is its stdout and stderr, and its exit code comes back unchanged.
  • A timeout: a command that runs past its timeout is stopped and returns exit code 124, without its partial output.
  • A label: every machine carries created-by=ag2, so boxd machine list --label created-by=ag2 shows any that a crashed run left behind.
AG2’s boxd guide covers the same setup, and the code execution page compares boxd with AG2’s other sandboxes.