Skip to main content

Agent frameworks & SDKs

LangChain Deep Agents

Make an E2B sandbox the environment for a LangChain Deep Agent. With the langchain-⁠e2b backend, the agent's file tools and shell commands run in the sandbox instead of on your machine.

Maintained by
E2B
How it works
Sandbox backend for Deep Agents
Package
langchain-e2b
Language
Python

Set it up

Get started

Paste the prompt into your coding agent, or follow the steps yourself.

Paste this into your coding agent

Add E2B sandboxes to this LangChain Deep Agents project.

  1. 1.Install deepagents, langchain-e2b and e2b using the project's package manager.
  2. 2.The sandbox needs the E2B_API_KEY environment variable. If it isn't set, ask me for my API key from https://console.e2b.dev/?tab=keys. Never hardcode it.
  3. 3.Follow the Deep Agents guide at https://docs.e2b.dev/agents/deep-agents. Create a sandbox with Sandbox.create(), pass E2BSandbox(sandbox=sandbox) as the backend to create_deep_agent, and kill the sandbox when the agent is done.
  4. 4.Run one task and show me the output that came from the sandbox.

Or set it up yourself

  1. 1. Install the packages

    pip install deepagents langchain-e2b e2b
  2. 2. Set your API keys

    export E2B_API_KEY="e2b_***"
    export ANTHROPIC_API_KEY="sk-ant-***"
  3. 3. Create the agent on a sandbox

    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),
    )
  4. 4. Run a task

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

Security

What runs in the sandbox

The agent's built-in tools all run in an isolated sandbox, away from your machine.

  • Filesls, read_file, write_file, edit_file, glob and grep work on the sandbox filesystem.
  • ShellThe execute tool runs shell commands inside the sandbox.
  • LifetimeYou create the sandbox and kill it when done. In dcode, the sandbox is created on start and deleted on exit.
  • TemplatePass a custom template to Sandbox.create() to preinstall the languages and tools your agent needs.

3B+ sandboxes started

Give every agent a machine