BaseSandbox implementations for command execution
and file transfer. Use ContreeSandbox with a sync executor, or
ContreeAsyncSandbox with an async executor. A session implements the matching contract.
Async snippets with top-level
await run inside an async function or a notebook
that supports it. For a standalone script, use the asyncio.run(main()) structure
from Your first sandbox.create_sandbox_session. Commands and
uploads use the same VM. Downloads automatically save its current filesystem
before reading; this stops the VM. The next command starts from the saved image.
The factory handles connection setup and cleanup.
Install and test the backend
This integration requires Python 3.11 or newer. Install the development API from the repository root, as in Your first sandbox:aexecute and aupload_files. Version 0.6.8 instead calls sync
methods from those tools and is incompatible with ContreeAsyncSandbox.
The sandbox image needs a POSIX shell, Python 3, and the file utilities used by
deepagents, including grep for search. Replace the example
tag:python:3.12-alpine with an image available on your endpoint.
Configure a connection as described in Connection sources.
This example exercises the backend directly, so no model credentials are needed.
- Sync
- Async
disposable=False. Its next tool call sees the
filesystem changes. It does not close the session, client, or store. Use SQLite
when the agent needs to resume after a Python restart.
Connect the backend to an agent
Pass the sandbox asbackend= to deepagents.create_deep_agent. Supply the model
configured by your application. Keep the client open for the entire agent invocation.
The following function accepts that model and uses the connection configured in
Connection sources:
- Sync
- Async
Advanced: file transfers and executor contracts
Uploads callsession.write_file(path, content). Its default implementation writes
binary stdin with cat, creates parent directories, and requests no snapshot.
Custom executors can override write_file without changing this adapter.
Downloads call session.read_file(). Lazy sessions read the active VM through
live inspect, so downloads preserve running processes and do not force a snapshot.
File paths must be absolute. Transfers return per-file responses; inspect
error
before reading their content. The async adapter’s synchronous methods raise
NotImplementedError. Each adapter serializes its own calls, but callers sharing
an executor must coordinate other mutations themselves.
To use another backend implementation, follow Customize the SDK.
See deepagents sandboxes for API signatures.