Run Codex CLI with an API key: the complete guide
How to run OpenAI's Codex CLI on an API key instead of a ChatGPT sign-in, what it costs per token, and how to set up a custom provider in config.toml.
Codex CLI can run on an API key instead of a ChatGPT sign-in. With an OpenAI key, you sign in with codex login --with-api-key. With an OpenAI-compatible service such as 5abeer, you add a model provider with its base URL to ~/.codex/config.toml, and Codex reads your key from the KHABEER_API_KEY environment variable.
Should you use a ChatGPT plan or an API key?#
Codex runs on either one. OpenAI's Codex pricing page lists Codex as part of its ChatGPT plans. These are the Plus and Pro plans, with the date we checked each one:
| Service | Item | Value | Note | Source |
|---|---|---|---|---|
| ChatGPT | Plus | $20 a month | Includes Codex. Read from a search result for the page, because the page itself didn't open for us. | help.openai.com |
| ChatGPT | Pro 100 | $100 a month | Includes Codex with more usage. Read from a search result for the page, because the page itself didn't open for us. | help.openai.com |
| ChatGPT | Pro 200 | $200 a month | More usage than Pro 100. Read from a search result for the page, because the page itself didn't open for us. | help.openai.com |
| ChatGPT | Pro 500 | $500 a month | The highest usage of the Pro plans. Read from a search result for the page, because the page itself didn't open for us. | help.openai.com |
A plan costs a fixed amount each month and comes with usage limits. A key is billed by the tokens each request sends and receives, with no monthly fee. A key fits best when:
- You use Codex now and then, and a monthly plan would sit mostly unused.
- Codex runs in a script, a container or a CI job, where nobody can sign in through a browser.
- You want a spend limit per project instead of one limit for everything.
If you would rather not pay per token, a 5abeer plan that includes Codex runs it with no key; see What each plan runs.
A key has one trade-off. OpenAI's sign-in docs say that "API key authentication supports local Codex workflows, but some features that rely on ChatGPT workspace access or cloud services are limited or unavailable." If you depend on those cloud features, keep the ChatGPT sign-in for them.
How much does Codex cost on an API key?#
A coding agent sends a lot of input. Each turn carries your request plus the files, command output and conversation that Codex needs, so input tokens usually far outnumber output tokens. These are the newest GPT models, at our price per million tokens:
GPT-6.1 SolInput$0.60Output$3Context922K
GPT-6 AstraInput$3Output$15Context—
GPT-6 SolInput$0.60Output$3Context922K
Official price from the provider's page, checked October 19, 2026.
All models and pricesAs an example, a month of regular Codex sessions that sends twenty million tokens and gets one million back, on gpt-5.6-sol:
20M input tokens and 1M output tokens
Official price from the provider's page, checked October 19, 2026.
Treat this as an upper bound: it prices every input token at the full rate, while Codex resends much of the same context each turn and OpenAI bills cached input at a lower rate. Your own numbers depend on the size of your repository and how long your sessions run. Use one key for Codex for a week and read its usage, as in Track your usage, before you decide between a key and a plan. The official price of each model sits next to ours on the OpenAI API page.
How to sign in to Codex with an OpenAI key#
If your key comes from the OpenAI Platform, Codex keeps its built-in OpenAI provider. Save the key in the OPENAI_API_KEY environment variable, then pass it to Codex:
printenv OPENAI_API_KEY | codex login --with-api-keyOn Windows PowerShell, printenv is not available. Use this line instead:
$env:OPENAI_API_KEY | codex login --with-api-keyThe account behind the key needs API credit before it can make paid calls. Where to create the key and how the official consoles take payment are in How to get an API key.
How to run Codex CLI on a 5abeer key#
5abeer serves GPT models in the Responses format that Codex uses, at our price. Codex reads its settings from ~/.codex/config.toml, so the whole setup is one file and one environment variable:
Install Codex CLI#
Terminalnpm install -g @openai/codexOn macOS you can also use
brew install --cask codex.Create a key for GPT models#
Top up your balance with one of the payment methods, then create a key for the OpenAI provider, as in Create a key. Give it a name such as
codexso its usage is easy to find.Add the provider to config.toml#
Put these settings in
~/.codex/config.toml, and save your key in theKHABEER_API_KEYenvironment variable, as in Keep the key in an environment variable. Full per-OS setup is in the Codex guide.model = "gpt-6.1-sol" model_provider = "khabeer" [model_providers.khabeer] name = "5abeer" base_url = "https://api.5abeer.ai/v1" env_key = "KHABEER_API_KEY" wire_api = "responses"~/.codex/config.toml
To keep plain
codexon your ChatGPT sign-in, use a profile, as in How to switch between ChatGPT and a key.Start Codex#
Terminalcd your-project codexAsk for something small. If a reply comes back, the key and the base URL both work.
What does each line in config.toml do?#
- `model`: the model Codex starts with. Copy any model id from the table above.
- `model_provider`: which provider block to use. Here it points to the
khabeerblock below it. - `base_url`: where Codex sends its requests. For 5abeer, that is our OpenAI-format base URL.
- `env_key`: the name of the environment variable that holds the key. The key itself never goes in the file.
- `wire_api`: the request format.
"responses"is the only value Codex supports, and it is the default.
You don't need codex login for this provider. Codex reads the key from the variable named in env_key each time it starts.
How to switch between ChatGPT and a key#
You don't have to choose once. Codex supports profiles: when you pass --profile profile-name, it loads ~/.codex/config.toml and then applies ~/.codex/profile-name.config.toml on top. Two common setups:
- Keep the
[model_providers.khabeer]table in~/.codex/config.tomland move the two top-level lines (modelandmodel_provider) into~/.codex/khabeer.config.toml. Plaincodexthen keeps your ChatGPT sign-in, andcodex --profile khabeeruses the key. - Override one setting for one run with
-c, for example to try another model without editing the file.
Codex keeps its settings and local state in ~/.codex unless the CODEX_HOME variable points somewhere else.
Which model should you pick in Codex?#
The table above shows the newest GPT models. Change the model line in the file, or pick a model inside a session with /model. A stronger model fits planning and large changes, and a cheaper one fits quick edits and questions. Every model that runs in Codex is listed on the Codex CLI setup page, and the full catalog is on the models page.
Common errors and fixes#
INVALID_API_KEY: the key is wrong, orKHABEER_API_KEYisn't set in the terminal you started Codex from.MODEL_PLATFORM_NOT_ALLOWED: the key belongs to a provider other than GPT.INSUFFICIENT_BALANCE: your balance doesn't cover the request. Top up, then try again.API_KEY_QUOTA_EXHAUSTED: the key reached the spend limit you set on it.
Every code and what it means is in Errors and fixes.
FAQ#
Do I need to run codex login with a 5abeer key?#
No. With the provider block in config.toml, Codex reads the key from KHABEER_API_KEY on every start. codex login --with-api-key is for OpenAI keys on the built-in provider.
Can I run Claude models in Codex?#
No. Each 5abeer key covers one provider, and Codex runs GPT models here. For Claude models, use Claude Code with a Claude key, and see The cheapest way to use Claude Code.
Will setting up a key remove my ChatGPT sign-in?#
No. If you move the model and model_provider lines into a profile, your main config still uses the ChatGPT sign-in, and Codex uses the key only when you start it with that profile.
Why does Codex say the key is missing?#
KHABEER_API_KEY isn't set in the terminal that started Codex. Set it again, or open a new terminal after saving it permanently, then run codex again.
Can I cap what Codex spends?#
Yes. Create a key just for Codex and give it a spend limit and an expiry date, as in Control every key. When it reaches the limit, requests return API_KEY_QUOTA_EXHAUSTED until you raise it.
Can I pay from Egypt?#
Yes. The ways to pay from Egypt and the steps are in How to pay for the OpenAI or Claude API from Egypt, and the details are on the API in Egypt page.
Can I run Codex on a 5abeer plan instead of a key?#
Yes, if your plan includes Codex. The 5abeer AI app sets Codex up on your plan with no key, as described in What each plan runs. Plans are on the pricing page.
Is 5abeer part of OpenAI?#
No. 5abeer is an independent service, not affiliated with OpenAI.
Get started#
Compare GPT prices on the OpenAI API page, or create a key for GPT models and start Codex on it.
Related guides
- How to get an API key for Claude, GPT and GeminiWhat an AI API key does, where to create one at Anthropic, OpenAI and Google, what it costs to use, and how to keep it safe.
- The cheapest way to use Claude Code: a plan or an API key?What Claude Code costs on a Claude plan and on an API key, Anthropic's published cost per developer, and which route is cheaper for light and heavy use.
- One subscription for all AI apps: ChatGPT, Claude, CodexOne 5abeer subscription runs ChatGPT, Codex, Claude Code, Antigravity, OpenCode, OpenClaw and Hermes below the official price. Which plan runs which app.
Ready to start?
Pick a plan that runs the official apps, or create an API key to use from your own code.