How to Use Codex CLI

Overview

This article summarizes how to access Codex from your local environment via the command line with Codex CLI. It covers installation, sign-in methods, command execution examples, and common commands.

How to Install Codex CLI

If you already have Node.js, you can install it via npm:

1
npm install -g codex-cli

If you use Homebrew, you can also install it as follows:

1
2
brew tap codexcli/tap
brew install codex

After installation, run the following and confirm the version is displayed:

1
codex --version

How to Sign In

Codex CLI requires authentication on the first run. You can sign in with either an API key or your ChatGPT account, depending on your use case.

Sign in with an API key

1
codex login --token <YOUR_API_KEY>

You can also store it in an environment variable to avoid typing it every time:

1
export CODEX_API_KEY=<YOUR_API_KEY>

Sign in with your ChatGPT account

If you prefer the browser-based flow, omit the token and run codex login:

1
codex login

This opens a browser and shows the ChatGPT sign-in screen. Sign in with your email, Google, or Apple account, grant access, and the token will be saved when you return to the CLI.

After signing in, run the following command in either case. If your account information appears, authentication succeeded.

1
codex whoami

How to Run Codex (with examples)

Generate code with a one-off prompt

1
codex run "Write FizzBuzz in Python"

Summarize while attaching a file as context

1
codex run "Summarize this file" --file README.md

Try interactive chat mode

1
codex chat

In chat mode, you can enter prompts to receive responses in sequence. Press Ctrl + D to exit.

Slash commands in chat

In chat mode, type a / prefix to perform helper actions.

CommandPurpose
/helpDisplay available commands
/newReset the conversation history and start a new thread
/file <path>Attach the specified file as context
/exitExit chat

How to Use the Commands

Here are the main commands:

CommandDescription
codex login --token <API_KEY>Sign in with an API key
codex loginStart browser authentication with your ChatGPT account
codex whoamiCheck the currently signed-in account
codex run "<prompt>"Send a one-off prompt and get the result
codex run "<prompt>" --file <path>Run with a file included as context
codex chatUse interactive chat mode
codex historyReview recent execution history

Summary

Once installed and signed in, you can immediately start using Codex from the command line. Knowing the frequently used commands helps you quickly generate scripts or summarize files.