Run a coding agent in a sandbox
This page runs Claude Code inside a sandbox, end to end. The agent works as a non-root user, can reach only Meadow, and spends from a key that allows one model and has a budget. The same steps fit any agent that speaks the OpenAI or Anthropic format.
Next step: save the Dockerfile and the script below, then run the script. It needs a key with the admin, models and sandboxes scopes, a stored anthropic key, and a stored daytona key.
The image
With outbound traffic limited to Meadow, the sandbox cannot install anything at run time. So the agent, its tools, and its user go into an image. Save this as Dockerfile:
CMD ["sleep", "infinity"] keeps the sandbox up on Modal, where a sandbox ends when the image’s command exits. This Dockerfile copies no local files, so it needs no build context. See Images and build contexts.
The run
Put the files the agent should work on in a folder named task next to the Dockerfile. Ours held one file, calc.py, with a bug:
Output from a run against the hosted beta:
The image build took 53 seconds. With the image built, a run of the whole script took 19 seconds. The agent’s own lines change from run to run.
Why each step is there
- The image is built once.
POST /v1/imageswith the same Dockerfile returns the same image, so the script can call it on every run. - The key is the agent’s budget.
allowed_modelsstops it from calling a dearer model,max_requestsandmax_tokensstop a runaway loop, andexpires_atends the key if your cleanup never runs. The key has only themodelsscope, so the agent cannot create sandboxes or create keys. See Authentication. allow_domainsholds only the host of your base URL. The agent can call models through Meadow and nothing else. Meadow does not add its own host for you. See Network rules.- Claude Code reads
ANTHROPIC_BASE_URL,ANTHROPIC_AUTH_TOKEN, andANTHROPIC_MODELfrom the sandbox’senv.CLAUDE_CODE_MAX_OUTPUT_TOKENSmatters on a key withmax_tokens: without it each call reserved about 145,000 tokens and the first call was rejected. See Claude Code on a key with a token limit. - Uploaded files do not belong to
agent. A tar keeps the owner IDs of the machine that made it, so the script hands/worktoagentwith onechown. Without that step, a write into an uploaded folder asagentfailed withPermission denied. - The agent runs as a background command. A blocking command is cut when your connection drops. A background command keeps running, and the loop reads its output by offset. The
Idempotency-Keymakes a retry of the start return the same command. See Background commands. --dangerously-skip-permissionslets Claude Code edit files and run commands without asking. Claude Code rejects that flag asroot(--dangerously-skip-permissions cannot be used with root/sudo privileges), which is one more reason for theagentuser. Use it only in a sandbox like this one.- The
finallyblock deletes the sandbox and revokes the key. Download what you need first: files and command output go with the sandbox.
Size the key
max_tokens on a key is a lifetime budget of input plus output tokens. Claude Code sends about 17,000 tokens of system prompt and tools with every call, and one task takes several calls. With CLAUDE_CODE_MAX_OUTPUT_TOKENS set to 4096, a one-line prompt reserved 21,099 tokens. The 400,000 token key above was enough for this task. A real task needs more. When a call no longer fits in what is left, Claude Code prints a line like this one, from a key that was too small for a single call:
The key is fine. The budget is too small or spent. Read what is left with GET /v1/key, and raise the limit with PATCH /v1/keys/{key_id} or create a new key with a larger budget. See Authentication. See Token and request limits.
Look inside while it runs
Add "ssh": true to the create request, or "ports": [8000], to reach the sandbox from outside. Both worked on Daytona together with the domain allowlist: the port URL answered 200, an SSH login worked, and example.com was still blocked inside the SSH session. See Files, ports, and SSH.
To follow many runs without polling, set a webhook_url and Meadow sends sandbox.running and sandbox.terminated. There are no events for commands. See Webhooks.
What was not run
- An interactive Claude Code session. Only print mode (
claude -p) ran. - Traffic other than what Network rules lists. The allowlist is the provider’s, and we tried a handful of connections.
- This flow on Modal. The pieces ran there one by one: a sandbox with a domain allowlist called Meadow, and
useron exec worked. - A long task. Ours took a few calls.
More in Known limits.
API reference
Operations on this page: createImage, createKey, createSandbox, uploadFile, execInSandbox, getExec, downloadFile, deleteSandbox, revokeKey.