Sandbox.shell() for interactive TTY sessions. It returns a TerminalSession with raw byte streaming and no output buffering, designed for interactive use. For batch command execution, use exec() instead.
Choose authentication
These examples run on serverless capacity unless a section specifies CKS. Choose the credential for the account that owns your sandboxes. See Choose and get credentials to obtain a key or token. Run the installation commands in an active Python virtual environment. Save the Python code from your credential tab assandbox_auth.py beside the Python scripts you run from this page. It stores the authentication choice, not your credential. The examples import AUTH from that file and pass it explicitly to each new sandbox or session.
- Forge API key
- CoreWeave API access token
Use your Forge API key through the SDK’s W&B authentication strategy:
sandbox_auth.py
placement_mode="cks" on sandbox creation or in SandboxDefaults. Creating a session with auth=AUTH also authenticates the sandboxes it owns. Keep the same credential when reconnecting, listing, or cleaning up sandboxes.
Shell session
Create a sandbox and open an interactive shell session.Sandbox.run() with no command uses the default long-running command. This example sends commands, reads their output, and waits for the shell to exit before leaving the with block. Leaving the block stops the sandbox.
shell() always allocates a TTY and enables stdin. The returned TerminalSession streams raw bytes with no UTF-8 decode or encode round-trip, making it safe for terminal escape sequences and binary output.
Send commands and read output
While the shell session is active, write commands toterminal.stdin and stream the merged byte output from terminal.output, as shown in the preceding example. Keep these operations inside the with block.
TerminalSession doesn’t buffer output. Stdout and stderr are merged into a single raw byte stream on terminal.output. There are no separate stdout or stderr attributes on the result. Consume terminal.output continuously. If you stop reading, backpressure from the bounded queue stalls the remote session.
Terminal resize
Send resize messages when the terminal dimensions change. Callresize() inside the with block while the shell session is active:
resize() is fire-and-forget on TerminalSession.
Exit a shell session
Inside thewith block, end a shell session by closing stdin or sending an exit command:
terminal.wait() or terminal.result() to block for the exit code.
Attach your local terminal
To work in a running sandbox from your own terminal, put the local terminal in raw mode and forward its input to ashell() session. The following script attaches to an existing sandbox by ID, forwards your keystrokes to a Bash shell in the sandbox, and restores your local terminal settings when the shell exits. It uses termios, so it runs on macOS or Linux.
Save the script as attach_sandbox.py beside sandbox_auth.py:
attach_sandbox.py
[SANDBOX-ID] with the ID of a running sandbox, then run the script from an interactive terminal:
Commands and use cases
To run commands with the command-line interface (CLI), install the CLI and configure your credentials. The CLI uses a CoreWeave API access token inCWSANDBOX_API_KEY by default. For a Forge API key, install the cli and wandb extras, set WANDB_API_KEY, and pass --auth wandb before the command. Use the credential that owns the sandbox.
For terminal access through the SDK with either credential, use the sandbox.shell() attachment script on this page.
See also
- Command execution - Run commands with
exec(). - Sandbox logging - Stream container logs.