Generate Code Using OpenAI Codex in a Koyeb Sandbox
Codex is OpenAI's cloud-based software engineering agent that can work on many tasks in parallel. It's designed to write features, answer questions about your codebase, fix bugs, and propose pull requests for review.
Running OpenAI Codex in a sandboxed environment is ideal because Codex dynamically generates and executes code, which can include untrusted or unpredictable logic that must be isolated from your core systems for safety, security, and resource control. A sandbox provides a controlled environment with strict boundaries, preventing generated code from accessing sensitive files, affecting host infrastructure, or consuming unbounded compute. Koyeb Sandboxes are an ideal choice for running Codex-generated code because they offer fast, ephemeral, fully isolated execution environments with built-in GPU/CPU resources, secure network and filesystem isolation, and an API-driven workflow that lets you spin up, run, and tear down environments in seconds. This makes it easy to safely test, iterate, and orchestrate Codex workflows at scale without managing infrastructure or compromising security.
You will need:
- A Koyeb account - it's free to get started!
- A Koyeb API key to create and manage Sandboxes
- Python 3.10+ installed on your machine
- An OpenAI account on a plan that enables Codex. See Codex website for details
- An OpenAI API key
Steps
To build an OpenAI Codex agent generating code in a Koyeb Sandbox, you'll complete the following steps:
Set up the environment
Create a new folder for your project and change to the directory:
mkdir example-codex
cd example-codex
In the example-codex folder, create a new virtual environment folder using the following command:
python -m venv venv
Activate and load your virtual environment:
. venv/bin/activate
Install the required packages:
pip install koyeb-sdk
Set up local environment variables
Set your Koyeb access token in the environment. You can generate an access token in the Koyeb control panel settings.
export KOYEB_API_TOKEN=your-koyeb-api-token
Set your OpenAI API key in the environment. See the OpenAI dashboard to generate a key.
export OPENAI_API_KEY=your-openai-api-key
Create a Koyeb Sandbox with Codex
Create a file called main.py and add the following code, replacing the placeholder with your OpenAI API key:
import os
from koyeb import Sandbox
# Create a new sandbox environment
sandbox = Sandbox.create(
image="node",
instance_type="medium",
wait_ready=True,
env={"OPENAI_API_KEY": os.getenv("OPENAI_API_KEY")},
)
# Install OpenAI Codex CLI
sandbox.exec("npm install -g @openai/codex", timeout=600, on_stdout=lambda data: print(data.strip()))
# View available OpenAI Codex CLI commands
sandbox.exec("codex --help", timeout=30, on_stdout=lambda data: print(data.strip()))
# Log in to OpenAI Codex CLI using the API key from environment variable
sandbox.exec('printenv OPENAI_API_KEY | codex login --with-api-key', on_stdout=lambda data: print(data.strip()))
# Use OpenAI Codex CLI to create a Python app and run it
sandbox.exec("codex exec --skip-git-repo-check --dangerously-bypass-approvals-and-sandbox 'Create a simple Python script that writes \"Hello, world!\" to a file /workspace/hello-world.txt and execute it without asking'", on_stdout=lambda data: print(data.strip()))
sandbox.exec("cat /workspace/hello-world.txt", on_stdout=lambda data: print(data.strip()))
# Clean up by deleting the sandbox
sandbox.delete()
Understand the script
This code takes the following actions:
- Creates a new Koyeb Sandbox running the Node.js official Docker image on a medium CPU Instance
- Installs the OpenAI Codex CLI using npm
- Logs into the OpenAI Codex CLI
- Executes a command to create a Python script that generates a "Hello, world!" text file, using the CLI's flag to bypass approvals and allow the agent to have full access to the sandbox environment
- Deletes the sandbox
Run the code
Run your code using the following command:
python main.py
The output will list all of the available commands of the Codex CLI, and then generate the "Hello World" page.
That's it!
You've successfully used the Koyeb Python SDK to generate a sandbox environment to run OpenAI Codex code generation. Koyeb spun up a CPU Instance, added the OpenAI Codex CLI, and used the Codex CLI's commands to create a Python file, add code, run it, and read the result. The Koyeb Python SDK can be used to run commands, create and edit files, expose ports, add and update directories, upload and download files, and more, giving you full control over your sandbox environments.
Next steps
- Learn more about Koyeb Sandboxes
- Complete the Sandbox Quickstart to get familiar with the basics of the Koyeb Python SDK for sandboxes

