GuidesGetting started

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.

November 23, 2026The Everpod team
The short answer

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

Any 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.
Copied

The 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):

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-..."
}
Copied

The 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 file
Copied

keyring 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 codexcodex exec, codex reviewcodex login status
Saved with codex login --with-api-key
UsedUsed“API key configured”
In CODEX_API_KEY
IgnoredUsed, ahead of any saved sign-inIgnored
In OPENAI_API_KEY
Only fills in the key box on the sign-in screenIgnoredIgnored

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."
Copied

The 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"
Copied

A 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>&1
Copied

If 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>&1
Copied

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

ModelInputCached inputCache 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:

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:

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:

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 key
Copied

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 seeWhat it meansFix
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.

Run Codex on an always-on developer pod.

A developer pod is a permanent cloud computer (VPS), set up and secured for you, with your pick of Claude Code, Codex, OpenCode, Pi, Hermes and OpenClaw installed. Run more agents at once than your laptop can hold, and keep them working when it’s closed. From $24 a month, built in about ten minutes.