Skip to content

The thread sandbox

A hosted conversation can have a sandbox: a Linux machine of its own, where the agent runs shell commands, starts a development server you can open by link, and works in one of your GitHub repositories. The sandbox is off unless the deployment turns it on. The terminal client runs commands on your own machine instead; see Terminal client.

The sandbox needs hosted threads (THREADS and FELIX_API_TOKEN), a Durable Object binding named SANDBOX for the class ThreadSandbox, its container, and FELIX_SANDBOX set to on:

"durable_objects": {
"bindings": [{ "name": "SANDBOX", "class_name": "ThreadSandbox" }]
},
"containers": [
{
"class_name": "ThreadSandbox",
"scheduling_policy": "durable_object",
"images": { "sandbox": { "dockerfile": "./sandbox/Dockerfile" } }
}
]

apps/api/wrangler.jsonc binds them in the workers-ai environment, with FELIX_SANDBOX set to on and the npm and Python package registries allowed. Set it to off (in wrangler.jsonc, or as a variable on the deployed Worker) to keep the class and the image but run no sandbox. The workers-ai-sandbox environment, for local development (pnpm dev:worker:sandbox), has it on; the default environment has no sandbox. The migration that creates the class (v4) and the nodejs_compat flag are in every environment. Building an environment with the container builds the image, so pnpm build:worker:ai, pnpm build:worker:sandbox and a deploy need Docker running.

Without the binding, or with FELIX_SANDBOX anything but on, /health does not report sandbox, runs get no shell tools, nothing offers a repository, and every sandbox route answers 503 sandbox_not_configured. A value other than on or off is logged and leaves it off. With both, /health reports sandbox: true. A sandbox is billed while it runs; it stops after FELIX_SANDBOX_IDLE_MINUTES without use, and at most FELIX_SANDBOX_MAX_RUNNING run at once. The container’s own max_instances setting does not apply to sandboxes, because they are scheduled by their Durable Objects, so Felix keeps that count itself.

With a workspace (WORKSPACES), the conversation’s files are in /workspace in the sandbox, and what commands change there comes back to the workspace; see below.

apps/api/sandbox/Dockerfile builds the image: Debian with Node.js 24 (Corepack for pnpm and Yarn), git, Python 3 with pip and venv, and the build tools. Add what your commands need there. It must keep the watch script (watch.mjs, copied in), which watches the sandbox’s files for the side pane, tmux, which keeps the interactive terminal’s shell, and the sandbox-shim helper, copied from the cloudflare/sandbox image whose tag matches the @cloudflare/sandbox version in the pnpm catalog. Wrangler builds the image with Docker when you deploy, so Docker must be running.

Variable Default Does
FELIX_SANDBOX off on turns the sandbox on where SANDBOX is bound.
FELIX_SANDBOX_MAX_RUNNING 20 How many sandboxes may run at once across the deployment (1 to 1,000). Starting another is refused with 503 sandbox_capacity until one stops; one already running is never refused.
FELIX_SANDBOX_MAX_PER_PERSON 5 How many of one person’s sandboxes may run at once (1 to 1,000).
FELIX_SANDBOX_ALLOWED_HOSTS none Hosts commands may read from, separated by commas: registry.npmjs.org, pypi.org, *.pythonhosted.org. *.example.com matches subdomains, not example.com itself.
FELIX_SANDBOX_INSTANCE basic The instance type: lite, basic, standard-1, standard-2, standard-3 or standard-4.
FELIX_SANDBOX_MAX_COMMAND_SECONDS 600 The longest timeout a command may ask for. A command that names none gets 120 seconds.
FELIX_SANDBOX_OUTPUT_KB 32 Output kept from one command: its start and end, with what falls between counted.
FELIX_SANDBOX_IDLE_MINUTES 10 A sandbox with no request for this long stops (at most 360).
FELIX_SANDBOX_PREVIEW_MINUTES 60 How long a preview link works (at most 1,440).
FELIX_SANDBOX_PREVIEW_ORIGIN An https origin for preview links, apart from the app’s. See Previews.
FELIX_SANDBOX_REPO_MAX_MB 500 The largest repository a checkout clones, by GitHub’s own size.
FELIX_SANDBOX_TERMINAL off on lets a conversation’s owner, or an admin, open an interactive terminal in its running sandbox. See Terminal.

A value that does not parse leaves the sandbox off, and the Worker’s log says why.

Limit Value
Sandboxes One per conversation, never shared
Background commands 4 at once; each stops after 12 hours or with the sandbox
Previews 4 per sandbox
Checkout files listed 5,000

