# 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.

Updated: 2026-11-01

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](https://learn.chatgpt.com/docs/pricing) lists Codex as part of its ChatGPT plans. These are the Plus and Pro plans, with the date we checked each one:

ChatGPT individual plans that include Codex (official prices)

| 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. | https://help.openai.com/en/articles/6950777-what-is-chatgpt-plus |
| 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. | https://help.openai.com/en/articles/9793128-about-chatgpt-pro-tiers |
| 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. | https://help.openai.com/en/articles/9793128-about-chatgpt-pro-tiers |
| 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. | https://help.openai.com/en/articles/9793128-about-chatgpt-pro-tiers |

Each row links to the page it comes from, checked October 31, 2026. These figures can change, so check the source before you buy.

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](https://5abeer.ai/en/docs/subscriptions/plan-apps.md#codex).

A key has one trade-off. [OpenAI's sign-in docs](https://learn.chatgpt.com/docs/auth) 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:

| Model | Id | Input (ours) | Output (ours) | Context |
| --- | --- | --- | --- | --- |
| GPT-6.1 Sol | `gpt-6.1-sol` | $0.60 | $3 | 922K |
| GPT-6 Astra | `gpt-6-astra` | $3 | $15 | — |
| GPT-6 Sol | `gpt-6-sol` | $0.60 | $3 | 922K |

As an example, a month of regular Codex sessions that sends twenty million tokens and gets one million back, on [`gpt-5.6-sol`](https://5abeer.ai/en/models/gpt-5.6-sol):

Example: 20,000,000 input tokens and 1,000,000 output tokens on GPT-5.6 Sol (`gpt-5.6-sol`) cost $30.00 at our price against $100.00 at the official price (70% lower). Official price 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](https://developers.openai.com/api/docs/pricing). 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](https://5abeer.ai/en/docs/start/pricing.md#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](https://5abeer.ai/en/api/openai).

## 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:

```bash
printenv OPENAI_API_KEY | codex login --with-api-key
```

On Windows PowerShell, `printenv` is not available. Use this line instead:

```powershell
$env:OPENAI_API_KEY | codex login --with-api-key
```

The 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](https://5abeer.ai/en/guides/ai-api-key-guide#official).

## 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:

### 1. Install Codex CLI

```bash
npm install -g @openai/codex
```

On macOS you can also use `brew install --cask codex`.

### 2. Create a key for GPT models

Top up your balance with one of the [payment methods](https://5abeer.ai/en/docs/start/payment.md), then create a key for the OpenAI provider, as in [Create a key](https://5abeer.ai/en/docs/start/keys.md#create). Give it a name such as `codex` so its usage is easy to find.

### 3. Add the provider to config.toml

Put these settings in `~/.codex/config.toml`, and save your key in the `KHABEER_API_KEY` environment variable, as in [Keep the key in an environment variable](https://5abeer.ai/en/docs/start/keys.md#env). Full per-OS setup is in the [Codex guide](https://5abeer.ai/en/docs/apps/codex.md).

`~/.codex/config.toml`

```toml
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"
```

To keep plain `codex` on your ChatGPT sign-in, use a profile, as in [How to switch between ChatGPT and a key](https://5abeer.ai/en/guides/codex-cli-api-key#switch).

### 4. Start Codex

```bash
cd your-project
codex
```

Ask 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 `khabeer` block 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.toml` and move the two top-level lines (`model` and `model_provider`) into `~/.codex/khabeer.config.toml`. Plain `codex` then keeps your ChatGPT sign-in, and `codex --profile khabeer` uses 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](https://5abeer.ai/en/docs/apps/codex.md#models), and the full catalog is on the [models page](https://5abeer.ai/en/models).

## Common errors and fixes

- [`INVALID_API_KEY`](https://5abeer.ai/en/docs/help/errors.md#INVALID_API_KEY): the key is wrong, or `KHABEER_API_KEY` isn't set in the terminal you started Codex from.
- [`MODEL_PLATFORM_NOT_ALLOWED`](https://5abeer.ai/en/docs/help/errors.md#MODEL_PLATFORM_NOT_ALLOWED): the key belongs to a provider other than GPT.
- [`INSUFFICIENT_BALANCE`](https://5abeer.ai/en/docs/help/errors.md#INSUFFICIENT_BALANCE): your balance doesn't cover the request. Top up, then try again.
- [`API_KEY_QUOTA_EXHAUSTED`](https://5abeer.ai/en/docs/help/errors.md#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](https://5abeer.ai/en/docs/help/errors.md).

## 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](https://5abeer.ai/en/docs/apps/claude-code.md) with a Claude key, and see [The cheapest way to use Claude Code](https://5abeer.ai/en/guides/claude-code-cheapest-way).

### 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](https://5abeer.ai/en/docs/start/keys.md#controls). 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](https://5abeer.ai/en/guides/pay-ai-api-from-egypt), and the details are on the [API in Egypt page](https://5abeer.ai/en/api/egypt).

### 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](https://5abeer.ai/en/docs/subscriptions/plan-apps.md#codex). Plans are on the [pricing page](https://5abeer.ai/en/pricing).

### 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](https://5abeer.ai/en/api/openai), or create a key for GPT models and start Codex on it.
