Start and manage an Anthropic Managed Agents self-hosted environment worker from your app or CLI.
This is the simplest E2B shape: your host process starts one E2B sandbox from the managed-agents
template, launches Anthropic's EnvironmentWorker.run() inside it, then creates Managed Agents
sessions that route tool calls to that worker.
flowchart LR
app["App or CLI"] --> session["Managed Agents session"]
session --> env["Anthropic self-hosted environment"]
worker["E2B worker sandbox"] --> env
worker --> tools["bash/read/write/edit/glob/grep"]
tools --> workdir["/mnt/session"]
Run these commands from this directory. The Python package and .env live one level up.
uv sync --project ..
cp ../.env.template ../.envFill in .env:
| Variable | Notes |
|---|---|
E2B_API_KEY |
Required to start worker sandboxes. |
E2B_ACCESS_TOKEN |
Required to build the E2B template. |
ANTHROPIC_API_KEY |
Used to create environments, agents, and sessions. |
ANTHROPIC_ENVIRONMENT_ID |
Printed by create-environment. |
ANTHROPIC_ENVIRONMENT_KEY |
Generated from the Anthropic Environments workspace. See Anthropic's environment docs. |
ANTHROPIC_AGENT_ID |
Printed by create-agent. |
For the Anthropic concepts behind these commands, see the Managed Agents docs for environments and agents.
make create-environment NAME=my-e2b-envSave the printed ANTHROPIC_ENVIRONMENT_ID, open the printed URL in the Anthropic Environments workspace, and generate
ANTHROPIC_ENVIRONMENT_KEY for the self-hosted environment.
make create-agent NAME=my-e2b-agentSave the printed ANTHROPIC_AGENT_ID.
make build-templateThis bakes the Python package, Anthropic SDK, FastAPI/Uvicorn webhook dependencies, shell tools, and
/mnt/session workdir into the anthropic-managed-agents E2B template.
make start-workerThe command prints E2B_WORKER_SANDBOX_ID and stores that value on the Anthropic environment
metadata as e2b_worker_sandbox_id. That gives another process a lookup path from
ANTHROPIC_ENVIRONMENT_ID to the active E2B sandbox:
make show-environmentStop that sandbox later with:
make stop-worker SANDBOX_ID="<sandbox-id>"If the stopped sandbox ID matches e2b_worker_sandbox_id, the stop command clears that metadata key.
With the worker running:
make sendExpected event stream signals:
UserToolResultEvent ... text='/mnt/session'
UserToolResultEvent ... text='hello from E2B'
SessionStatusIdleEvent ... stop_reason=EndTurn
To upload a local file into the running E2B worker sandbox:
make upload-file \
SANDBOX_ID="<sandbox-id>" \
FILE=/tmp/example-input.txt \
REMOTE_PATH=/mnt/session/uploads/example-input.txtThen ask the agent to read /mnt/session/uploads/example-input.txt. Anthropic session resources
are not supported for self-hosted environments, so this example uploads files through E2B.
- This is a simple worker demo, not per-session isolation.
- One worker sandbox can service the self-hosted environment. Start more workers for more capacity.
- Persistent state is scoped to the worker sandbox. Files under
/mnt/sessioncan be shared by any session that worker claims. - Tool calls execute inside the E2B sandbox under
/mnt/session. - Secrets and Anthropic resource IDs stay runtime-only in
../.env; they are not baked into the E2B template.
Use ../app-webhooks/ with APP_SANDBOX_ROUTING_SCOPE=session when each Managed Agents session
needs its own sandbox and follow-up state.
For a concrete event-by-event walkthrough, see ../EXAMPLE_USAGE.md. For a complete code-level implementation, see IMPLEMENTATION.md.