Skip to main content
The LangSmith SDK provides a programmatic interface to create and interact with sandboxes.

Install

The [sandbox] extra for Python installs websockets, which enables real-time streaming and timeout=0. Without it, run() falls back to HTTP automatically. For TypeScript, install the optional ws package for WebSocket streaming:

Create and run a sandbox

Pass a snapshot ID or name when you want to boot from a reusable custom filesystem image; see Snapshots for that flow.

Run commands

Every run() call returns an ExecutionResult with stdout, stderr, exit_code, and success.

Stream output

For long-running commands, stream output in real time using callbacks or a CommandHandle.

Stream with callbacks

Stream with CommandHandle

Set wait=False to get a CommandHandle for full control over the output stream.

Send stdin and kill commands

Kill a running command:

Reconnect to a running command

If a client disconnects, reconnect using the command ID:

File operations

Read and write files in the sandbox:

Sandbox lifetime and retention

Sandboxes are governed by a two-stage retention model anchored to idle activity and the stopped state. Both values must be multiples of 60 (minute resolution). The full lifecycle is:
You can also call stop_sandbox / stopSandbox explicitly — that also populates stopped_at and starts the deletion timer.

Command lifecycle and TTL

The sandbox daemon manages command session lifecycles with two timeout mechanisms:
  • Session TTL (finished commands): After a command finishes, its session remains in memory for a TTL period. During this window you can reconnect to retrieve output. After the TTL expires, the session is cleaned up.
  • Idle timeout (running commands): Running commands with no connected clients are killed after an idle timeout (default: 5 minutes). The idle timer resets each time a client connects. Set to -1 for no idle timeout.

Combine lifecycle options

Set kill_on_disconnect=True (Python) or killOnDisconnect: true (TypeScript) to kill the command immediately when the last client disconnects, instead of waiting for the idle timeout.

Service URLs (Python)

Access an HTTP service running inside a sandbox via an authenticated URL. You can open it in a browser, call it from code, or share it with a teammate.
For more details, including use cases, REST API access, and a full FastAPI example, see Service URLs.

TCP tunnels (Python)

Access any TCP service running inside a sandbox as if it were local. The tunnel opens a local TCP port and forwards connections through a WebSocket to the target port inside the sandbox.
Tunnels work with any TCP service (Redis, HTTP servers, etc.) and you can open multiple tunnels simultaneously:

Async support (Python)

The Python SDK provides a full async client:

Trace sandbox activity

Pass LangSmith tracing environment variables through the env parameter on run() to send traces from code running inside a sandbox. Call flush() before the process exits to ensure all traces are delivered.
Inside the sandbox, any LangSmith-instrumented code (@traceable, LangChain, LangGraph) automatically picks up the tracing configuration from the injected environment variables.
Always call flush() before the sandbox process exits — langsmith.Client().flush() in Python or await new Client().flush() in TypeScript. Without it, traces may be lost because the container is destroyed when the command finishes.

Error handling

Both SDKs provide typed exceptions for specific error handling:
For more details, see the sandbox SDK reference on GitHub for Python or TypeScript.