Running Commands
Execute commands and code in a sandbox, and manage long-running sessions.
Every sandbox exposes a process API through sandbox.process, giving you three ways to run things: one-shot shell commands, direct code execution in a configured language runtime, and long-lived sessions that keep shell state between commands. One-shot execution is the right default for independent commands; sessions are the tool when commands need to build on each other — an install followed by a run, a background server you interact with, or anything that depends on a working directory or environment set up earlier.
One-shot execution
exec (Python; execute_command is an alias) / executeCommand (TypeScript) runs a single shell command and returns its exit code and output. You can set the working directory, extra environment variables, and a timeout per call. Each invocation is independent — no state carries over to the next.
Running code directly
code_run / codeRun executes a source string in the sandbox's configured language runtime, without you writing the file-and-invoke boilerplate. The language comes from the sandbox's code-toolbox-language label, set at create time; calling code_run on a sandbox without that label raises an error.
Sessions: long-lived shells
A session is a persistent shell inside the sandbox. State accumulates across the commands you send to it — the working directory, environment variables, and background processes you start all persist until the session is deleted. That makes sessions the natural fit for multi-step workflows where one-shot exec calls would each start from scratch.
You create a session under an id you choose, then execute commands in it:
Async commands and logs
Passing run_async: true / runAsync: true returns immediately with a command id instead of waiting for completion — the way to launch servers or long jobs. You can then poll the command's status, fetch its accumulated logs, or send it input:
Managing sessions
list_sessions() / listSessions() returns all active sessions, get_session(id) / getSession(id) returns one with its command history, and delete_session(id) / deleteSession(id) tears one down along with anything still running in it.
Note: a common pattern is one session per logical task —
installdeps in it, thenrunin the same session so the environment carries over — and a separate session per background server so its lifetime is explicit.
See also
- Working with Files — get code and data in and out of the sandbox.
- SSH Access — an interactive shell when you want a terminal instead of an API.