Skip to main content
run() starts a command and gives you the result once it finishes. spawn() is for when you need to stay connected while it runs: send it input, signal it, and read its output as it arrives. You get back a session and keep it until the process exits.
In Python, spawn() is on AsyncSandbox only; the synchronous Sandbox.commands.spawn() raises and points you there. In TypeScript it works in the browser and in Node 22+. Older Node needs a WebSocket polyfill such as ws.

When to use which

Most of the time you want run(): scripts, builds, anything that takes its input up front and hands back output. Switch to spawn() when the process reads from stdin, when you need to signal it, or when you’re streaming an agent’s output and feeding it more as it goes.

The session

spawn() resolves once the process is running and hands back a session:
  • stdin.write(data) takes a string or raw bytes. stdin.close() sends EOF, which programs like cat or a REPL wait for before they finish.
  • kill(signal) sends a signal, SIGTERM by default.
  • wait() resolves with the result when the process exits.
  • Wrap the session in await using (TypeScript) or async with (Python) and it kills the process and closes the connection when the block ends.
The SDK reference lists the full surface.

Timeouts

spawn() has no timeout unless you set one, so the process runs until it exits or you kill it. Pass timeoutMs / timeout_seconds to stop it at a hard limit. Output doesn’t extend the limit, and when it’s reached, wait() resolves with exit code 124. See Timeouts for how this compares to run().

If the connection drops

wait() throws SandboxError if the socket closes before the process exits. The sandbox then stops the process, and there’s no way to reattach to a session. Sandbox.connect(id) gets you the sandbox back, so you can start a new session. For a job that has to outlive the connection, start it in the background with nohup and its output in a file. See If the connection drops. A paused sandbox wakes on its own before the session opens.