Skip to main content

Agent frameworks & SDKs

LangGraph

Give your LangGraph graph a code-execution tool backed by E2B. Model-written Python runs in an isolated sandbox that survives graph steps, checkpoints and interrupts.

Maintained by
E2B
How it works
Sandbox as a graph tool
Package
e2b-code-interpreter
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 LangGraph project.

  1. 1.Install e2b-code-interpreter using the project's package manager.
  2. 2.The SDK 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 LangGraph guide at https://docs.e2b.dev/agents/langgraph. Add a run_python tool that reads the Sandbox from runtime context, keep the sandbox_id in graph state, reconnect with Sandbox.connect() on resume, and kill the sandbox in a finally block.
  4. 4.Run the graph once and show me the output that came from the sandbox.

Or set it up yourself

  1. 1. Install the dependencies

    pip install langgraph langchain langchain-openai e2b-code-interpreter
  2. 2. Set your API keys

    export E2B_API_KEY="e2b_***"
    export OPENAI_API_KEY="sk-***"
  3. 3. Add a code-execution tool

    from dataclasses import dataclass
    
    from e2b_code_interpreter import Sandbox
    from langchain.agents import create_agent
    from langchain.tools import ToolRuntime, tool
    from langchain_openai import ChatOpenAI
    
    
    @dataclass
    class Context:
        sandbox: Sandbox
    
    
    @tool
    def run_python(code: str, runtime: ToolRuntime[Context]) -> str:
        """Run Python in a Jupyter kernel. Returns stdout, the last result, or the error."""
        execution = runtime.context.sandbox.run_code(code)
        if execution.error:
            return f"{execution.error.name}: {execution.error.value}"
        return "".join(execution.logs.stdout) + (execution.text or "")
    
    
    agent = create_agent(
        ChatOpenAI(model="gpt-5.6-luna", use_responses_api=True, reasoning={"effort": "high"}),
        tools=[run_python],
        system_prompt="Answer by running Python. Check the tool result for an error before answering.",
        context_schema=Context,
    )
  4. 4. Run with one sandbox per run

    sandbox = Sandbox.create(timeout=600)
    try:
        result = agent.invoke(
            {"messages": [("human", "How many r's are in 'strawberry'? Use code.")]},
            context=Context(sandbox=sandbox),
        )
        print(result["messages"][-1].text)
    finally:
        sandbox.kill()

Security

What runs in the sandbox

Code the model writes runs in an isolated sandbox, away from the machine running your graph.

  • CodePython the model writes, run in a Jupyter kernel with run_code. Output and errors flow back into graph state.
  • StateOne sandbox per run: files and variables carry over between nodes. Keep its sandbox_id in state and Sandbox.connect() after an interrupt.
  • LifetimeThe default timeout is 300 seconds. on_timeout pause with auto_resume keeps the sandbox through long human reviews.
  • SecretsHost API keys stay with your graph unless the task needs them. Kill the sandbox in a finally block.

3B+ sandboxes started

Give every agent a machine