> ## Documentation Index
> Fetch the complete documentation index at: https://docs.boxd.sh/llms.txt
> Use this file to discover all available pages before exploring further.

# LangChain

> Run a LangChain Deep Agent's commands and file edits on a boxd machine, then come back to it or fork it.

![The LangChain logo inside the boxd card.](https://mintcdn.com/azin/hzxvJlAVnEvK1ZI1/images/boxd-x-langchain.png?fit=max\&auto=format\&n=hzxvJlAVnEvK1ZI1\&q=85\&s=a06e9b4dd5212afe74d6f8fa6da34fd8)

[LangChain](https://www.langchain.com) builds open-source frameworks for AI agents. [Deep Agents](https://docs.langchain.com/oss/python/deepagents/overview) is its agent harness: an agent that plans, runs shell commands, and edits files in a sandbox you choose.

`BoxdSandbox` makes that sandbox a boxd machine. It comes from [`boxd-langchain`](https://github.com/boxd-sh/boxd-langchain), a package the boxd team maintains, and LangChain lists it with its [sandbox integrations](https://docs.langchain.com/oss/python/integrations/sandboxes) ([pull request](https://github.com/langchain-ai/docs/pull/7622)). Deep Agents compiles to a LangGraph graph, so the same backend works from LangChain, LangGraph, and Deep Agents.

## Set up

**1. Install the package**

```bash theme={"theme":"github-dark"}
pip install boxd-langchain langchain-anthropic
```

`langchain-anthropic` is for the example below; use your model provider's package instead.

**2. Create an API key**

```bash theme={"theme":"github-dark"}
boxd auth keys create langchain --org <your-org>
export BOXD_API_KEY=bxd_...
```

Machines are created and billed in the key's organization.

**3. Give the agent a machine**

```python theme={"theme":"github-dark"}
from deepagents import create_deep_agent
from langchain_anthropic import ChatAnthropic

from boxd_langchain import BoxdSandbox

with BoxdSandbox.create(isolated=True, delete_after=3600) as backend:
    agent = create_deep_agent(
        model=ChatAnthropic(model="claude-sonnet-5-5"),
        system_prompt="You are a coding assistant with a Linux machine. Work in /home/boxd.",
        backend=backend,
    )
    result = agent.invoke(
        {"messages": [{"role": "user", "content": "Write hello.py that prints hello, run it and show me the output."}]}
    )
    print(result["messages"][-1].text)
```

Set `ANTHROPIC_API_KEY` for the model. The agent's shell and file tools now run on the machine, and leaving the `with` block deletes it.

## Come back to a machine

To keep the agent's work, create the machine yourself and hand its id to `BoxdSandbox`. A backend made from an existing machine never deletes it.

```python theme={"theme":"github-dark"}
from boxd import Boxd

from boxd_langchain import BoxdSandbox

client = Boxd()  # BOXD_API_KEY from the environment
machine = client.machines.create(isolated=True, labels={"app": "agent"})
client.machines.wait_until_ready(machine.id)

backend = BoxdSandbox(client=client, machine_id=machine.id)
backend.write("/home/boxd/notes.md", "# notes\n")

client.machines.hibernate(machine.id)
```

A hibernated machine keeps its disk and memory, and you stop paying for CPU and memory. Store the machine's id. The next command wakes the machine, with its files as the agent left them:

```python theme={"theme":"github-dark"}
backend = BoxdSandbox(client=client, machine_id=machine.id)
print(backend.execute("cat notes.md").output)

client.machines.delete(machine.id)
```

## Fork a machine

A [fork](https://docs.boxd.sh/guides/fork.md) copies a machine into a new one, with its files and running processes as they are at that moment. Set a project up once, fork it, and give each agent its own copy:

```python theme={"theme":"github-dark"}
from boxd_langchain import BoxdSandbox

with BoxdSandbox.create(isolated=True, delete_after=3600) as base:
    base.execute("git clone --depth 1 https://github.com/langchain-ai/deepagents")

    client = base.client
    fork = client.machines.fork(base.id, delete_after=3600)
    client.machines.wait_until_ready(fork.id)

    copy = BoxdSandbox(client=client, machine_id=fork.id)
    print(copy.execute("ls deepagents").output)

    client.machines.delete(fork.id)
```

The two machines change independently after the fork. `backend.client` is the plain [boxd Python SDK](https://docs.boxd.sh/reference/python-sdk.md) client, so snapshots, egress allowlists, and everything else boxd offers are one call away. The package's [`parallel_forks.py`](https://github.com/boxd-sh/boxd-langchain/blob/main/examples/parallel_forks.py) example runs two agents on two forks at once.

## Deep Agents Code

The package also registers boxd as a sandbox for [Deep Agents Code](https://docs.langchain.com/oss/python/deepagents/code/overview), LangChain's terminal coding agent:

```bash theme={"theme":"github-dark"}
dcode install boxd-langchain --package
export BOXD_API_KEY=bxd_...
export BOXD_DELETE_AFTER=3600
dcode --sandbox boxd
```

Each session gets its own machine. `dcode` deletes it on a clean exit, and `BOXD_DELETE_AFTER` has boxd delete it after that many seconds if a run ends early. To pick a machine up again, pass its id or name, and `dcode` wakes it if it is asleep:

```bash theme={"theme":"github-dark"}
dcode --sandbox boxd --sandbox-id <machine>
```

## What each machine gets

* **Isolation when you ask for it:** `isolated=True` creates the machine with no boxd credentials and no route to your organization's other machines. Use it whenever a model writes the code that runs.
* **A deadline:** `delete_after` sets a [delete-after](https://docs.boxd.sh/guides/delete-after.md) timer, so boxd deletes the machine even if your process dies before the `with` block ends.
* **The default image:** Ubuntu 24.04 with Python 3, git, Node.js, Docker, and ripgrep. For your own dependencies, start from a [snapshot](https://docs.boxd.sh/guides/snapshots.md) with `BoxdSandbox.create(from_snapshot="my-snapshot")`.
* **Commands as the `boxd` user,** who has passwordless `sudo`, in `/home/boxd`. A command's output is its stdout and stderr, and its exit code comes back unchanged.
* **A timeout:** a command that runs past its timeout, 30 minutes by default, is stopped with its whole process group and returns exit code 124.

## Read more

* [`boxd-langchain` on GitHub](https://github.com/boxd-sh/boxd-langchain): options, errors, limits, and three examples
* [`boxd-langchain` on PyPI](https://pypi.org/project/boxd-langchain/)
* [Sandbox integrations](https://docs.langchain.com/oss/python/integrations/sandboxes) in LangChain's docs
* [Sandboxes on boxd](https://docs.boxd.sh/use-cases/sandboxes.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.