# Pause, snapshot or stop: choosing a lifecycle for each agent sandbox

> Source: <https://dev.to/sandscale/pause-snapshot-or-stop-choosing-a-lifecycle-for-each-agent-sandbox-3hkn>
> Published: 2026-11-26 03:59:45+00:00

**Recommendation:** Snapshot a sandbox after one-time setup like dependency installs and start each run from that snapshotId, and pause stateful chat sessions instead of stopping them so memory and files survive between turns.
More in the playbook "Designing agent systems": <https://wpnews.pro/playbooks/agent-architecture>

Most teams start with one lifecycle for every sandbox: create, run, stop. That is the right default, and it stays right for a lot of work. But agents have other shapes. A chat assistant waits minutes or hours between turns and wants its Python session intact. A coding agent spends its first few minutes installing the same dependencies every time. Picking the lifecycle per workload, not per platform, saves money and makes runs more predictable.

This post uses Sandscale's TypeScript SDK (`@sandscale/sdk` 1.0.0), but the decisions apply anywhere.

| Operation | What it keeps | SDK call | 
|---|---|---|
| Stop | Nothing; the sandbox is released | `sandboxes.stop(id)` | 
| Pause | Memory and files; resume carries on where it stopped | `sandboxes.pause(id)` ,`sandboxes.resume(id)` | 
| Snapshot | The sandbox's `/workspace` , as a starting point for new sandboxes | `sandboxes.snapshot(id)` , then`sandboxes.create({ snapshotId })` | 
| Idle between calls | The running sandbox; an idle one is hibernated and woken by the next call | automatic; `keepAwake(id, ttlSeconds)` to hold it awake | 

On billing: Sandscale counts awake vCPU-hours. Paused time and idle time between tool calls do not count. Snapshots use storage, which counts against your plan's included storage (the first 10 GB of snapshots and templates is free). Details on [pricing](https://sandscale.dev/pricing).

Create, do the work, stop. Every run starts from the same template, so nothing from a previous run can leak into this one, and nothing this run did survives.

``` js
import { Sandscale } from '@sandscale/sdk';

const sandscale = new Sandscale(); // reads SANDSCALE_API_KEY

const box = await sandscale.sandboxes.create({
  template: 'builtin:python',
  timeoutSeconds: 15 * 60,
  labels: { task: 'grade-submission' },
});
try {
  const result = await sandscale.sandboxes.run(box.id, ['python', 'main.py'], {
    region: box.region,
    timeoutSeconds: 120,
  });
  // hand result.exitCode, result.stdout and result.stderr back to the caller
} finally {
  await sandscale.sandboxes.stop(box.id, { region: box.region });
}
```

**Use it for:** one-shot code execution, grading, CI-style test runs, anything where a run should not see another run's state. The `finally` matters; the `timeoutSeconds` lifetime is the backstop when your process dies before reaching it.

A conversational agent runs code, answers the user, and waits. The next message might come in ten seconds or tomorrow. If the run's state (a loaded dataframe, a running dev server, an open REPL) is worth keeping, pause instead of stopping.

```
// after the agent's turn ends
await sandscale.sandboxes.pause(box.id, { region: box.region });

// when the next user message arrives
await sandscale.sandboxes.resume(box.id, { region: box.region });
const next = await sandscale.sandboxes.run(box.id, ['python', '-c', 'print(len(df))'], {
  region: box.region,
});
```

Pause keeps memory and files, so processes carry on after resume. It works on every plan. Check `box.suspend?.canPause` if you want the server's word on whether pause is possible right now; pausing a container-based sandbox answers `409 pause_unsupported`.

You may not need an explicit pause for short gaps. A sandbox that is idle between two calls is hibernated, its memory compressed in RAM, and woken by the next call. Explicit pause is for gaps where you want the box clearly parked, with its own record.

**Use it for:** chat assistants with stateful sessions, notebooks, agents waiting on a human approval or a slow external system.

**Watch for:** the sandbox's lifetime still applies. Keep it within your plan's session length, and decide what happens to a session that is never resumed (a sweep that stops paused sandboxes older than a day is a good habit).

A coding agent clones a repository and runs `npm ci` before it can do anything useful. If every run does the same setup, do it once, snapshot, and start each run from the snapshot.

``` js
// once, when dependencies change
const base = await sandscale.sandboxes.create({ template: 'builtin:node', network: 'allowlist' });
await sandscale.sandboxes.run(base.id, 'npm ci', { region: base.region, timeoutSeconds: 600 });
const snap = await sandscale.sandboxes.snapshot(base.id, { region: base.region, label: 'deps-2026-11-26' });
await sandscale.sandboxes.stop(base.id, { region: base.region });

// every run
const box = await sandscale.sandboxes.create({ snapshotId: snap.id });
```

The snapshot covers `/workspace`, so put what you want reused there. Each run still gets its own sandbox; the snapshot is only its starting point.

**Use it for:** repeated setup that does not depend on the task. Rebuild the snapshot when the lockfile changes, and label it with the date or the lockfile hash so you can tell which one a run used.

**Watch for:** anything secret in `/workspace` at snapshot time is in every sandbox started from it. Our post on [microVM snapshot pitfalls](https://sandscale.dev/blog/microvm-snapshots-pitfalls) covers this and the entropy and clock issues on restore. Snapshot after `npm ci`, before any credential arrives.

Ask two questions about each workload:

| Workload | Lifecycle | 
|---|---|
| Run a model's code once, return the result | Throwaway | 
| Grade or test user submissions | Throwaway | 
| Chat assistant with a Python session | Pause between turns | 
| Agent waiting on a human approval | Pause | 
| Coding agent on a repository with heavy setup | Snapshot, then throwaway per run | 
| Long-lived personal dev machine | A workspace, not a sandbox | 

Label every sandbox with the task or session it belongs to, so a sweep can find paused or forgotten ones. That one habit prevents most lifecycle surprises.

*Originally published on the [Sandscale blog](https://sandscale.dev/blog/pause-resume-or-stop-agent-sandboxes).*
