How to Use Codex CLI: Install, Log In and Run It in 5 Steps
Codex CLI is OpenAI's command-line coding agent. You install it with npm, pick a login method, run codex in a project directory, and describe in plain language what you want changed. It reads your code, edits files, and runs commands. Beginners only need to get three things straight: how to install it, how to log in, and how to set the sandbox and approval modes. This guide walks through them in order.
The commands and options here change between versions. Treat the official OpenAI documentation and codex --help as the source of truth.
How to use Codex: the overall flow
- Prepare a Node.js environment and a terminal.
- Install globally with npm.
- Choose a login method: a ChatGPT account, or an API key.
- Enter a project directory and understand the sandbox and approvals.
- Run a first small task in a test repo and check what it changed.
Step 1: How do you install Codex CLI?
First confirm that Node.js and npm work. The required versions are listed in the official docs:
node -v
npm -v
Then install and verify:
npm install -g @openai/codex
codex --version
If npm complains about permissions, don't reach for sudo. Use a version manager such as nvm, or change npm's global directory instead. If codex can't be found after installing, the global bin directory is usually missing from your PATH. Run npm prefix -g to see where global packages go (the bin directory under that path needs to be on your PATH).
Step 2: What is the difference between ChatGPT login and an API key?
Codex supports two common ways to authenticate, and they bill differently:
| Item | ChatGPT account login | API key |
|---|---|---|
| Billing | Uses the allowance included in your subscription; scope depends on the plan | Charged per token to your API account |
| Best for | People who already have a ChatGPT subscription, personal use | Teams, scripts, or anyone who needs exact cost accounting |
| What you need | A working ChatGPT account | An OpenAI API account with a balance |
| Risk | You wait for a reset once the allowance runs out | A leaked key can be abused, so set a spending limit |
For what a subscription covers, see Does ChatGPT Plus include Codex? How to check what your plan covers. For how to choose between the options, see Which plan to use for Codex: ChatGPT subscription, API key, or a compatible gateway.
Log in with a ChatGPT account
Run codex and choose ChatGPT sign-in when prompted. A browser window opens an authorization page, and when you finish you return to the terminal. The exact entry point depends on what your current version shows.
Log in with an API key
Create a key on the OpenAI platform. Use a dedicated project key and set a limit on it. See How to use OpenAI project API keys: per-project permissions and limits and How to set a spending limit and usage alerts for the OpenAI API.
Never commit the key to a repository. The usual approach is an environment variable:
export OPENAI_API_KEY="sk-your-key"
Whether Codex reads this variable directly, or whether you need to pass it to the login command, is something to check in the official docs.
Step 3: What do the sandbox and approval modes mean?
This is the part beginners most often skip and most need to understand. The two concepts are independent:
- The sandbox decides what Codex can technically touch: read-only, write access to the working directory only, or no restrictions at all.
- Approval decides whether it has to ask you before certain kinds of actions.
These are the common combinations (option names on the command line may vary by version, so check them with codex --help):
| Setting | Meaning | Suggested use |
|---|---|---|
| Read-only sandbox + approval required | Can read code, asks before changing anything | First contact, unfamiliar repos |
| Workspace-writable + ask when going out of bounds | Can edit files inside the project; asks before using the network or writing outside the project | A reasonable default for daily development |
| No sandbox + no approval | Does whatever it decides to do | Only for throwaway, isolated environments; not recommended for beginners |
The rule for beginners is simple: start at the strictest level and loosen it once you know how Codex behaves. Before you relax approvals, make sure the project is under git and the working tree is clean, so any change can be undone with one command.
Step 4: What should your first task be?
Don't ask it to refactor your whole project on day one. Create a test repo instead:
mkdir codex-demo && cd codex-demo
git init
printf 'def add(a, b):\n return a - b\n' > calc.py
git add . && git commit -m "init"
codex
In the interactive interface, type:
Read calc.py, find what's wrong with the add function and fix it, then add a simple test.
Watch for three things:
- Whether it reads the file before editing, rather than writing from nothing.
- Whether the approval prompts for each command or file write match the level you chose.
- When it finishes, check the actual changes with
git diffinstead of relying on its summary.
If you're happy with the result, save it with git commit. If not, undo with git checkout ..
Step 5: Where are the config file and advanced settings?
The default configuration lives at ~/.codex/config.toml in your home directory. For how to write the model, provider, and profile fields, see Codex config.toml reference: model_providers, env_key, wire_api and profiles. If you've used a similar tool before, How to use Claude Code: from install to your first code change is a useful comparison.
Common problems and troubleshooting
codexsays command not found: check whether npm's global directory is on your PATH.- Requests fail with 401 or a quota error after login: first confirm the account or key is valid and has enough balance.
- Requests time out or are slow: check cdnping's Codex status page and OpenAI API status to see whether the problem is on the server side, then look at your local network.
- No ChatGPT account: you can use an API key or a custom provider. See Can you use Codex without a ChatGPT account? Connecting with an API key and a custom provider.
What if you don't have an overseas payment method?
Which regions and payment methods OpenAI's official accounts and API support is defined by OpenAI, and you should follow its terms of service and local law. If your main goal is to use several models in tools like Codex and you don't have an overseas credit card (a common obstacle for developers in mainland China), you can look at AGI Plan. AGI Plan is a commercial partner of cdnping.
AGI Plan is a monthly-subscription gateway that aggregates large models from multiple providers, payable in RMB with no overseas credit card needed. Codex doesn't need a ChatGPT account for this; you only change one endpoint URL, and the same allowance can be switched between several capability groups. It is not an officially authorized channel, and some models are marked as available only outside China. See the models page for specifics. For setup steps, see the AGI Plan getting-started page.
Next steps
- Follow steps 1 to 4 above in a test repo and get one run working, keeping approvals at the strict level.
- If you use an API key, set a spending limit right away.
- To point Codex at a custom endpoint, read the config.toml reference article.
- To see how the tools differ, start with How to use Claude Code.