
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
anthropic extra is for the example below; use your model provider’s extra instead.2
Create an API key
3
Give the agent a machine
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 theasync 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
boxduser, who has passwordlesssudo. 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, soboxd machine list --label created-by=ag2shows any that a crashed run left behind.