---
title: "LangGraph integration"
description: "Run model-written Python from your LangGraph graph in an E2B sandbox, and keep that sandbox alive across graph steps, checkpoints and interrupts."
url: "https://e2b.dev/integrations/langgraph"
---

# 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.

Agent frameworks & SDKs · [All integrations](https://e2b.dev/integrations.md)

## About this integration

- **Maintained by:** E2B
- **How it works:** Sandbox as a graph tool
- **Package:** e2b-code-interpreter
- **Language:** Python
- **Upstream:** [langchain-ai/langgraph · ★ 45k](https://github.com/langchain-ai/langgraph)
- **Example:** [e2b-cookbook/langgraph-plan-and-execute-python](https://github.com/e2b-dev/e2b-cookbook/tree/main/examples/langgraph-plan-and-execute-python)

## Get started

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

### Paste this into your coding agent

```text
Add E2B sandboxes to this LangGraph project.

1. Install e2b-code-interpreter using the project's package manager.
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. 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. Run the graph once and show me the output that came from the sandbox.
```

## Or set it up yourself

### 1. Install the dependencies

```bash
pip install langgraph langchain langchain-openai e2b-code-interpreter
```

### 2. Set your API keys

```bash
export E2B_API_KEY="e2b_***"
export OPENAI_API_KEY="sk-***"
```

### 3. Add a code-execution tool

```python
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. Run with one sandbox per run

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

## What runs in the sandbox

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

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

## More agent frameworks

Other frameworks that run their tools on E2B.

- [LangChain Deep Agents](https://e2b.dev/integrations/langchain-deepagents.md): Back LangChain Deep Agents with an E2B sandbox through the langchain-e2b package.
- [CrewAI](https://e2b.dev/integrations/crewai.md): Give CrewAI agents E2B tools for shell commands, Python, and files with the crewai-tools[e2b] extra.
- [Pydantic AI](https://e2b.dev/integrations/pydantic-ai.md): Run Pydantic AI agent tools in E2B through the pydantic-ai-harness E2B sandbox.

## Links

- [Get API key](https://console.e2b.dev/?tab=keys)
- [Read the docs](https://docs.e2b.dev/agents/langgraph)
- [E2B documentation](https://docs.e2b.dev)