Commands reach only the hosts in FELIX_SANDBOX_ALLOWED_HOSTS, over HTTP and HTTPS, with GET and HEAD and without credentials, so npm audit and publishing do not work. Every other request is refused with 403, and nothing leaves the sandbox on another port or protocol. While the sandbox has a repository checked out, GitHub (github.com, api.github.com and codeload.github.com) is reachable too, for reading only. Refused requests are logged by host, never by path or content.

No credential is ever in the sandbox. For a checkout, Felix adds the conversation owner’s GitHub connection token to the sandbox’s requests for that repository as they leave, and only to those. Requests for other repositories go without it, and the sandbox cannot change anything on GitHub through the API.

A run on a hosted conversation gets these tools, with the terminal’s names and arguments:

Tool Does
run_command {command, timeout_seconds?, cwd?} Runs a command with bash -lc and returns its exit code and output.
start_command {command, cwd?} Starts a command that keeps running, such as a development server.
command_output {id, wait_seconds?} Reads a background command’s status and recent output.
stop_command {id} Stops a background command and what it started.
github_push {repo, branch, message?} Commits the checkout’s changes and pushes them to a branch.

The working directory is /repo while a checkout is ready, and /workspace otherwise, unless cwd names /repo, /workspace or a folder in either. The file tools (read_file, edit_file, grep_files and the rest) work on the checkout too, with paths that start /repo/; see Repository checkout. None of these run in plan mode. Admins can turn any of them off, and an agent’s tool list narrows them. No approval is required for commands by default: the sandbox is the conversation’s own machine, it reaches only the allowed hosts, and holds no credentials. An approval rule that names run_command or start_command still applies. github_push always needs a person’s approval, once per call, like the other GitHub writes, and never pushes to the repository’s default branch.

When the sandbox starts and before each command, the conversation’s workspace files are copied into /workspace. When a command ends, what it changed there is written back to the workspace, so those changes are checkpointed and can be rewound like any other, and the side pane shows them. If a person changed the same file in the meantime, the person’s version is kept and the command’s result says so. Folders such as node_modules, .git, .venv and __pycache__ stay in the sandbox, files over 5 MiB are not copied back, and the workspace limits apply; see Workspaces.

The sandbox’s own files last only while it runs. When it stops, for being idle or with the conversation, its next start begins from the workspace again.

While the side pane shows a running sandbox, it watches the files under /workspace and /repo (not .git or node_modules). Recent changes lists the latest paths created, changed, deleted or moved there, and the checkout’s file tree is read again when its files change, or when so many changed at once that some went unreported. The watcher compares snapshots of the folders, woken by the kernel’s file events, so a folder created or renamed with its contents is reported whole; where those events are not available it compares them every two seconds instead. Watching stops when the pane is closed or the browser tab is hidden.

A person signed in with GitHub can open one repository from the deployment’s GitHub page (Manage → GitHub) in a conversation, read or write: from the empty conversation’s Repository line, or from the Sandbox section of Files, with a branch if it is not the default. It is cloned into /repo with their GitHub connection, runs no hooks and follows no submodules, and it is not copied to the workspace.

  • The agent works in it. Commands start in /repo, and the file tools take paths that start /repo/ (read_file {path: "/repo/README.md"}, grep_files {path: "/repo"}). They never read or change .git, never follow a symbolic link, and read or write at most 5 MiB a file. Nothing moves between the checkout and the workspace with rename_file.
  • People work in it. Its files are listed with their git status in Files, and open in the side pane to read, edit, rename or delete. A save is refused when the file changed since it was read, as in the workspace.
  • It stays with the conversation. When the sandbox stops, its copy goes with it, but the conversation keeps the repository: it reads stopped, and the next start, or the agent’s next file tool call or command, clones it again from the same branch. Work not pushed before the stop is lost. A conversation holds one repository at a time; remove it to open another.
  • It is the owner’s. The checkout reads GitHub as the conversation’s owner, so only they can open one, and while one is open only they can start the sandbox or run commands in it. An admin can still see the sandbox, stop it and remove the repository.
  • Changes reach GitHub only through github_push, for a repository listed with write. Its approval names the commit and shows what it sends: the commits, the changed files and the diff against the branch the checkout came from. Felix works this out outside the sandbox, from git objects it checks itself, so code in the sandbox cannot change it. The push can only add to a branch, never replace its commits. A push of more than 200 commits or 300 files is refused. Files that are not text, or are over 512 KB, are listed without their contents, and a diff over 32 KiB is cut; the approval’s first line says so. Its result links GitHub’s comparison and suggests a pull request with github_create_pull_request.

