Skip to main content
If you used Reflex for coding tasks, personas, or connected tools, the following examples show how to build those workflows with the OpenAI Agents API. Each starts from a Reflex usage pattern and shows the corresponding API calls. The examples use OpenAI-managed sandboxes (environment.type: "openai_hosted"). OpenAI provides the workspace; your application supplies instructions, inputs, and tools.
OpenAI also supports self-hosted sandboxes on your own laptop, container, or compute. That option requires you to manage the environment and its lifecycle. This guide covers the managed sandbox only.

Before you begin

Use a small task you have already done in Reflex and keep its brief and expected output for comparison. These examples create new sessions; existing Reflex conversations, personas, and connections are not imported. You need Python 3.11 or newer and an OpenAI project with Agents API access. Set OPENAI_API_KEY in your application shell or secret manager. Grant api.agents.read, api.agents.write, and api.responses.write; the GitHub example also needs api.vaults.read and api.vaults.write. Keep this application key outside the sandbox. See the OpenAI quickstart. Save the shared setup as common.py, then save the scripts below beside it:
The model defaults to gpt-6-astra; set OPENAI_AGENT_MODEL to use another model available to your project. Runs incur model and sandbox charges. All runnable code is included here, with matching public source links. Examples have been checked offline; live results depend on your account and task.

If you started a task in Reflex

Create a session with a managed sandbox, include the brief in environment.files, then send the first message. Ask the agent to write deliverables under /workspace/outputs so you can download published artifacts. Files and artifacts describes uploads and output retrieval. Source copy: 01_start_task.py.
01_start_task.py
Run .venv/bin/python 01_start_task.py. The shared helper waits for the main turn to finish and the session to become idle, downloads the artifact from that turn, then deletes the session. Check the plan itself: turn completion does not guarantee that the result meets your requirements.

If you sent follow-up messages in Reflex

Send the next message to the same session ID. The session keeps conversation context, and the managed workspace keeps files across turns while the sandbox exists. See Run and continue sessions. Source copy: 02_follow_up.py.
02_follow_up.py

If you used a persona

Put the persona’s role, priorities, and response style in agent.instructions, then reuse that configuration when creating sessions. Copy only the user-facing instructions you want to keep. A saved agent can also hold reusable configuration. Instructions and tools cannot be changed through a session update; create a new session to change them. See Configuring Agents. Source copy: 03_use_persona.py.
03_use_persona.py

If you connected MCPs in Reflex

Add the corresponding server to agent.tools. Carry over its endpoint and intended tool access, then authenticate it for the new session. Reflex connections do not automatically become Agents API connections.

Connect other services and local paths

Use the route that matches where the tool lives: For HTTP from the environment, localhost means the sandbox, and the endpoint must be reachable under its network policy. Stdio starts a process inside that sandbox. Managed stdio MCP connections currently require network.access: "enabled". The following example supports three routes. Run 04_connect_mcp.py service for the public OpenAI documentation MCP, environment for the same endpoint reached from the sandbox, or stdio for a small uploaded tool. See MCP connections. Source copy: 04_connect_mcp.py.
04_connect_mcp.py
For authenticated HTTP MCPs, use transport.authorization or headers for a session, or attach a vault for reusable service-origin credentials. Environment-origin HTTP does not use vault MCP credentials; supply inline authentication or use a trusted proxy. Stdio can inherit selected environment variables through transport.env_vars.

If you suspended and resumed work in Reflex

For a managed sandbox, separate stopping a turn from retaining work:
  1. To stop active work, send agent.session.input.cancel. Closing the stream alone does not cancel it.
  2. Wait until the session is idle, then retain its ID. Send the next instruction to that same ID to continue.
  3. Keep important files published under /workspace/outputs and download copies you need to retain.
The documented managed API does not expose sandbox suspend/resume or promise a frozen process image. Cancelling a turn keeps the session; continuing starts a new turn, rather than resuming an interrupted shell command. See cancel an active turn. Connected sandboxes receive keep-alives between turns. If activity and keep-alives stop for an hour, the sandbox can be deleted. Conversation retention does not guarantee workspace availability. If the sandbox expires, download published artifacts, create a new managed session, and upload the files you need as inputs. Environment templates preserve configuration, not live workspace state. See managed sandbox lifetime. This example deliberately keeps its session so separate invocations can continue it. Run:
Use the printed ID in place of SESSION_ID. To stop an active turn from another terminal, run .venv/bin/python 05_return_to_work.py cancel SESSION_ID. A session left open may incur charges; delete it when finished. The continue command checks the environment before requesting another turn. Source copy: 05_return_to_work.py.
05_return_to_work.py

If you connected GitHub in Reflex

Authenticate GitHub independently for the Agents API. The example below has two read-only paths:
  • mcp: store a bearer token scoped to GitHub’s MCP endpoint in a vault and allow only search_issues and issue_read.
  • api: store an environment_variable credential for api.github.com, then make an authenticated HTTPS request from the sandbox.
The GitHub example prints its temporary vault ID for recovery. If vault cleanup fails, delete that vault through the Vaults API after checking the session. Set GITHUB_TOKEN in the application environment using your secret manager. Use a token authorized for the repositories you intend to read. GITHUB_REPOSITORY is owner/repo (required for MCP mode). Run .venv/bin/python 06_github_auth.py mcp or .venv/bin/python 06_github_auth.py api. In API mode, the sandbox receives a placeholder; OpenAI’s egress proxy substitutes the real secret only for allowed HTTPS destinations. Pass that placeholder unchanged in the authorization header. This example does not authenticate git clone or configure a GitHub login flow. See Vaults. Source copy: 06_github_auth.py.
06_github_auth.py

If you scheduled tasks in Reflex

Have your scheduler invoke the start-task example for each run. Save the session ID and downloaded outputs with that job’s record. Your application owns scheduling, retries, and overlapping-run policy. For event-driven progress, use session webhooks.

Shared setup

Save this as common.py. It uses only the OpenAI SDK. It scopes downloads to the completed turn, bounds execution and cleanup waits, and prints controlled errors without dumping SDK response bodies. The short examples delete their sessions; the return-to-work example explicitly retains one. Source copy: common.py.
common.py