MegaBrain University · guide
Codex CLI Custom Provider: Run Codex on GPT-6 and Other Models With One API Key
Updated · 5 min setup
Short answer
To run Codex on MegaBrain, add [model_providers.megabrain] to ~/.codex/config.toml with base_url = "https://getmegabrain.com/api/gateway/v1", env_key = "MEGABRAIN_API_KEY" and wire_api = "responses", then set model_provider = "megabrain" and model = "openai/gpt-6.1-sol". Codex then codes with GPT-6 paid per token from your balance, without a ChatGPT plan.
| Base URL (OpenAI-compatible) | https://getmegabrain.com/api/gateway/v1 |
|---|---|
| Base URL for Claude Code and the Anthropic SDK | https://getmegabrain.com/api/anthropic |
| Key | Authorization: Bearer <key> — the API key page in your dashboard |
| Default model | anthropic/claude-sonnet-5.5 |
| Free model for testing | mb-auto/free |
What is Codex
Codex is OpenAI’s open-source coding agent. It runs in the terminal (codex), in IDEs and headless in CI (codex exec): it reads the repo, follows the project’s AGENTS.md, edits files and runs commands inside a sandbox with approval modes, and uses MCP servers.
Codex signs in with ChatGPT by default, but its config accepts custom model providers. MegaBrain serves the OpenAI Responses API that Codex speaks, so one key and one balance give Codex GPT-6.1 Sol and Terra, and the same key works for every other model and agent in MegaBrain University.
Setup, step by step
Step 1. Get a MegaBrain API key
Sign up with GitHub, Google or email, add funds on the Balance page and create a key on the API key page. The key works for every text, image and video model, and each request is paid from your balance — no subscription.
Put the key in an environment variable so it never lands in a config file you share:
# ~/.zshrc or ~/.bashrc export MEGABRAIN_API_KEY="your-megabrain-key"curl https://getmegabrain.com/api/gateway/v1/chat/completions \ -H "Authorization: Bearer $MEGABRAIN_API_KEY" \ -H "Content-Type: application/json" \ -d '{"model": "anthropic/claude-sonnet-5.5", "messages": [{"role": "user", "content": "Say OK"}]}'Check the key before you touch the agent. A JSON reply with
choicesmeans the key, the balance and the model all work:Step 2. Install Codex
With npm, or with Homebrew on macOS:
npm install -g @openai/codex # or brew install --cask codexStep 3. Add MegaBrain as a model provider
Edit
~/.codex/config.toml. The provider must live in this user-level file: a project’s.codex/config.tomlcannot setmodel_provider.model = "openai/gpt-6.1-sol" model_provider = "megabrain" [model_providers.megabrain] name = "MegaBrain" base_url = "https://getmegabrain.com/api/gateway/v1" env_key = "MEGABRAIN_API_KEY" wire_api = "responses"env_keynames the environment variable that holds the key, so the key itself never sits in the file.wire_api = "responses"makes Codex call/responses, which MegaBrain serves.Step 4. Run Codex and check
Open a repo and run
codex. Ask for something that touches files — “add a unit test for utils/date.ts and run it”. If Codex edits the file and runs the test, the provider and tool calls work. For one task without the UI:codex exec "summarize what this repo does".Step 5. Switch models with profiles
A profile is a file next to the config with its own top-level keys. Create
~/.codex/cheap.config.tomland runcodex --profile cheapfor routine work (Codex 0.134 and later; older versions used[profiles.<name>]tables):# ~/.codex/cheap.config.toml model = "openai/gpt-6.1-terra" model_reasoning_effort = "low"
Which model to choose
Codex is tuned for OpenAI models: use openai/gpt-6.1-sol by default and openai/gpt-6.1-terra for cheaper routine runs. Other models in the catalog are served over the same Responses endpoint, but Codex’s prompts and tools are built around GPT; try another model on a small task first, and for Claude prefer Claude Code.
| Model ID | When to use it |
|---|---|
anthropic/claude-sonnet-5.5 | The default for an agent: reliable tool calls, holds long tasks, 1M-token context. |
anthropic/claude-opus-5.5 | Hard multi-step work, code review and analysis where a mistake costs more than tokens. |
openai/gpt-6.1-sol | OpenAI's flagship: the alternative to Claude for code and reasoning. |
google/gemini-3.8-flash | Fast and cheap: long documents, spreadsheets, inbox triage. |
moonshotai/kimi-k3.1-pro | A strong open-weight model for agents and code. |
z-ai/glm-5.3 | Open-weight model for code and tools with good price-to-quality. |
deepseek/deepseek-v4.1-flash | High-volume routine work: classification, drafts, summaries. |
mb-auto/free | Free, for checking your setup. 128K context, weaker at tool calls; do not send confidential data. |
Every model with its price and context window is in the catalog. They all work with the same key.
What you can do
AGENTS.md
Project instructions in AGENTS.md (build commands, conventions, what not to touch) are read on every run, so the agent follows the team’s rules.
Sandbox and approvals
Codex runs commands in a sandbox and asks before risky steps; approval modes decide how much it may do alone.
codex exec for automation
codex exec runs one task non-interactively — in CI, a cron job or a script — and prints the result.
MCP servers
Add MCP servers in config.toml to give Codex your issue tracker, database or docs.
Business use cases
Fix failing CI automatically
When a build fails, a CI step runs codex exec with the log: it finds the cause, fixes it on a branch and leaves a short explanation for the reviewer.
Task for the agent:
The CI job failed with the log in ci.log. Find the root cause, fix it with the smallest change, run the failing tests until they pass, and write a 3-line summary of the cause and the fix to FIX.md.Second opinion on Claude’s code
Teams that write with Claude Code ask Codex on GPT-6.1 Sol to review the diff: a different lab’s model catches different mistakes, on the same balance.
Batch chores across many repos
A script loops over repos and runs codex exec with a cheap profile: bump a dependency, update a license header, fix lint — one pull request each.
Internal tools for non-engineers
Ops and analysts describe a small tool in plain words and Codex builds it inside the sandbox, on a key with a known budget.
Troubleshooting
401 Unauthorized
The key is missing or mistyped, or the agent did not read the environment variable (restart the agent after export). Test the key with the curl command from step 1.
402 Payment Required
The balance is empty. Add funds on the Balance page, or switch to mb-auto/free to keep testing.
Model not found
The model ID must match the catalog exactly, including the lab prefix: anthropic/claude-sonnet-5.5, not claude-sonnet-5.5. The base URL must end in /v1.
Codex ignores the provider and asks to sign in with ChatGPT
The provider is in a project .codex/config.toml, which cannot set it. Move model_provider and [model_providers.megabrain] to ~/.codex/config.toml.
401 although the key is right
MEGABRAIN_API_KEY is not exported in the shell that starts Codex. Add the export to your shell profile and open a new terminal.
FAQ
Can I use Codex without a ChatGPT subscription?
Yes. With a custom provider pointing at https://getmegabrain.com/api/gateway/v1, Codex is paid per token from your MegaBrain balance; no ChatGPT plan or OpenAI account is needed.
Which wire_api should I use?
"responses". Codex speaks the OpenAI Responses API, and MegaBrain serves it at /responses under the same base URL.
How much does it cost?
You pay per token at each model’s price, from a prepaid balance in US dollars topped up by card. There is no subscription and no seat fee, so an agent that sits idle costs nothing. Prices per million tokens are in the model catalog; mb-auto/free costs nothing at all.
Can Codex use Claude or Gemini?
Every catalog model is served over the same Responses endpoint, so you can set model to one. Codex is built around GPT, though: for Claude, Claude Code is the better harness, and OpenCode suits any model.
Where do profiles go?
In separate files, ~/.codex/<name>.config.toml, run with codex --profile <name>. Since Codex 0.134, [profiles.<name>] tables inside config.toml are no longer read.
MegaBrain · one API for every model
Claude, GPT, Gemini, Kimi, GLM and DeepSeek in any agent — one key, one balance.
Sign up, add funds by card and pay only for what your agents use. No subscription. Free models work without a key.