With FELIX_SANDBOX_TERMINAL=on, the side pane offers Open a terminal while the sandbox runs. It is a shell in /workspace, as root, in a tmux session the sandbox keeps: closing the terminal or reloading the page leaves it running, and the next terminal returns to it. It has the same network limits as commands, and a push to GitHub still needs an approval. What you type there is not checked by approval rules or seen by the agent, so tell the agent what you changed.

Opening a terminal takes two requests. POST /v1/threads/{id}/sandbox/terminal, with the person’s own sign-in, answers a ticket that opens one terminal within a minute; then a WebSocket to …/sandbox/terminal?ticket=… opens it, since a browser cannot send a sign-in token on a WebSocket. Only the ticket’s hash is kept, and using it removes it. Each terminal opened is recorded in the audit log as sandbox_terminal_opened, with the person the ticket was issued to.

A server in the sandbox that listens on 0.0.0.0 can be opened with a preview link. A preview link is public: anyone who has it can open the preview, without signing in, until it expires or the sandbox stops. Pages are served without your cookies or sign-in, in an isolated origin, and cannot set cookies. Links that start with / leave the preview, so use relative links, or a base path, in the app you preview.

By default previews are served on the app’s own origin, kept apart from it by the browser’s sandbox and by filtering their response headers. To give them a hostname of their own, set FELIX_SANDBOX_PREVIEW_ORIGIN (such as https://previews.example.com), add that hostname to the Worker as a custom domain, and block every path on it that does not start with /previews/ at the edge, since the web app’s files are served before the Worker. The Worker answers nothing else there, and sends an older link to it. It may not be one of FELIX_AUTH_ORIGINS; a value that is not a bare https origin turns the sandbox off.

Stopping a conversation’s run ends the command it was running, with every process that command started, and copies nothing it half-wrote back to the workspace. The sandbox keeps running, with its files, installed packages, background commands, previews and checkout, until it has been idle for FELIX_SANDBOX_IDLE_MINUTES or is stopped with Stop in the sandbox panel (the side pane’s Files tab) or POST /v1/threads/ID/sandbox/stop, which copies its changes to the workspace first. Deleting a conversation destroys its sandbox.

The conversation’s owner, admins and the operator token can use these:

Route Does
GET, POST, DELETE /v1/threads/{id}/sandbox Status, start, destroy.
POST /v1/threads/{id}/sandbox/stop Copies changes back and stops it.
POST /v1/threads/{id}/sandbox/exec {command, cwd?, timeoutSeconds?} runs a command to completion.
GET, POST /v1/threads/{id}/sandbox/commands Lists or starts background commands.
GET, DELETE /v1/threads/{id}/sandbox/commands/{commandId}?wait= Reads (waiting up to 60 seconds) or stops one.
POST /v1/threads/{id}/sandbox/sync Copies changes back to the workspace now.
GET, POST /v1/threads/{id}/sandbox/previews; DELETE …/previews/{port} Preview links.
GET, POST, DELETE /v1/threads/{id}/sandbox/repo; GET …/repo/files The checkout and its files.
GET, PUT, DELETE /v1/threads/{id}/sandbox/repo/file?path=; POST …/repo/file/rename One checkout file: read, write (conditionally on expectSha256), remove, move.
GET /v1/threads/{id}/sandbox/repo/diff The checkout’s commits and diff against its base branch.
POST /v1/threads/{id}/sandbox/terminal; GET …/terminal?ticket=…&cols=…&rows=… A terminal ticket, then the terminal’s WebSocket. See Terminal.
GET /v1/threads/{id}/sandbox/watch What changes under /workspace and /repo, as server-sent events: watching, then changes batches (each path once, at most 200, truncated when more), and stopped when the sandbox stops.

Starting, stopping and destroying a sandbox, checkouts, pushes and preview links are recorded in the audit log as sandbox_started, sandbox_stopped, sandbox_destroyed, sandbox_checkout_opened, sandbox_checkout_restored, sandbox_checkout_removed, sandbox_repo_written, sandbox_repo_removed, sandbox_repo_renamed, sandbox_pushed, sandbox_preview_opened, sandbox_preview_closed and sandbox_terminal_opened, with the conversation, the repository, the branch, the path or the port. Commands, what is typed in a terminal, file contents and output are never recorded.

A sandbox is billed as a container while it runs: memory and disk for its instance type, CPU for what it uses, and network egress, beside the Durable Object and Worker requests that drive it. A sandbox that has stopped costs nothing. Smaller instance types and a shorter idle time cost less. See Cloudflare’s Containers pricing for the rates.

wrangler dev runs each sandbox as a Docker container on your machine, so Docker must be running. The first start builds the image. Use the workers-ai-sandbox environment, which has the binding:

pnpm dev:worker:sandbox