langchain-backend-microsandbox
A Microsandbox backend for LangChain Deep Agents, bringing isolated microVM execution to autonomous AI agents.
Run agent-generated commands and manage files inside lightweight, isolated virtual machines through Deep Agents' native BaseSandbox interface.
Built on Microsandbox, this integration provides a seamless bridge between Deep Agents and Microsandbox's asynchronous execution and filesystem APIs.
Inspired by kenwoodjw/langchain-microsandbox, this implementation extends the original approach with a focus on compatibility, predictable behavior, and closer adherence to the BaseSandbox contract.
Why Microsandbox?
AI agents increasingly need to execute code, manipulate files, and interact with shell environments to accomplish complex tasks.
Running these operations directly on the host introduces unnecessary security risks.
Microsandbox addresses this challenge by providing isolated microVM environments in which agent workloads can execute independently of the host system.
langchain-backend-microsandbox brings this isolation model to LangChain Deep Agents, allowing developers to use Microsandbox as their agent execution backend without implementing their own sandbox integration.
The result: a standardized Deep Agents backend with isolated execution and native filesystem support.
Features
| Feature | Description |
|---|---|
| Deep Agents integration | Implements the BaseSandbox interface. |
| Contract compliance | Designed to closely follow the BaseSandbox behavior and expectations. |
| Isolated execution | Runs agent-generated commands inside Microsandbox microVMs. |
| Async & sync execution | Supports both synchronous and asynchronous command execution. |
| Native file transfers | Binary-safe file uploads and downloads through Microsandbox's filesystem API. |
| Filesystem operations | Inherits read, write, edit, delete, ls, grep, and glob from BaseSandbox. |
| Timeout management | Configurable execution timeouts with standardized timeout exit codes. |
| Error normalization | Maps filesystem exceptions to Deep Agents-compatible error codes. |
| Atomic file uploads | Uses temporary files and filesystem rename operations to avoid exposing partially written files. |
Architecture & Compatibility
MicrosandboxSandbox acts as an adapter between LangChain Deep Agents and an existing Microsandbox instance.
It implements the backend-specific operations while relying on BaseSandbox for the higher-level filesystem functionality.
Inherited BaseSandbox operations
The following operations are inherited directly from LangChain Deep Agents:
read(),write(),edit(),delete()ls(),grep(),glob()
This approach avoids reimplementing filesystem behavior and helps maintain consistency with other Deep Agents sandbox backends.
BaseSandbox behavior
Grep directory exclusions
Directories listed in grep_exclude_dirs are excluded from recursive searches, regardless of their depth in the directory tree.
However, explicitly specifying an excluded directory as the search root overrides this exclusion.
In other words:
- Searching from a parent directory skips excluded directories.
- Explicitly targeting an excluded directory still searches its contents.
This allows common directories such as .git to be excluded from general searches without preventing developers or agents from inspecting them when needed.
Reference: BaseSandbox.grep_exclude_dirs
Command execution timeouts
Timeout behavior follows the SandboxBackendProtocol conventions:
Noneuses the default timeout configured when creating the adapter.0disables the execution timeout.- Negative values are rejected with a
ValueError. - When a command exceeds its configured timeout, the backend returns exit code
124.
Reference: SandboxBackendProtocol.execute
Python runtime requirement
The Microsandbox VM must have Python 3 installed and available in PATH.
This is a requirement of the inherited BaseSandbox filesystem operations, which rely on Python scripts executed inside the sandbox.
Having Python installed on the host machine is not sufficient: the interpreter must be available inside the Microsandbox VM.
Make sure your sandbox image includes a compatible Python installation before initializing the backend.
Requirements
- Python >= 3.12
- Deep Agents >= 0.7.35, < 0.8.0
- Microsandbox >= 0.7.14, < 0.8.0
- A supported Microsandbox runtime environment.
Microsandbox requires hardware virtualization support:
- Linux: KVM
- macOS: Apple Silicon virtualization support
- Windows: Windows Hypervisor Platform (WHP)
On debian/ubuntu system, you can install KVM by installing:
sudo apt install qemu-system libvirt-daemon-system
Installation from sources
Clone the repository and install the package:
git clone https://github.com/dod-ia/langchain-backend-microsandbox.git
cd langchain-backend-microsandbox
uv sync
Get started
The adapter wraps an existing Microsandbox instance and exposes it as a Deep Agents backend.
Since Microsandbox provides an asynchronous API, sandbox creation and adapter initialization must be performed asynchronously.
Basic example
from microsandbox import Sandbox
from langchain_backend_microsandbox import MicrosandboxSandbox
from deepagents import create_deep_agent
model = init_chat_model("YOUR_MODEL")
async def main():
# Create a sandbox from MicroSandbox API
sandbox = await Sandbox.create(
"deep-agent",
image="python:3.13",
memory=1024,
cpus=2,
ephemeral=True,
)
# Create a DeepAgent MicroSandbox Backend
backend = await MicrosandboxSandbox.create(
sandbox,
timeout=1800,
)
try:
agent = create_deep_agent(model=model, backend=backend)
result = await agent.ainvoke(
{
"messages": [
{
"role": "user",
"content": "Write 'Hi !' in the /tmp/test.txt",
}
]
},
)
finally:
content_file = await backend.adownload_files(["/tmp/test.txt"])
print(content_file)
# [FileDownloadResponse(path='/tmp/test', content=b'Hi !', error=None)]
# Sandbox lifecycle management remains the caller's responsibility.
await sandbox.stop()
Sandbox lifecyle
The adapter does not create, start, stop, or destroy virtual machines.
The calling application remains responsible for:
- Creating and configuring the Microsandbox instance.
- Managing its lifecycle.
- Defining its persistence policy.
- Stopping and cleaning up the sandbox when execution is complete.
Setting ephemeral=True when creating the sandbox allows Microsandbox to apply its ephemeral VM lifecycle policy (destroyed after stopping).
The adapter wraps an existing Microsandbox instance. Sandbox creation, lifecycle management and persistence remain the responsibility of the calling application.
Configuration
MicrosandboxSandbox.create() accepts the following parameters:
| Parameter | Type | Default | Description |
|---|---|---|---|
sandbox |
Sandbox |
Required | Existing Microsandbox instance. |
timeout |
int |
1800 |
Default command execution timeout in seconds. Use 0 to disable it. Must be positive. |
enable_capture_offload |
bool |
False |
Enables capture-at-source offloading for large command outputs. Enabling it requires a compatible shell environment and the utilities expected by Deep Agents' capture wrapper. |
grep_exclude_dirs |
tuple[str, ...] |
(".git",) |
Directory names excluded from recursive grep searches. |
Example
backend = await MicrosandboxSandbox.create(
sandbox,
timeout=600,
enable_capture_offload=False,
grep_exclude_dirs=(".git", "__pycache__", ".venv"),
)
Testing
Tests use a real Microsandbox VM.
A working Microsandbox runtime and hardware virtualization support are required.
Install development dependencies:
uv sync --group dev
Run the test suite:
uv run pytest
Run with verbose output and coverage
uv run pytest -v -s --tb=short --cov --cov-report=term-missing
License
This project is distributed under the MIT License.
Acknowledgements
This project builds upon the BaseSandbox interface provided by
LangChain Deep Agents.
It is inspired by kenwoodjw/langchain-microsandbox, whose work helped establish the initial integration approach.
Deep Agents is distributed under the MIT License.
langchain-backend-microsandbox is an independent community integration and is not affiliated with, endorsed by, or officially supported by LangChain.
Metadata
Release files for langchain-backend-microsandbox 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| langchain_backend_microsandbox-0.1.0.tar.gz | 10.0 kB | With provenance | Details for langchain_backend_microsandbox-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| langchain_backend_microsandbox-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for langchain_backend_microsandbox-0.1.0-py3-none-any.whl |
Total release size: 21.5 kB
Release files / langchain_backend_microsandbox-0.1.0.tar.gz
| Download URL | langchain_backend_microsandbox-0.1.0.tar.gz |
|---|---|
| Size | 10.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4577c7c64ba7e431fd4d1cc0b38dd05f0ffe957a87ec2c3198457d9676ff252a
|
|
BLAKE2b-256 checksum How to use checksums |
c2425428d954c971e8258fb808ecf6a177ac07e0f4286ddeb2b4049a84a0c5f9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 13, 2026.
Transparency logRelease files / langchain_backend_microsandbox-0.1.0-py3-none-any.whl
| Download URL | langchain_backend_microsandbox-0.1.0-py3-none-any.whl |
|---|---|
| Size | 11.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
1b4f7bcac8800528991c27856b395144c87c108e0730c8f288159802f0cddcec
|
|
BLAKE2b-256 checksum How to use checksums |
e5bc999ae9d56e74b6dd1682a8254737d6ac93e0d2105a5b3c4a48d9d68f6be0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 13, 2026.
Transparency logSecurity for langchain-backend-microsandbox 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for langchain_backend_microsandbox-0.1.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 13, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.