Codex API key: login, CI use, billing and errors
Sign Codex in with an OpenAI API key: codex login --with-api-key, CODEX_API_KEY in CI, where the key is kept, API prices, what needs ChatGPT, and the errors.
Save the key once with printenv OPENAI_API_KEY | codex login --with-api-key. Codex keeps it in ~/.codex/auth.json, and the CLI, the IDE extension and codex exec use it from then on. For a single scripted run, set CODEX_API_KEY on the command instead; for codex exec it overrides a saved ChatGPT sign-in. An OPENAI_API_KEY exported in your shell does not sign Codex in. A key is billed per token to your OpenAI Platform account, not your ChatGPT plan: GPT-6.2 Sol, Codex’s default model, costs $2 per million input tokens and $10 per million output as of November 23, 2026. It runs Codex locally with the models your API organization and project can use, but Codex cloud, GitHub code review, Slack and the phone remote need a ChatGPT sign-in.
Save a key with codex login
Create a key on the Platform’s API keys page, then pipe it to Codex, the form OpenAI’s authentication page gives:
$ printenv OPENAI_API_KEY | codex login --with-api-key
Reading API key from stdin...
API key saved. Authentication is checked when you send a request.CopiedAny pipe or redirect works, such as codex login --with-api-key < key.txt, and spaces or a newline around the key are trimmed. Run it with nothing piped and it stops with “--with-api-key expects the API key on stdin. Try piping it”; pipe an empty variable and it says “No API key provided via stdin.”
Take the last sentence of the output literally: Codex does not test the key when it saves it. A mistyped or revoked key is accepted here and fails on the first request. codex login status has the same blind spot. It prints the key’s first eight and last five characters and exits 0 without contacting OpenAI:
$ codex login status
API key configured - sk-proj-***ABCDE
Authentication is checked when you send a request.CopiedThe check is one real request, billed like any other turn: codex exec --skip-git-repo-check "reply with OK".
Other ways in, as of Codex 0.171.0 (November 2026):
- The CLI’s sign-in screen. Start
codexwith nothing saved and it offers Sign in with ChatGPT, Sign in with Device Code and Sign in another way; the last leads to Use an OpenAI API key (“Pay for what you use”). IfOPENAI_API_KEYis set in that shell, the box is filled in for you and the screen says “Detected OPENAI_API_KEY environment variable.” Once saved it shows “✓ API key configured”. - The IDE extension. On the signed-out screen, select Use API Key, enter the key and select OK. The CLI and the extension share one saved sign-in, so a key saved in either is used by both.
- The ChatGPT desktop app. On the signed-out screen, select Sign in another way, enter the key and select Continue.
Where Codex keeps the key
By default in a file, auth.json, inside CODEX_HOME, which is ~/.codex unless you set it. For a key, the file holds two fields:
{
"auth_mode": "apikey",
"OPENAI_API_KEY": "sk-proj-..."
}CopiedThe field is called OPENAI_API_KEY, but it is an entry in the file, not the environment variable. The key is in plain text, and on Linux and macOS Codex writes the file readable by its owner only (mode 0600). OpenAI’s advice for the file applies as written: “Don’t commit it, paste it into tickets, or share it in chat.” On a desktop you can keep it in the operating system’s credential store instead, with this line in ~/.codex/config.toml before you save the key:
cli_auth_credentials_store = "keyring" # or "auto": keyring, else the fileCopiedkeyring fails where there is no credential store, which on a headless server is common, and auto falls back to the file. codex logout deletes Codex’s copy and prints “Successfully logged out”. It revokes nothing at OpenAI: only a ChatGPT sign-in’s tokens are revoked on logout, so a key you no longer trust has to be revoked on the API keys page.
CODEX_API_KEY, OPENAI_API_KEY and the saved key
Three things look like “the API key” to Codex, and each part of Codex reads a different subset. In the 0.171.0 source:
| The key is… | Interactive codex | codex exec, codex review | codex login status |
|---|---|---|---|
Saved with codex login --with-api-key | Used | Used | “API key configured” |
In CODEX_API_KEY | Ignored | Used, ahead of any saved sign-in | Ignored |
In OPENAI_API_KEY | Only fills in the key box on the sign-in screen | Ignored | Ignored |
Codex’s login code says of CODEX_API_KEY: “API key via env var takes precedence over any other auth method.” OpenAI’s non-interactive mode page lists where it applies: “codex exec, codex review, the TypeScript SDK, and codex exec-server --remote.” Two consequences catch people. A job that exports only OPENAI_API_KEY sends no credentials and fails with 401 “Missing bearer or basic authentication”. And codex login status reports “Not logged in” in a shell where CODEX_API_KEY is set and nothing is saved, even though codex exec would run there.
In CI and on a server
OpenAI calls a key “the right way to authenticate automation”, and its pricing page describes the key option as “Great for automation in shared environments like CI.” How to hand it over depends on where the job runs.
GitHub Actions. OpenAI’s docs send you to the Codex GitHub Action rather than installing the CLI and exporting a key. It installs Codex, starts a Responses API proxy with the key, and by default removes the runner user’s sudo before Codex runs:
- name: Run Codex
uses: openai/codex-action@v1
with:
openai-api-key: ${{ secrets.OPENAI_API_KEY }}
prompt: "Review only the changes in this pull request."CopiedThe proxy narrows the key’s exposure rather than sealing it: of the read-only strategy, the README says the key “still flows through the proxy, so Codex could read it if it can reach process memory.”
Any CI. “Do not set OPENAI_API_KEY or CODEX_API_KEY as a job-level environment variable in workflows that check out or run repository-controlled code,” since build scripts, tests and dependency hooks in the same job can read it. Set CODEX_API_KEY on the one command that needs it:
CODEX_API_KEY=<api-key> codex exec --json "triage open bug reports"CopiedA server with cron or a timer. codex exec “reuses saved CLI authentication by default,” so the simplest setup saves the key once as the account the job runs under:
# once, as the user the job runs as
printenv OPENAI_API_KEY | codex login --with-api-key
# crontab -e
0 6 * * * cd $HOME/app && $HOME/.local/bin/codex exec "summarise yesterday's commits" >> $HOME/codex.log 2>&1CopiedIf you sign in to the same account with ChatGPT for your own sessions, leave that saved and give only the job a key, read from a file that only you can read:
chmod 600 $HOME/.config/codex-api-key
# crontab -e
0 6 * * * cd $HOME/app && CODEX_API_KEY="$(cat $HOME/.config/codex-api-key)" $HOME/.local/bin/codex exec "..." >> $HOME/codex.log 2>&1CopiedInstalling Codex on the server, its sandbox and the PATH that cron does not have are in Codex CLI on a Linux server. Running a job on a ChatGPT plan instead means carrying auth.json between runs, one copy per machine, which the refresh-token guide explains.
What a key costs
Every request is billed to the Platform organization that owns the key, at the per-token rates on OpenAI’s API pricing page; “API-key sessions use separate API pricing, not your ChatGPT allowance.” Standard speed, per million tokens, requests up to 272K input tokens, as listed on November 23, 2026:
| Model | Input | Cached input | Cache write (30 min) | Output |
|---|---|---|---|---|
gpt-6.2-sol (Codex’s default) | $2.00 | $0.10 | $2.50 | $10.00 |
gpt-6.2-astra | $8.00 | $0.40 | $10.00 | $40.00 |
gpt-6.1-sol | $2.00 | $0.10 | $2.50 | $10.00 |
gpt-6.1-terra | $1.00 | $0.05 | $1.25 | $5.00 |
gpt-6-astra | $10.00 | $1.00 | $12.50 | $50.00 |
gpt-6-sol | $2.00 | $0.20 | $2.50 | $10.00 |
gpt-6-luna | $0.10 | $0.01 | $0.125 | $0.50 |
gpt-5.5 | $5.00 | $0.50 | – | $30.00 |
GPT-6.2 Sol became the default selection in Codex 0.171.0; other models go in -m or model in the config, as in the Codex models guide. GPT-5.5 left ChatGPT sign-ins on October 14, 2026, but “remains available through the OpenAI API and in Codex sessions authenticated with an API key.” As arithmetic: a GPT-6.2 Sol turn that sends 150,000 input tokens, 140,000 of them cached, and gets 4,000 back costs $0.014 for the cached tokens, $0.04 for the output, and $0.02 to $0.025 for the 10,000 new ones, since the API bills tokens it writes to its prompt cache at the cache-write rate ($2.50 per million on GPT-6.2 Sol) in place of the input rate: about 7 to 8 cents. A codex exec --json run reports input_tokens, cached_input_tokens, cache_write_input_tokens and output_tokens in its turn.completed event, counted in the 0.171.0 source as the session’s running total rather than that turn alone, and the Platform’s Usage dashboard shows what was actually billed.
Things that move the bill:
- Speed.
/fastcosts twice the Standard rate./ultrafastcosts 3 times Standard on the GPT-6.2 models through February 28, 2027 and 6 times from March 1, 2027, and 6 times on GPT-6 Astra and GPT-6.1 Sol. On a key you pay these token rates; the speed page says ChatGPT’s credit multipliers “apply only when you sign in with ChatGPT.” - Long context. For a request with more than 272K input tokens, “input and cache rates double and output costs 1.5 times the short-context rate for the full request.” Codex gives GPT-6.2 Sol a 500,000-token window by default (GPT-6.2 Astra 272,000), and in the 0.171.0 source it compacts the history only at 90% of the window, about 450,000 tokens on Sol, so a long Sol session passes that line well before it compacts.
model_auto_compact_token_limitin the config sets a lower compaction point. - Prepaid credit. The organization pays from prepaid credits; when they run out the API refuses with
credit_balance_exhausteduntil you add more in billing settings or turn on Auto Recharge. - A spend limit you set. Give Codex’s key a project of its own with a spend limit, and a runaway job stops once the project reaches its monthly limit, with
project_spend_limit_exceeded. - Not your plan’s API credits. Pro and Business subscribers can claim monthly API credits, but OpenAI’s credits page says “Codex usage through API keys is not supported by this benefit.”
The data terms change with the sign-in too: “With an API key, usage follows your API organization’s retention and data-sharing settings,” and OpenAI’s feature table marks “No training on API or business data by default” for API keys, Business and Enterprise, but not for Plus or Pro.
What works with a key, and what needs ChatGPT
Per the feature table on OpenAI’s Codex pricing page (read November 23, 2026), a key runs Codex’s local work:
- the CLI, the IDE extension, the desktop app’s local chats,
codex execand the SDK; - GPT-6.2 Astra and Sol, GPT-6.1 Sol and Terra, and GPT-6 Sol and Luna, as far as “the API models available to your key” allow;
- Fast and Ultrafast, web search, image generation (at API prices), MCP servers, skills, subagents,
/review, scheduled tasks and SSH remote connections.
Codex cloud, by OpenAI’s auth page, “requires signing in with ChatGPT,” and so does everything the table ties to your ChatGPT account: cloud chats and environments, ChatGPT Work on the web, GitHub code review and automatic PR reviews, the Slack and Linear integrations, the phone remote control, connectors, Sites, voice dictation and ChatGPT for Excel. Plugins are limited on a key: “Some first party plugins are not available.” For automation that needs a ChatGPT workspace’s entitlements, a ChatGPT Enterprise admin can let members create Codex access tokens instead, which sign in with codex login --with-access-token.
Switching between ChatGPT and a key
Codex keeps one saved sign-in per CODEX_HOME. Saving a key replaces a saved ChatGPT sign-in. Going back, codex login (browser) and codex login --device-auth (a server: it needs one ChatGPT setting) clear whatever is saved before they start, so a ChatGPT sign-in you abandon halfway leaves Codex signed out, key gone. The usual arrangements:
- ChatGPT for you, a key for jobs. Stay signed in with ChatGPT and put
CODEX_API_KEYon scheduledcodex execruns, as above. It is also the way out of “You’ve hit your usage limit” for a job that has to run before the reset, since a key’s usage never touches the plan’s allowance. - Two saved sign-ins. Give the key its own Codex home. Codex refuses a
CODEX_HOMEthat does not exist, so create it first; it gets its ownconfig.tomlas well:
mkdir -p ~/.codex-api
printenv OPENAI_API_KEY | CODEX_HOME=~/.codex-api codex login --with-api-key
CODEX_HOME=~/.codex-api codex # this session bills the keyCopied- Key only, enforced. Add
forced_login_method = "api"to config.toml on a machine that must never draw on a plan. ChatGPT sign-in then stops with “ChatGPT login is disabled. Use API key login instead.”, and if the credentials in use don’t match, “Codex logs the user out and exits.” The reverse setting,"chatgpt", is what makes a key fail with “API key login is disabled”; a workspace admin can also set it for you.
Errors from a wrong or unfunded key
A saved key fails on the first request, not at login. For an HTTP error it does not recognise, Codex prints unexpected status, the status and OpenAI’s message, then the URL and a request id. The causes are from OpenAI’s API error codes:
| What you see | What it means | Fix |
|---|---|---|
unexpected status 401 Unauthorized: Incorrect API key provided… | Codex sent a key and OpenAI does not recognise it: a typo, a key from another organization or project, or a deleted or deactivated key. | Copy the key again, or create a new one, and save it again. |
401 “Invalid Authentication” | A revoked key, a key that doesn’t belong to the organization or project, or one without “the required permissions for the endpoint you are calling.” | Check that the key is current and belongs to the organization and project you mean; if it is restricted, give it access to the Responses API, which Codex calls. |
“Quota exceeded. Check your plan and billing details.” | A 429 for money, not speed. Codex prints this one line for five API codes: insufficient_quota, credit_balance_exhausted, organization_spend_limit_exceeded, project_spend_limit_exceeded and organization_usage_limit_exceeded. | Add credits, or raise or remove the organization or project spend limit, on the Platform; organization_usage_limit_exceeded is OpenAI’s own limit, for which you request a higher approved usage limit. A spend limit also lifts when its month resets. Codex does not retry it, and OpenAI says retrying “won’t restore API access.” |
model_not_found, or a model missing from your project | The project can’t use that model: “Meeting a usage-tier threshold doesn’t grant access to every model.” | List the models the same key can call, then pick one with -m. |
403 “Country, region, or territory not supported” | The request came from a location the API does not serve, which for a server means where the server is. | Run Codex from a supported location. |
“API key login is disabled. Use ChatGPT login instead.” | forced_login_method = "chatgpt" is set, in your config or by an admin. | Remove it from your config, or ask the admin. |
Two refusals look like key problems and are not. “401 Unauthorized: Missing bearer or basic authentication in header” means no key was sent at all, covered in the 401 guide. And “You’ve hit your usage limit” is always a ChatGPT plan’s limit: on a key, running out of money reads “Quota exceeded” instead. How far a plan’s allowance goes before either is worth a key is in which ChatGPT plans include Codex.