Example: Vendor coding agent CLIs in a sandbox
You use this pattern when you want real terminal coding agents (for example GitHub Copilot CLI, Claude Code, OpenCode, or Kilo Code, sometimes written “Kilocode”) to work inside an isolated Linux VM instead of on a developer laptop. The sandbox gives you a clean Node-capable environment (typically thecode system image), a persistent workspace directory, and the same exec / filesystem APIs you use for any other automation.
What you accomplish
- You create a sandbox with enough memory for
npm install -g(often 4 GB or more). - You create a workspace under
/work/.... Ifmkdiris denied for the default user, you usesudo mkdirandsudo chown "$(id -u):$(id -g)"so the sandbox user can read and write (andfs.uploadFileworks). - You install the vendor CLI with
sudo npm install -gso packages land in the default global prefix (for example/usr/local/bin) without relying on/tmpas an install location. - You pass provider secrets with
envVarsoncreateSandbox, orenvon eachexec, so keys never live in the command string. - For interactive full-screen TUIs, you attach a PTY (or SSH/VNC per your deployment). For CI-style runs, you use each vendor’s non-interactive or headless flags from their docs.
Tools this guide matches
Install methods change; always confirm the current command on the vendor’s site. Alternatives include
curl | bash installers (Copilot: https://gh.io/copilot-install) or brew, which you can run inside a max image if you need apt/brew-style workflows.
Concepts you use
codeimage: Fast TTI with Node and many runtimes; good default for npm-based CLIs. If a binary needs glibc-heavy dependencies, trymaxor a custom image.sudo: Use it when/workor the global npm prefix is root-owned. Aftersudo mkdir,chownthe tree to the interactive user so uploads and git writes succeed.- Secrets: Map orchestrator secrets into
createSandbox({ envVars: { … } })so every shell in the VM sees them, or passenvonly on theexecthat starts the agent. - Interactive vs headless: TUIs expect a terminal; use Interactive PTY. Unattended jobs need the vendor’s documented flags (print mode, CI,
--helpoutput). - Performance: Repeated
npm install -gin every sandbox is slow; for production, bake the CLI into a custom image and create sandboxes from that image.
Prerequisites
You completed Examples overview. You setVR_API_KEY. Outbound npm registry access must be allowed for installs inside the VM (see Internet access if you restrict egress). Your image must ship sudo (and typically passwordless rules for the sandbox user) if you follow this pattern.
Reference script (TypeScript SDK repo)
The SDK includests-sdk/example/coding-agent-sandbox.ts, which:
- Creates a
codesandbox (4 GB RAM default). - Runs
sudo mkdir -p /work/agent-projectandsudo chownto the current user, then seedsREADME.mdviafs.uploadFile. - Runs
sudo npm install -gforclaude,opencode,kilo, orcopilot(first CLI argument). - Copies known provider env vars from the host into
envVarswhen set (never prints their values).
collectProviderEnvForSandbox and setupVendorCliInSandbox from that file if you want to reuse the same steps from your own orchestrator.
Minimal shape in your own app
Practices you should follow
- You never commit provider tokens; you inject them from a secret store at runtime.
- You pin CLI versions (
sudo npm install -g package@x.y.z) when reproducibility matters. - You read exit codes from
execafter install and after agent runs; surface stderr in your logs. - You combine this with Untrusted user code when the agent can execute arbitrary commands suggested by an end user.
Related guides
Interactive PTY
Terminal UI for CLIs that need a real TTY
Ephemeral dev environment
Throwaway VMs with tooling pre-installed
Custom images
Bake CLIs into the image to skip repeated installs
Multi-step job
Shared disk across clone, install, test

