Codex API key: use OpenAI Codex with your own API key
Updated · 5 min read
OpenAI's Codex coding agent signs in one of two ways. Sign in with ChatGPT and Codex uses the usage included in your ChatGPT plan. Sign in with an API key and every token is billed to your OpenAI Platform account at standard API prices. The Codex CLI, the IDE extension and Codex in the ChatGPT desktop app support both. To use a key in the CLI, run printenv OPENAI_API_KEY | codex login --with-api-key, then codex login status to check. Codex cloud requires ChatGPT sign-in. (OpenAI's Codex docs moved to learn.chatgpt.com; the old developers.openai.com/codex links redirect there.)
ChatGPT sign-in or API key
| Sign in with ChatGPT | API key | |
|---|---|---|
| Billing | Usage included in your plan, then ChatGPT credits | Per token at API prices, from your OpenAI Platform balance |
| Plans | Free $0 and Go $8 (desktop app, GPT-6 Luna, subject to rollout), Plus $20, Pro $100, $200 or $500 a month, Business $20 per user (billed annually) | No plan |
| Where it works | Desktop app, CLI, IDE extension, web, iOS | Desktop app, CLI, IDE extension, SDK and codex exec |
| Codex cloud, GitHub code review, Slack and Linear integrations, connectors, voice | Included from Plus up | Not available |
| Data settings | Your ChatGPT workspace's settings | Your API organization's retention and data-sharing settings |
| Models | Those your plan includes | Any API model your key can access |
Plan prices are from OpenAI's Codex pricing page on 2026-11-18.
Step by step: Codex CLI with an API key
- Create an API key on the OpenAI Platform and add credit. The OpenAI API key guide walks through it.
- Install the CLI:
npm install -g @openai/codex,brew install --cask codex, orcurl -fsSL https://chatgpt.com/codex/install.sh | shon macOS and Linux. - Log in with the key:
export OPENAI_API_KEY=sk-..., thenprintenv OPENAI_API_KEY | codex login --with-api-key. Codex caches the login in~/.codex/auth.jsonor your system's credential store. - Check it:
codex login statusshows the active method.codex logoutclears it. - Pick a model:
codex -m gpt-6.2-solfor one session, ormodel = "gpt-6.2-sol"in~/.codex/config.tomlas the default./modelswitches inside a session.
In the IDE extension, choose Use API Key on the signed-out screen. In the ChatGPT desktop app, choose Sign in another way, enter the key and select Continue. The CLI and the extension share the same cached login.
For CI, OpenAI calls API keys the right default. Pass the key to one run only: CODEX_API_KEY=<key> codex exec --json "triage open bug reports". On GitHub, OpenAI recommends its Codex GitHub Action, which is designed to reduce exposure of the key.
Models and API prices
OpenAI's API prices per million tokens on 2026-11-18, at standard speed for requests up to 272K input tokens:
| Model | Codex docs suggest it for | Input | Cached input | Output |
|---|---|---|---|---|
gpt-6.2-astra |
The hardest end-to-end work | $8.00 | $0.40 | $40.00 |
gpt-6.2-sol |
Everyday coding | $2.00 | $0.10 | $10.00 |
gpt-6.1-sol |
Used in OpenAI's config examples | $2.00 | $0.10 | $10.00 |
gpt-6.1-terra |
Writing, visual work, computer use | $1.00 | $0.05 | $5.00 |
gpt-6-luna |
Clear, repeatable tasks | $0.10 | $0.01 | $0.50 |
Requests over 272K input tokens cost twice the input price and 1.5 times the output price. Fast mode, which also works with an API key, doubles the per-token price. GPT-5.5 retired from Codex for ChatGPT sign-in on 2026-10-14, but API-key sessions can still use it.
Is an API key cheaper than a ChatGPT plan?
OpenAI doesn't publish an average cost per Codex task for API users, so compare against what the plans include. Plus at $20 a month allows an estimated 17-180 local GPT-6.2 Sol messages per five hours, or 6-50 with GPT-6.2 Astra, and weekly limits may also apply. Pro plans currently have no five-hour limit.
A plan is the predictable choice if you code with Codex most days. An API key fits better when you:
- run Codex in CI or scripts;
- use it occasionally;
- hit your plan's limit and want to keep working on local chats at API rates, which OpenAI explicitly allows;
- need a model your plan no longer offers, such as GPT-5.5.
Other providers in config.toml
Codex reads ~/.codex/config.toml (the folder is CODEX_HOME). Built-in providers are openai, ollama, lmstudio, amazon-bedrock, amazon-bedrock-runtime and gcp. Any other provider gets a table under model_providers, and model_provider selects it:
model_provider = "openrouter"
model = "~openai/gpt-sol-latest"
[model_providers.openrouter]
name = "OpenRouter"
base_url = "https://openrouter.ai/api/v1"
env_key = "OPENROUTER_API_KEY"
The provider must speak OpenAI's Responses API: wire_api = "responses" is the only supported value, and Chat Completions-only endpoints don't work in current releases. OpenRouter's own Codex guide uses an [model_providers.openrouter.auth] block with a command that prints the key instead of env_key, because only then does Codex load OpenRouter's model list. See our OpenRouter API key guide for the key itself.
Other setups from OpenAI's docs:
- Local models:
codex --ossruns against Ollama or LM Studio. Set the default withoss_provider = "ollama"(or"lmstudio"). Setup for both is in local LLM apps. - Azure OpenAI: a
[model_providers.azure]table withbase_url = "https://YOUR_PROJECT_NAME.openai.azure.com/openai",env_key = "AZURE_OPENAI_API_KEY",query_params = { api-version = "2025-04-01-preview" }andwire_api = "responses". - A proxy in front of OpenAI: set
openai_base_urlinstead of defining a new provider. - Amazon Bedrock:
model_provider = "amazon-bedrock"with an AWS profile and region under[model_providers.amazon-bedrock.aws]. It uses a Bedrock API key or AWS credentials, notOPENAI_API_KEY. - Google Cloud Vertex AI:
model_provider = "gcp"withproject_idandlocationunder[model_providers.gcp]. OpenAI models on Google Cloud are in private preview.
Control what you spend
- Spend alerts and hard limits: on the Limits page of the OpenAI Platform, a spend alert emails you, and a hard spend limit makes requests fail with error 429 once tracked spend reaches it. Projects can have their own limits, so a separate project and key for Codex keeps its spend apart.
/statusin the CLI shows the session's model and token usage. The Platform's usage dashboard tracks token usage across billing cycles.- To spend fewer tokens, OpenAI suggests precise prompts, only the files that matter, a small
AGENTS.md, and fewer MCP servers, since each one adds context. Lower reasoning effort and a cheaper model such asgpt-6.1-terraalso help.
Keep the key safe
~/.codex/auth.jsonis a plain-text file; treat it like a password and never commit it. Setcli_auth_credentials_store = "keyring"to keep the login in your system's credential store instead.- In CI, don't set
OPENAI_API_KEYorCODEX_API_KEYfor the whole job if the job runs repository code: build scripts, tests or a compromised action could read it.
Other coding agents that take your OpenAI API key
Other agents
Claude Code is Anthropic's equivalent, and it can also run on an API key instead of a subscription. Cursor takes an OpenAI key for its chat models, and the open-source agents above accept OpenAI keys alongside other providers.
Questions
Can I use Codex with an API key instead of a ChatGPT plan?
Yes. The Codex CLI, the IDE extension and Codex in the ChatGPT desktop app all accept an OpenAI API key, billed per token at API prices. Codex cloud, GitHub code review and the Slack integration need ChatGPT sign-in.
Is Codex free?
The ChatGPT Free and Go plans include Codex in the desktop app with GPT-6 Luna, subject to rollout. The CLI, IDE extension and cloud features are listed from Plus ($20/month). With an API key there is no plan; you pay for the tokens you use.
Does ChatGPT Plus include API credit for Codex?
No. API-key sessions are billed through your OpenAI Platform account at API rates and don't use your ChatGPT allowance.
How do I switch Codex from ChatGPT sign-in to an API key?
Run codex logout, then printenv OPENAI_API_KEY | codex login --with-api-key. codex login status confirms the active method.
Which environment variable does Codex read for the API key?
For interactive use, codex login --with-api-key stores the key, so no variable is needed afterwards. Non-interactive runs (codex exec, codex review, the TypeScript SDK) can take CODEX_API_KEY. Custom providers read the variable you name in env_key.
Can Codex use OpenRouter, Ollama or LM Studio?
Yes. Add a provider under model_providers in ~/.codex/config.toml, or run codex --oss for Ollama or LM Studio. The provider has to support the Responses API; Chat Completions-only endpoints don't work in current Codex releases.
Sources
Checked against the providers' own documentation on 2026-11-18. Prices and limits change; the links below have the current figures.
- learn.chatgpt.com/docs/auth
- learn.chatgpt.com/docs/pricing
- learn.chatgpt.com/docs/models
- learn.chatgpt.com/docs/codex/cli
- learn.chatgpt.com/docs/config-file/config-basic
- learn.chatgpt.com/docs/config-file/config-advanced
- learn.chatgpt.com/docs/config-file/config-reference
- learn.chatgpt.com/docs/config-file/environment-variables
- learn.chatgpt.com/docs/non-interactive-mode
- learn.chatgpt.com/docs/developer-commands
- learn.chatgpt.com/docs/amazon-bedrock
- learn.chatgpt.com/docs/enterprise/connect-to-vertex-ai
- developers.openai.com/api/docs/pricing
- developers.openai.com/api/docs/guides/production-best-practices
- developers.openai.com/api/docs/guides/rate-limits
- openrouter.ai/docs/cookbook/coding-agents/codex-cli