# LangChain Deep Agents (/docs/agents/deep-agents)

<!-- agent-signals: reading_time_min: 4 · est_tokens: 2001 · updated: 2026-07-30 -->
Related: [Amp](/docs/agents/amp.md), [Claude Code](/docs/agents/claude-code.md), [Claude Managed Agents](/docs/agents/claude-managed-agents.md), [Codex](/docs/agents/codex.md), [Crabbox with E2B](/docs/agents/crabbox.md), [Devin](/docs/agents/devin.md)

[Deep Agents](https://docs.langchain.com/oss/python/deepagents/overview) is LangChain's agent harness for building LLM-powered agents, built on the [LangGraph](https://langchain-ai.github.io/langgraph/) runtime. In Deep Agents, a [sandbox backend](https://docs.langchain.com/oss/python/deepagents/sandboxes) defines the environment where the agent operates — E2B is a supported backend via the [`langchain-e2b`](https://pypi.org/project/langchain-e2b/) package.

With the E2B backend, the agent's built-in tools all execute inside an isolated sandbox instead of your machine:

* **Filesystem tools** — `ls`, `read_file`, `write_file`, `edit_file`, `glob`, and `grep` operate on the sandbox filesystem.
* **Shell tool** — `execute` runs arbitrary shell commands in the sandbox.

## Install the dependencies [#install-the-dependencies]

Set API keys for E2B and your model provider:

```bash
export E2B_API_KEY="e2b_***"        # get yours at https://e2b.dev/dashboard
export ANTHROPIC_API_KEY="sk-ant-***"
```

Then install the packages:

```bash
pip install deepagents langchain-e2b e2b
```

## Basic example [#basic-example]

Create an E2B sandbox, wrap it in the `E2BSandbox` backend, and pass it to `create_deep_agent`. You manage the sandbox lifecycle yourself — create it before the agent runs and kill it when you're done.

```python
from deepagents import create_deep_agent
from e2b import Sandbox
from langchain_e2b import E2BSandbox

sandbox = Sandbox.create(timeout=600)

agent = create_deep_agent(
    model="anthropic:claude-sonnet-5",
    system_prompt="You are a coding agent working in an isolated Linux sandbox.",
    backend=E2BSandbox(sandbox=sandbox),
)

result = agent.invoke({
    "messages": [{
        "role": "user",
        "content": "Create hello.py that prints 'Hello from E2B', run it, and show the output.",
    }]
})

print(result["messages"][-1].content)
sandbox.kill()
```

The agent's file operations and shell commands all run inside the sandbox — nothing touches your machine. Because the backend wraps the native E2B SDK sandbox, you can configure everything the SDK supports: [custom templates](/docs/template/quickstart), timeouts, [persistence](/docs/sandbox/persistence), or reconnecting to an existing sandbox by ID.

## Deep Agents Code (dcode) [#deep-agents-code-dcode]

[Deep Agents Code](https://docs.langchain.com/oss/python/deepagents/sandboxes#deep-agents-code) is LangChain's terminal coding agent built on Deep Agents. `langchain-e2b` ships a sandbox provider for it, so every `dcode` session can run in an E2B sandbox:

```bash
dcode --install langchain-e2b --package
export E2B_API_KEY="e2b_***"
dcode --sandbox e2b
```

Requires `dcode` >= 0.1.19 and `langchain-e2b` >= 0.0.4. Unlike the library path, `dcode` manages the sandbox lifecycle for you — it creates the sandbox on start and deletes it on exit. Configure it with:

* `E2B_TEMPLATE` — custom sandbox template to start from
* `E2B_SANDBOX_TIMEOUT` — sandbox timeout in seconds

## Custom templates [#custom-templates]

By default, sandboxes start from the E2B base template. To pre-install languages, frameworks, or tools your agent needs — and cut setup time per run — build a [custom template](/docs/template/quickstart) and pass it when creating the sandbox:

```python
sandbox = Sandbox.create(template="your-template-id-or-name", timeout=600)
```

## Deep Agents vs. Open SWE [#deep-agents-vs-open-swe]

[Open SWE](/docs/agents/open-swe) is LangChain's coding agent framework built on top of Deep Agents — both use the same `langchain-e2b` backend under the hood, but they sit at different layers:

|                   | **Deep Agents** (this page)                                              | **[LangChain Open SWE](/docs/agents/open-swe)**                      |
| ----------------- | ------------------------------------------------------------------------ | -------------------------------------------------------------------- |
| What it is        | A Python library you embed in your own agent                             | A deployable internal coding agent you fork and run                  |
| How you use E2B   | Create the sandbox yourself and pass `E2BSandbox` to `create_deep_agent` | Set `SANDBOX_TYPE="e2b"` — sandboxes are created and managed for you |
| Sandbox lifecycle | Yours to manage (`Sandbox.create()` / `sandbox.kill()`)                  | Per-thread persistent sandbox, reconnect by ID, auto-recreate        |
| Use it when       | Building any custom agent that needs isolated code execution             | You want a GitHub/Slack/Linear-driven coding agent for your org      |

Use this page's approach for custom agents; if you're deploying a coding agent for your team, start from [Open SWE](/docs/agents/open-swe) instead.

## Learn more [#learn-more]

* [Deep Agents sandboxes](https://docs.langchain.com/oss/python/deepagents/sandboxes) — sandbox backends overview
* [E2B integration reference](https://docs.langchain.com/oss/python/integrations/sandboxes/e2b) — LangChain's E2B provider page
* [`langchain-e2b` on PyPI](https://pypi.org/project/langchain-e2b/)

## Related guides [#related-guides]

<CardGroup cols="3">
  <Card title="Templates" icon="<svg xmlns=&#x22;http://www.w3.org/2000/svg&#x22; viewBox=&#x22;0 0 24 24&#x22; fill=&#x22;none&#x22;><path d=&#x22;M15 18H13C12.0681 18 11.6022 18 11.2346 17.8478C10.7446 17.6448 10.3552 17.2554 10.1522 16.7654C10 16.3978 10 15.9319 10 15C10 14.0681 10 13.6022 10.1522 13.2346C10.3552 12.7446 10.7446 12.3552 11.2346 12.1522C11.6022 12 12.0681 12 13 12H15C15.9319 12 16.3978 12 16.7654 12.1522C17.2554 12.3552 17.6448 12.7446 17.8478 13.2346C18 13.6022 18 14.0681 18 15C18 15.9319 18 16.3978 17.8478 16.7654C17.6448 17.2554 17.2554 17.6448 16.7654 17.8478C16.3978 18 15.9319 18 15 18Z&#x22; stroke=&#x22;currentColor&#x22; stroke-linecap=&#x22;round&#x22; stroke-linejoin=&#x22;round&#x22; stroke-width=&#x22;1.5&#x22;/><path d=&#x22;M10 13C9.06812 13 8.60218 13 8.23463 12.8478C7.74458 12.6448 7.35523 12.2554 7.15224 11.7654C7 11.3978 7 10.9319 7 10C7 9.06812 7 8.60218 7.15224 8.23463C7.35523 7.74458 7.74458 7.35523 8.23463 7.15224C8.60218 7 9.06812 7 10 7H12C12.9319 7 13.3978 7 13.7654 7.15224C14.2554 7.35523 14.6448 7.74458 14.8478 8.23463C15 8.60218 15 9.06812 15 10C15 10.9319 15 11.3978 14.8478 11.7654&#x22; stroke=&#x22;currentColor&#x22; stroke-linecap=&#x22;round&#x22; stroke-linejoin=&#x22;round&#x22; stroke-width=&#x22;1.5&#x22;/><path d=&#x22;M16.5 21.5C17.4293 21.5 17.894 21.5 18.2804 21.4231C19.8671 21.1075 21.1075 19.8671 21.4231 18.2804C21.5 17.894 21.5 17.4293 21.5 16.5M7.5 2.5C6.57069 2.5 6.10603 2.5 5.71964 2.57686C4.13288 2.89249 2.89249 4.13288 2.57686 5.71964C2.5 6.10603 2.5 6.57069 2.5 7.5M7.5 21.5C6.57069 21.5 6.10603 21.5 5.71964 21.4231C4.13288 21.1075 2.89249 19.8671 2.57686 18.2804C2.5 17.894 2.5 17.4293 2.5 16.5M16.5 2.5C17.4293 2.5 17.894 2.5 18.2804 2.57686C19.8671 2.89249 21.1075 4.13288 21.4231 5.71964C21.5 6.10603 21.5 6.57069 21.5 7.5&#x22; stroke=&#x22;currentColor&#x22; stroke-linecap=&#x22;round&#x22; stroke-linejoin=&#x22;round&#x22; stroke-width=&#x22;1.5&#x22;/></svg>" href="/docs/template/quickstart">
    Build custom sandbox templates with pre-installed dependencies
  </Card>

  <Card title="Sandbox persistence" icon="<svg xmlns=&#x22;http://www.w3.org/2000/svg&#x22; viewBox=&#x22;0 0 24 24&#x22; fill=&#x22;none&#x22;><circle cx=&#x22;12&#x22; cy=&#x22;12&#x22; r=&#x22;10&#x22; stroke=&#x22;currentColor&#x22; stroke-width=&#x22;1.5&#x22;/><path d=&#x22;M12 8V12L14 14&#x22; stroke=&#x22;currentColor&#x22; stroke-linecap=&#x22;round&#x22; stroke-linejoin=&#x22;round&#x22; stroke-width=&#x22;1.5&#x22;/></svg>" href="/docs/sandbox/persistence">
    Auto-pause, resume, and manage sandbox lifecycle
  </Card>

  <Card title="Open SWE" icon="/_assets/7fef88aa789747be0e9634bd24911c10f8c58ae52b599146fb6f85aeeccfd463" href="/docs/agents/open-swe">
    LangChain's coding agent framework with E2B built in
  </Card>
</CardGroup>
