Skip to main content

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:

  • None uses the default timeout configured when creating the adapter.
  • 0 disables 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.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for langchain-backend-microsandbox 0.1.1
File Size Uploaded Provenance Actions
langchain_backend_microsandbox-0.1.1.tar.gz 10.2 kB With provenance Details for langchain_backend_microsandbox-0.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for langchain-backend-microsandbox 0.1.1
File Interpreter ABI Platform Actions
langchain_backend_microsandbox-0.1.1-py3-none-any.whl Python 3 none any Details for langchain_backend_microsandbox-0.1.1-py3-none-any.whl

Total release size: 21.8 kB

Release files / langchain_backend_microsandbox-0.1.1.tar.gz

Download URL langchain_backend_microsandbox-0.1.1.tar.gz
Size 10.2 kB
Tags Source
SHA-256 checksum
How to use checksums
982a2b2752cecef07b0c39fd9b1039f0a7ea43a3eb77363d6c4b17d2c970bd38
BLAKE2b-256 checksum
How to use checksums
36078017f07e54e57d8713d2773a280c7efc9b7bd9802e0d8462d17f1d8618ed
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 log

Release files / langchain_backend_microsandbox-0.1.1-py3-none-any.whl

Download URL langchain_backend_microsandbox-0.1.1-py3-none-any.whl
Size 11.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
59c54c082793b4c38064d5a9e3cdc5ee25202f52cf773006bedf56d8ee25a231
BLAKE2b-256 checksum
How to use checksums
821988d206a2e05746b4b151b7d72f5c3e821c4a400543e3dab86e4bcebdfc88
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 log

Security for langchain-backend-microsandbox 0.1.1

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.

Select from 2 files

Attestation for langchain_backend_microsandbox-0.1.1.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 log

A 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.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page