When you put an agent's code execution in a sandbox, you also decide, often without noticing, how many pieces of work share each sandbox. One per tool call? One per conversation? One per user, kept for days? The choice sets your isolation story more than the sandbox technology does. A microVM shared by two users is a shared environment, whatever kernel it runs.
This post compares the three common granularities.
Per run
Every task, or every tool call, gets a fresh sandbox from a template, and the sandbox is stopped when the task ends.
Isolation: the strongest. Nothing from one run can affect another, including a run by the same user. A prompt injection that plants a malicious file dies with the box.
State: none carried over. If a task needs setup (cloning, installing dependencies), every run pays for it, unless you start each run from a snapshot taken after setup.
Cost while waiting: low, because there is no waiting; the box exists only while work happens.
Failure mode: expensive setup repeated thousands of times, and agents that expect state ("the file I wrote last turn") finding it gone. Fix the first with snapshots; fix the second by making the tool's description say plainly that each call starts clean.
Per session
A conversation or agent task gets one sandbox, kept across its turns and stopped (or left to its lifetime) when the session ends.
Isolation: good between users and sessions, none between turns of the same session. That is usually what you want: the agent built a dataframe in turn 3 and wants it in turn 7. It also means that a malicious instruction in turn 3, for example from a web page the agent read, can leave something behind that runs in turn 7.
State: files, processes and memory carry over, which is the point.
Cost while waiting: this is where the platform's billing model shows. Sessions spend most of their time waiting for the user or the model. On a meter that bills provisioned resources while running, an idle session costs as much as a busy one; you control it with pause or aggressive timeouts. On a meter that skips idle gaps, the wait costs less without extra work. See five ways agent sandboxes are billed.
Failure mode: sessions that never end. Give each session sandbox a lifetime and a sweep that stops sessions with no activity for a set period.
Per user
Each user gets a long-lived environment: their own workspace or a persistent sandbox that survives across sessions and days.
Isolation: between users, yes; within one user's history, none. Every session sees what every earlier session left behind, including anything an attacker planted.
State: everything persists. Installed tools, history, configuration.
Cost while waiting: the highest exposure, because the environment exists for days. You need sleep-when-idle behaviour or it will cost money every hour no one uses it.
Failure mode: environments drift and accumulate secrets. Old tokens in shell history, credentials in .git/config, caches with private data. Treat a per-user environment like a laptop: it needs hygiene, rotation and a way to reset it.
The rule that matters most
Never share a sandbox across users. Cloudflare's sandbox security docs put it well: the sandbox "is therefore the smallest unit of trust", and "when two users share a sandbox, each user's code can read the other's work". That holds on every platform. If your mapping from user to sandbox comes from a name or an ID in a request, derive it from the authenticated identity, not from anything the client sends.
Decision table
| Workload | Granularity | Why |
|---|---|---|
| One-shot code execution for an LLM tool call | Per run | No state needed; strongest isolation |
| Grading or running user submissions | Per run | Submissions must not see each other |
| Agent PRs in CI | Per run | Each PR is untrusted on its own; see running untrusted PR code |
| Data analysis chat | Per session | Loaded data must survive between turns |
| Coding agent on one task | Per session | The repo and tools persist for the task's turns |
| A developer's cloud machine | Per user | It is a computer, not a sandbox |
Mixing them
Most real systems use two. A coding agent might keep a per-session sandbox for its working copy and send risky commands, such as running a downloaded script, to a per-run sandbox that has no credentials and no network. The per-session box keeps state; the per-run box absorbs risk.
How these map to Sandscale
- Per run: create a sandbox, run, stop. Each run is its own Firecracker microVM with its own kernel. Start from a snapshot to skip repeated setup.
- Per session: keep the sandbox across turns. An idle sandbox is hibernated between calls and woken by the next one; pause keeps memory and files for longer gaps, on every plan. Paused time and idle time between tool calls do not count against awake vCPU-hours.
- Per user: use a workspace, a persistent cloud computer that opens in the browser or over SSH with short-lived certificates and sleeps when idle.
Label every sandbox with the run, session or user it belongs to. When something goes wrong, the first question is always "what else shared this box?", and labels are how you answer it.
Originally published on the Sandscale blog.
