stream_logs(), which captures output from the command passed to Sandbox.run(). Output from exec() commands isn’t included. Use Process.stdout or Process.stderr for that.
stream_logs() returns a StreamReader that yields log lines. Iterate synchronously or asynchronously.
When you call
Sandbox.run() without a command, the sandbox’s default command doesn’t write to stdout or stderr, so stream_logs() returns no output. Pass a command that writes to stdout or stderr.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.
Retrieve recent logs
Usetail_lines to fetch the most recent log lines without streaming the entire history. This is useful for quick inspection of a running sandbox.
Follow mode
Stream logs continuously, liketail -f. The iterator blocks until new data arrives.
Ctrl+C to interrupt follow mode. Its effect on sandboxes depends on what the Python process
owns:
- In an observer process that only reconnects with
Sandbox.from_id()and streams logs,Ctrl+Cinterrupts the log iterator without sending a stop request to the sandbox. - In a process that owns session-managed sandboxes, the SDK’s installed
SIGINThandler also attempts to stop all sandboxes in registered sessions. Interrupting logs can therefore shut down other sandboxes that process owns.
cwsandbox>=1.15.0 activate on first sandbox ownership
and install only from Python’s main thread. The global handlers don’t stop standalone sandboxes,
though a sandbox context manager stops its sandbox when the block exits. If your
application disables or replaces signal handlers, its own shutdown behavior applies. See
Global cleanup handlers.
Filter by time
Passsince_time to retrieve only logs after a specific timestamp. This narrows output to a window of interest, such as logs since a deployment or incident.
Timestamps
Settimestamps=True to prefix each line with an ISO 8601 timestamp from the server. Server-side timestamps help correlate sandbox events with other systems.
Async iteration
If your application usesasyncio, iterate over the same StreamReader with async for to avoid blocking the event loop.
Retrieve logs before stopping
Log retrieval requires a running sandbox. Retrieve the logs you need before stopping it. Both the Python SDK and the CLI reject log retrieval after the sandbox stops.See also
- Command execution: Running commands and streaming their output with
exec() - Sync vs async: Iteration patterns