↩ BLOG
/
Guides

Python Guide: Run Claude Code in an E2B Sandbox

Tereza Tizkova
Growth & E2B
Share
JS/TS
PYTHON

Claude Code is Anthropic’s CLI that turns natural-language prompts into concrete edits, files, and shell commands. Run it inside an E2B Sandbox, and you get an isolated, long-running environment where the agent can safely create files, install tools, and execute without interactive prompts.

What we’ll build

  • In this guide, we will show step-by-step how to run Claude Code in E2B’s isolated cloud sandbox.
  • We’ll pass a simple prompt (“create a hello-world index.html”), cover the two timeouts you should care about, and keep secrets in .env.
  • We will build a one-shot command that runs a prompt using Claude Code agent and returns the output.

See full example in the E2B Cookbook.

This is a Python guide using E2B Python SDK, for the JS/TS SDK version, see the JavaScript version.

Components we’ll use

  • E2B Python SDK - creates and controls the E2B sandbox.
  • Prebuilt E2B template: anthropic-claude-code - ships with @anthropic-ai/claude-code ready to go.
  • Anthropic API - powers Claude Code via your Anthropic API key.

Prerequisites

Outline

  1. Create the project
  2. Add the files (pyproject.toml, main.py)
  3. Set API keys in .env
  4. Install with pip and run

1) Create the project

First, we create the directories and the Python package folder.

mkdir anthropic-claude-code-in-sandbox-python
cd anthropic-claude-code-in-sandbox-python
mkdir -p anthropic_claude_code_in_sandbox

This is the target structure of the project. Keeping code in anthropic_claude_code_in_sandbox/ (with __init__.py) lets pip install -e . work cleanly and keeps imports tidy.

anthropic-claude-code-in-sandbox-python/
├─ anthropic_claude_code_in_sandbox/
│  ├─ __init__.py
│  └─ main.py
├─ pyproject.toml
├─ README.md              
├─ .gitignore
└─ .env    

2) Files to add

Now we create the following files.

File: pyproject.toml

[project]
name = "anthropic-claude-code-in-sandbox"
version = "0.1.0"
description = "Run Anthropic Claude Code in an E2B Sandbox"
readme = "README.md"
requires-python = ">=3.11"
dependencies = [
  "e2b",
  "python-dotenv",
]

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

File: anthropic_claude_code_in_sandbox/__init__.py

# intentionally empty

File: anthropic_claude_code_in_sandbox/main.py

The main.py file reads both keys from .env, uses the prebuilt template, 5-minute sandbox timeout, and no command timeout - so Claude may run as long as needed. In the main.py, we first import everything we need and load .env into process environment:

from dotenv import load_dotenv
from e2b import Sandbox

load_dotenv()  # loads .env into process environment

Then we initiate the sandbox instance.

template_name = "anthropic-claude-code"
sbx = Sandbox(
    template_name,
    envs={"ANTHROPIC_API_KEY": "<your api key>"},
    timeout=60 * 5,  # 5 minutes; match README (remove this line to match the file exactly)
)
print("Sandbox created", sbx.sandbox_id)

If you need more diagnostics, also print result.stderr. We add an example that generates a file via Claude Code inside the sandbox, print the result, and then kill the sandbox.

# Run a prompt with Claude Code
result = sbx.commands.run(
    "echo 'Create a hello world index.html' | claude -p --dangerously-skip-permissions",
    timeout=0,  # allow long-running commands
)
print(result.stdout)

sbx.kill()

We also need to add a .gitignore and .env files to store secrets/config outside code and git history.

File: .gitignore

.venv/
.env
__pycache__/
*.pyc

File: .env

E2B_API_KEY="YOUR_E2B_API_KEY"

3) Set up and install

python -m venv .venv
# macOS / Linux
source .venv/bin/activate
# Windows (PowerShell)
# .venv\Scripts\Activate.ps1

pip install -e .

4) Run the example

python anthropic_claude_code_in_sandbox/main.py

What’s expected is the console output from Claude Code. The created files (e.g., index.html) live inside the sandbox filesystem.

See full example in the E2B Cookbook.

Follow @e2b on X to see live updates.
/
We're hiring! Check open positions on the Careers page.
/
Hundreds of millions of sandboxes launched.
/
We're always Excited 2 Build.
/