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

  1. Create an API key on the OpenAI Platform and add credit. The OpenAI API key guide walks through it.
  2. Install the CLI: npm install -g @openai/codex, brew install --cask codex, or curl -fsSL https://chatgpt.com/codex/install.sh | sh on macOS and Linux.
  3. Log in with the key: export OPENAI_API_KEY=sk-..., then printenv OPENAI_API_KEY | codex login --with-api-key. Codex caches the login in ~/.codex/auth.json or your system's credential store.
  4. Check it: codex login status shows the active method. codex logout clears it.
  5. Pick a model: codex -m gpt-6.2-sol for one session, or model = "gpt-6.2-sol" in ~/.codex/config.toml as the default. /model switches 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 --oss runs against Ollama or LM Studio. Set the default with oss_provider = "ollama" (or "lmstudio"). Setup for both is in local LLM apps.
  • Azure OpenAI: a [model_providers.azure] table with base_url = "https://YOUR_PROJECT_NAME.openai.azure.com/openai", env_key = "AZURE_OPENAI_API_KEY", query_params = { api-version = "2025-04-01-preview" } and wire_api = "responses".
  • A proxy in front of OpenAI: set openai_base_url instead 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, not OPENAI_API_KEY.
  • Google Cloud Vertex AI: model_provider = "gcp" with project_id and location under [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.
  • /status in 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 as gpt-6.1-terra also help.

Keep the key safe

  • ~/.codex/auth.json is a plain-text file; treat it like a password and never commit it. Set cli_auth_credentials_store = "keyring" to keep the login in your system's credential store instead.
  • In CI, don't set OPENAI_API_KEY or CODEX_API_KEY for 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

  • Cline

    Free, open source · VS Code, JetBrains, CLI · Open source

    Visit

    Open-source AI coding agent for VS Code, JetBrains and the terminal. Bring your own key, endpoint or local model.

    Works with:AnthropicOpenAIGoogle GeminiOllama+1 more

  • Kilo Code

    Free to start, no markup on model usage · VS Code, JetBrains, CLI · Open source

    Visit

    Open-source AI coding agent for VS Code, JetBrains and the CLI. Use 500+ models at zero markup or bring your own keys.

    Works with:AnthropicOpenAIGoogle GeminiOpenRouter+8 more

  • Aider

    Free, open source · CLI · Open source

    Visit

    AI pair programming in your terminal. Works with your own Anthropic, OpenAI, DeepSeek and other model keys.

    Works with:AnthropicOpenAIDeepSeek

  • OpenCode

    Free, open source · CLI, Web · Open source

    Visit

    Open-source AI coding agent for the terminal, desktop and web that works with any model provider or local models.

    Works with:OpenAIAnthropicxAI

  • Zed

    Free editor; paid plans for Zed-hosted AI · macOS, Windows, Linux · Open source

    Visit

    Fast open-source code editor with an AI agent that works with your own Anthropic, OpenAI, Gemini, OpenRouter or local model keys.

    Works with:AnthropicOpenAIGoogle GeminiMistral+5 more

  • goose

    Free, open source · macOS, Windows, Linux · Open source

    Visit

    Open-source AI agent for desktop and CLI that automates engineering tasks with any LLM and MCP extensions.

    Works with:AnthropicOpenAIGoogle GeminiOpenRouter+3 more

  • OpenHands

    Free, open source; hosted and enterprise plans · Web, CLI, Self-hosted · Open source

    Visit

    Open-source platform for AI coding agents with a GUI, CLI and SDK. Use any LLM with your own API key or local models.

    Works with:OpenAIAnthropicGoogle GeminiOpenRouter+6 more

  • Refact

    Free, open source (Refact Cloud discontinued) · VS Code, JetBrains · Open source

    Visit

    Open-source AI coding agent for VS Code and JetBrains that runs a local engine with your own provider keys or local models.

    Works with:OpenAIAnthropicGoogle GeminiDeepSeek+5 more

See all 112 apps that work with OpenAI

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.

Related guides