Codex CLI + MegaBrain Gateway
OpenAI Codex CLI is an open-source agentic coding tool that runs in the terminal. It reads your files, writes patches, and executes shell commands — all with your approval. With MegaBrain Gateway you can use it with any model, not just OpenAI.
Why MegaBrain
Any model
Use Claude, Gemini, Llama, Mistral, or GPT-4o — swap with a single flag.
No OpenAI lock-in
Route Codex CLI through MegaBrain and gain access to 500+ models without changing your workflow.
Unified billing
One API key, one invoice. No need to manage credentials across multiple providers.
Prerequisites
- Node.js 18 or later — download at nodejs.org
- A MegaBrain API key from your profile
Setup
Install Codex CLI
Install the CLI globally with npm:
npm install -g @openai/codexVerify the installation with codex --version.
Set environment variables
Codex CLI reads the standard OpenAI environment variables. Point them at MegaBrain:
export OPENAI_API_KEY="mb-your-key-here"
export OPENAI_BASE_URL="https://getmegabrain.com/api/gateway/v1"Add these lines to your ~/.zshrc or ~/.bashrc so they persist across sessions. Reload with source ~/.zshrc.
Run with a model
Pass any MegaBrain model ID with the --model flag:
codex --model anthropic/claude-sonnet-4-6 "refactor this function"Codex CLI will read relevant files in your working directory, propose changes, and ask for your approval before applying anything.
Or use a config file
To avoid repeating flags, create ~/.codex/config.json:
{
"model": "anthropic/claude-sonnet-4-6",
"apiKey": "mb-your-key-here",
"baseURL": "https://getmegabrain.com/api/gateway/v1"
}With the config file in place you can run Codex CLI with just a prompt:
codex "add unit tests for the auth module"How Codex CLI works
Reads files
Codex CLI scans your project tree and pulls in relevant source files as context before planning any changes.
Writes patches
Changes are proposed as unified diffs. You review each patch before it is applied to disk.
Runs shell commands
The agent can execute build commands, run tests, and inspect output — with explicit approval each time.
Approval loop
Nothing happens without your OK. Each file write and shell command is gated behind an interactive prompt.
Git-aware
Codex CLI respects .gitignore and surfaces diffs in terms of your existing git history.
Iterative
The agent loops — run, observe, plan next action — until the task is complete or you interrupt.
Recommended models
# Best overall coding agent
anthropic/claude-sonnet-4-6
# Fast and cost-effective
openai/gpt-4o-mini
# Large context for big refactors
google/gemini-2.5-pro
# Open-source alternative
meta-llama/llama-3.3-70b-instruct
# Auto-select the best available model
auto-frontierPass any of these as --model <id> or set it in ~/.codex/config.json. Browse all available models on the models page.
Troubleshooting
OPENAI_API_KEY is not recognized
Make sure you have reloaded your shell after editing ~/.zshrc or ~/.bashrc. Run echo $OPENAI_API_KEY to confirm the variable is set. Alternatively, set apiKey directly in ~/.codex/config.json.
Model not found / 404 error
Double-check the model ID on the models page. IDs are case-sensitive and follow the format provider/model-name. Also verify that OPENAI_BASE_URL ends with /api/gateway/v1 — no trailing slash.
Approval prompt keeps looping
This usually means the model is re-attempting a command that keeps failing. Press Ctrl+C to interrupt, inspect any error output, and re-run with a more specific prompt. You can also pass --approval-mode auto-edit to allow file edits without per-edit confirmation (shell commands still require approval).
Slow on large repositories
Codex CLI indexes your working directory on startup. Use a .gitignore or .codexignore file to exclude large generated directories like node_modules, .next, or dist. Running from a subdirectory also limits the scope Codex needs to read.