Skip to main content
E2B provides cloud sandboxes for running AI-generated code in isolated environments. See the E2B docs for signup, authentication, and platform details.

Installation

pip install langchain-e2b

Authentication

Set your E2B API key:
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.
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

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()

Cleanup

Always kill E2B sandboxes when you are done to avoid ongoing resource usage. See also: Sandboxes.