Prerequisites
- Python 3.11 through 3.14 and Git
- An OpenAI project with Agents API access and two API keys:
OPENAI_API_KEY, the project key. It stays with your application and the handler.OPENAI_EXECUTOR_API_KEY, a separate restricted executor key created in the same project by the same owner. It is the only OpenAI key passed to worker Sandboxes when configured and is required for webhook deployment.
- A Blaxel workspace, logged in with
bl loginor provided asBL_WORKSPACEandBL_API_KEY(API keys) - GitHub access to the OpenAI preview client referenced in the public Blaxel cookbook;
./run.shinstalls the client
us-was-1 is optional for the first run and required for the handoff, team example, and reconnection proof.
This walkthrough uses the Agents API preview client, which requires access from OpenAI. It is separate from the OpenAI Agents SDK tutorial.
Strict executor permissions require
api.agents.environments.connect; List models: Read alone is insufficient. Ask your OpenAI representative if that permission is unavailable.The application-managed examples warn and pass your project key to the Sandbox if OPENAI_EXECUTOR_API_KEY is absent; webhook deployment requires a separate executor key and rejects reuse of the project key.
This tutorial uses the Blaxel OpenAI Agents API cookbook. Its
run.sh is application-managed; its webhook/ directory is the handler.
Who owns what
1. Run the example
Prompt your agent
Copy this into a coding agent with terminal access:Run it yourself
The default
auto mode uses Agent Drive when available. Set BL_AGENT_DRIVE_MODE=off before the run for completely disposable storage.2. Check the result
A successful run confirms the generated artifact and temporary resource cleanup:confirmed generated file is the line that matters. The script reads summary.md back and checks it for an exact marker from the source file, which proves the agent used the provided file instead of returning an ungrounded answer.
3. Try the fresh-session handoff
Agent Drive becomes most useful when another agent continues from an explicit file instead of copied conversation history.summary.md, then its session and Sandbox are deleted. A fresh session and Sandbox mount the same Drive, read that file, and write review.md:
summary.md and review.md remain in the same Agent Drive run directory. The second agent must read the original verification marker before its review passes.
Agent Drive shares inspectable files. It does not copy model memory, conversation history, or session state.
Try only the storage handoff
4. Run a team on a shared Drive
From the configured checkout, run:
The two specialists work in parallel, each writing a separate file. Python waits for both tasks to complete and verifies their files before submitting the coordinator’s task. By then the specialists’ sessions and Sandboxes have been deleted; their findings remain on Agent Drive.
The coordinator also deletes its session and Sandbox after the final plan is read back. The script prints the retained Drive name so you can inspect
report.txt, both findings, and plan.md. If a specialist fails, the example cancels the other specialist and skips the coordinator’s task.
Agent Drive stores the shared files; the Python example defines the task order. Each run gets its own Drive and workload-label permissions, so independent teams do not share access.
5. Let a webhook handler run the Sandbox
In the webhook-managed mode your application never imports the Blaxel SDK. It creates a session for a saved agent and sends input. When a turn needs an executor that is not connected, OpenAI sendsagent.session.action_required with an environment_connection action to a handler you deploy once. The handler starts or reconnects the Sandbox, and the pending input continues without being resubmitted.
The controller creates Sandboxes on your behalf, so it needs a valid Blaxel API key for the selected workspace. A local bl login session does not validate an exported BL_API_KEY; use a durable API key for a long-lived controller.
- Creates a saved OpenAI agent and prints
export OPENAI_AGENT_ID=.... Export it; the handler only acts on sessions of this agent. - Starts the controller Sandbox
openai-agents-api-webhook-controllerinus-was-1and prints its public webhook URL.
agent.session.action_required and agent.session.failed. Export the signing secret OpenAI shows as OPENAI_WEBHOOK_SECRET and run the deploy again. Until then the endpoint answers 503 and accepts nothing.
To test alongside an existing deployment, set a unique OPENAI_WEBHOOK_RESOURCE_PREFIX and optionally OPENAI_AGENT_NAME before the first deploy, starting without the old OPENAI_AGENT_ID or signing secret. Register the new URL and keep its prefix, agent ID and secret consistent across redeployment and reconnect. See the cookbook’s separate-deployment steps. With no prefix, the original resource names remain unchanged.
The handler is a few hundred lines of Python in webhook/handler.py. It verifies every delivery, stores the session ID in SQLite before answering 200, and one loop re-reads the session and starts or reconnects its worker.
6. Reconnect with files preserved
note.md. The script then stops the executor, deletes that worker, waits for OpenAI’s session.environment.disconnected event, and sends a second turn in the same session. OpenAI sends agent.session.action_required again, and the handler starts a replacement worker on the same Drive path. The run passes only when the replacement reads the marker the first worker wrote.
session.environment.disconnected event before sending more input. Input sent while OpenAI still believes the executor is connected runs without file access and does not trigger the webhook.
The handler keeps the executor’s worker awake while it is connected. Release that compute by deleting the worker; Agent Drive preserves the mounted files for its replacement.
7. Choose the storage behavior
If Drive access is unavailable, the baseline still completes and prints the workspace-specific Console page for requesting access. The
--handoff path stops because a fresh Sandbox needs the persisted summary.md. The webhook handler logs a warning at startup and replacement workers start with an empty filesystem.
Agent Drive currently requires us-was-1, which is the cookbook default for both modes.
8. Make it yours
Keep the lifecycle and replace the example task:
Start with main.py for one application-managed session, handoff.py for a fresh agent reading a saved artifact, the team example for parallel work, or webhook/handler.py when OpenAI should trigger provisioning.
9. Review and clean up
Read the cookbook README for the current verified client and executor versions. Replace the fictional support brief with your own document and keep a verification rule that checks the actual generated files. Use a separate Drive and workload-label permissions for each independent webhook session. A replacement worker reuses its session’s Drive. The application-managed handoff intentionally shares a Drive between trusted agents; a mount path alone is not an isolation boundary. The cookbook submits each task once, verifies its new turn and durable idle state within 180 seconds (600 seconds for webhook-managed turns, including cold worker setup), and reads the retained final answer. It does not depend on live event delivery and rejects concurrent input to the same session. Cleanup cancels unfinished work if OpenAI requires an idle session before deletion. The controller’s SQLite queue survives process restarts within the same Sandbox. It does not survive deletion or expiration of that Sandbox.CONTROLLER_TTL defaults to 24h and WORKER_TTL to 2h, both measured from Sandbox creation. Restarting the controller process does not renew its lifetime.
The examples create hosted resources and the agent examples invoke a model. After a run, check the printed cleanup results rather than relying on the Sandbox TTL:
Deleting an OpenAI session does not send a cleanup webhook, so your application must also delete its worker. Use your configured resource prefix to identify the deployment and preserve resources belonging to other deployments.
Resources
OpenAI Agents API integration reference
Review execution modes, credentials, lifecycle rules, and configuration.
OpenAI Agents API cookbook
Run the complete examples and inspect the verification code.
Agent Drive
Configure durable files and request access.
Blaxel Sandboxes
Manage your agent’s execution environment.
