> ## Documentation Index
> Fetch the complete documentation index at: https://langchain-5e9cc07a-preview-docsdy-1782337909-2539eb6.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# E2BSandbox integration

> Integrate with the E2BSandbox sandbox using LangChain Python.

[E2B](https://e2b.dev/) provides cloud sandboxes for running AI-generated code in isolated environments. See the [E2B docs](https://e2b.dev/docs) for signup, authentication, and platform details.

## Installation

<CodeGroup>
  ```bash pip theme={null}
  pip install langchain-e2b
  ```

  ```bash uv theme={null}
  uv add langchain-e2b
  ```
</CodeGroup>

## Authentication

Set your E2B API key:

```bash theme={null}
export E2B_API_KEY="..."
```

Configure E2B-specific options, such as templates or sandbox lifetime, through the E2B SDK when creating the sandbox.

## Create a sandbox

In Python, you create the sandbox using the E2B SDK, then wrap it with the [deepagents backend](/oss/python/deepagents/backends).

```python theme={null}
from e2b import Sandbox
from langchain_e2b import E2BSandbox

e2b_sandbox = Sandbox.create()
backend = E2BSandbox(sandbox=e2b_sandbox)

try:
    result = backend.execute("echo hello")
    print(result.output)
finally:
    e2b_sandbox.kill()
```

## Use with Deep Agents

```python theme={null}
from e2b import Sandbox
from deepagents import create_deep_agent
from langchain_anthropic import ChatAnthropic
from langchain_e2b import E2BSandbox

e2b_sandbox = Sandbox.create()
backend = E2BSandbox(sandbox=e2b_sandbox)

agent = create_deep_agent(
    model=ChatAnthropic(model="claude-sonnet-4-6"),
    system_prompt="You are a coding assistant with sandbox access.",
    backend=backend,
)

try:
    result = agent.invoke(
        {
            "messages": [
                {"role": "user", "content": "Create a hello world Python script and run it"}
            ]
        }
    )
finally:
    e2b_sandbox.kill()
```

## Use with Deep Agents Code

`langchain-e2b` also publishes an E2B sandbox provider for [Deep Agents Code](/oss/python/deepagents/code/remote-sandboxes), so `dcode` can run agent tool calls inside an E2B sandbox. Install the package into the `dcode` environment, set your API key, then select the provider:

<Note>
  Using E2B with Deep Agents Code requires `dcode` 0.1.19 or newer and `langchain-e2b` 0.0.4 or newer.
</Note>

```bash theme={null}
dcode --install langchain-e2b --package
export E2B_API_KEY="..."
dcode --sandbox e2b
```

E2B sandboxes use `/home/user` as the default working directory. To create sandboxes from a specific [E2B template](https://e2b.dev/docs/sandbox-template), set `E2B_TEMPLATE`; to change the sandbox lifetime, set `E2B_SANDBOX_TIMEOUT` (in seconds). Each variable also accepts a `DEEPAGENTS_CODE_`-prefixed form (for example, `DEEPAGENTS_CODE_E2B_API_KEY`), which takes precedence.

Deep Agents Code manages the sandbox lifecycle for you, creating a sandbox on start and deleting it on exit.

## Cleanup

Always kill E2B sandboxes when you are done to avoid ongoing resource usage.

See also: [Sandboxes](/oss/python/deepagents/sandboxes).

***

<div className="source-links">
  <Callout icon="terminal-2">
    [Connect these docs](/use-these-docs) to Claude, VSCode, and more via MCP for real-time answers.
  </Callout>

  <Callout icon="edit">
    [Edit this page on GitHub](https://github.com/langchain-ai/docs/edit/main/src/oss/python/integrations/sandboxes/e2b.mdx) or [file an issue](https://github.com/langchain-ai/docs/issues/new/choose).
  </Callout>
</div>
