- Application-managed: your application calls the E2B SDK to start, connect, and stop the sandbox. The Agents API Workbench in the E2B cookbook is built this way.
- Webhook-managed: a controller deployed in your E2B account reacts to OpenAI webhooks and creates a dedicated worker sandbox for each session. Your application only talks to the Agents API and never imports the E2B SDK. The E2B webhook-managed example is the E2B variant of the handler OpenAI ships.
agent_api_sdk Python package installs from the
preview repository
rather than PyPI.Looking for the open-source agent framework instead? See
OpenAI Agents SDK.Prerequisites
- an E2B API key
- an OpenAI project with Agents API access
- an OpenAI application key with Responses → Write, used to create sessions
- a restricted OpenAI executor key with only List models → Read, created in the same organization, project, and user or service account as the application key
- uv; every script declares its dependencies inline
CODEX_API_KEY. Agent-generated code can read it, so keep the application key
out of workers entirely.
Webhook-managed: set up the controller
Run everything from the webhook-managed example directory..env is gitignored and
holds the five values below.
Create an agent: once, prints the agent ID
.env as OPENAI_AGENT_ID.Build the worker template: once per Codex version, ~3 min
openai-agents-api-python-sdk-webhook-managed with
codex exec-server baked in, so a session’s first turn does not pay for an
npm install. Rebuild only after bumping CODEX_VERSION in
build_template.py.Start the controller: ~1 min, prints the webhook URL
OPENAI_WEBHOOK_SECRET empty the first time..controller.json. Rerunning
deploy.py reuses that sandbox, extends its one-hour timeout, and restarts
the handler with the current .env. If that sandbox is gone, it creates a
new one and prints the new webhook URL.uv run --env-file does not override variables already exported in your
shell. If E2B_API_KEY is exported there, that key wins over .env.Register the webhook: OpenAI platform, paste the URL
OPENAI_API_KEY, add the URL and subscribe to
agent.session.action_required and agent.session.failed. Put the
generated signing secret in .env and redeploy to the same controller:Health check: no side effects
Webhook-managed: run a session
New session: one OpenAI session + one E2B worker, ~40 s
{"session_id": "sess_..."}. Keep it.agent.session.action_required to the controller, the controller creates
the worker and launches codex exec-server inside it, the executor connects
out to OpenAI, and the turn runs in /workspace.Follow-up: same worker, starts immediately
SESSION_ID is the first line printed by the previous step.Cleanup: no deletion webhook, release both sides
New controller: ~1 min, prints a new webhook URL
Lifecycle
Workers stay running between turns and have a 30-minute timeout, refreshed on every reconnect. Pause a worker withe2b sandbox pause <id> and the next input
resumes it with files intact. A killed worker is replaced by a fresh sandbox
without the previous files.
When a session fails, the controller pauses its worker instead of killing it,
so you can inspect the sandbox afterwards. Kill it when done.
OpenAI traffic cannot wake a paused or expired controller. Rerun deploy.py
before the hour is up to extend it, or start a new controller as above. Remove
the OpenAI webhook before killing the controller for good.
Application-managed: Agents API Workbench
If you want the agent’s events in your own process, for a live UI or for function tools, your application creates the worker and starts the executor itself. The Agents API Workbench behind theopenai-agents-api-python-sdk template does exactly that: a Flask
backend that runs one executor sandbox per chat, and a React frontend with a
streaming transcript and a live /workspace file viewer.
Troubleshooting
Every session fails after five minutes with 500 internal_error
Every session fails after five minutes with 500 internal_error
deploy.py, that it is
registered in the project that owns the application key, and that
OPENAI_AGENT_ID matches the agent the session was created for.The webhook returns 503 Webhook not configured
The webhook returns 503 Webhook not configured
OPENAI_WEBHOOK_SECRET is empty in the controller. Put the signing secret
in .env and run deploy.py again.Inspect the controller and workers
Inspect the controller and workers
enqueued, started, and paused to
/app/controller.log. Each worker writes the executor output to
/tmp/codex-executor.log. Find them by metadata:
agents-webhook-controller=e2b for the controller and
agents-session-id=<session> for workers.