Skip to main content
This tutorial explains how to run OpenAI Codex inside a Blaxel sandbox and have it execute coding tasks on a codebase hosted in a different sandbox.

Prerequisites

Before starting, ensure you have:

Install the Blaxel CLI and SDK

  1. Download and install the Blaxel CLI and log in to your Blaxel account:
  2. In a new directory, install the Blaxel SDK (Python and TypeScript are both supported):

Create sandboxes

  1. In the host environment, define the following variables:
  2. Create a script named main.py (Python) or index.ts (TypeScript) in the same directory.
    This script creates two Blaxel sandboxes:
    • codex-sandbox using Blaxel’s pre-built Codex image, which includes the Codex CLI
    • nextjs-sandbox using Blaxel’s Next.js base image
    In the Codex sandbox, it:
    • adds the Blaxel and OpenAI API keys to codex-sandbox as environment variables.
    In the application sandbox, it:
    • starts the Next.js dev server in nextjs-sandbox on port 3000;
    • creates a preview URL for the Next.js service running in nextjs-sandbox on port 3000;
    • creates an access token for the preview URL, valid for 24 hours;
    • returns the preview URL.
  3. Run the script to create the sandboxes and preview URL:
    Once complete, the script displays the generated preview URL for the Next.js application (for example, https://b186....preview.bl.run?bl_preview_token=cbba622560db78e...) and the MCP server URL (for example, https://sbx-nextjs-sandbox....bl.run/mcp) for the Next.js sandbox. Note these values, as you will require them in subsequent steps.

Configure and test Codex

  1. Connect to the Codex sandbox terminal:
  2. Configure Codex to connect to the application sandbox’s MCP server URL (obtained from the sandbox creation script in the previous section) with your Blaxel API key (set in the sandbox environment):
  3. Start Codex and confirm that it is connected to the sandbox MCP server with the /mcp command. This command should return the list of tools available in the sandbox.

Test Codex

Once Codex starts, give it a coding task referencing the application sandbox, as in the example prompt below:
Codex will connect to the application sandbox, inspect the Next.js codebase and make changes as per your request. Once complete, visit the application sandbox’s preview URL (obtained from the sandbox creation script in the previous section) to see the result.

Resources

Want more information on building and deploying with OpenAI on Blaxel? Check out the following resources:

Use OpenAI Agents SDK with Blaxel sandboxes

Build compute-capable agents backed by Blaxel sandboxes using OpenAI Agents SDK.

Deploy on Blaxel

Build and deploy agents with OpenAI Agents SDK to Blaxel.
Last modified on July 10, 2026