CodexUse logo CodexUse Download

Codex usage basics: commands, examples, pitfalls

The Codex CLI is an AI coding agent that runs in your terminal. This guide covers Codex usage essentials: installation, authentication, and the commands you will actually use.

Installation

Choose your preferred installation method:

# Via npm (requires Node.js 18+)
npm install -g @openai/codex
# Via Homebrew (macOS)
brew install codex
# Verify installation
codex --version

Authentication

The first time you run codex, it opens a browser for ChatGPT authentication. Your credentials are stored locally.

Alternative: API key authentication

If you have an OpenAI API key, you can use that instead of ChatGPT login:

export OPENAI_API_KEY="sk-..."
codex

Basic Codex commands

Interactive mode

Just run codex to start an interactive session:

# Start interactive session
codex
# Start in a specific directory
codex -C /path/to/project
# Start with a specific model
codex --model gpt-4o

Non-interactive (exec mode)

Use codex exec for single tasks, great for scripting:

# Execute a single task and exit
codex exec "Add error handling to main.py"

Codex configuration

Codex configuration controls model selection, sandbox mode, and approval policy. Key settings include:

Understanding sandbox modes

Mode Can read files Can write files Network access
read-only Yes (all) No (asks permission) No
workspace-write Yes (all) Yes (current directory) No
danger-full-access Yes (all) Yes (anywhere) Yes

Common Codex usage patterns

Code explanation

codex
> Explain what this function does

Refactoring

codex
> Refactor the UserService class to use dependency injection

Bug fixing

codex
> The login form submits twice when clicking the button - fix it

Test generation

codex
> Write unit tests for the PaymentProcessor class

Common Codex usage pitfalls

1. Wrong model name

If you specify a model that does not exist, Codex will error. Valid models include gpt-5.1-codex-max, gpt-4o, o3, and o4-mini.

2. Sandbox blocking needed operations

If Codex cannot write files or access the network when you expect it to, check your sandbox_mode setting.

3. Rate limits with no warning

The CLI does not show remaining quota. You only know you are rate-limited when you hit a 429 error. CodexUse can help by monitoring usage across accounts.

4. Single account limitation

The CLI only supports one account at a time. To manage multiple accounts without constant re-authentication, use CodexUse.

How to log out of Codex

To log out of Codex CLI, you can remove the stored auth credentials. With CodexUse, you can simply switch to a different profile instead of fully logging out.

Tips for effective Codex usage

Related

How do I install the Codex CLI?

Use npm install -g @openai/codex or brew install codex on macOS.

What is the difference between interactive and exec mode?

Interactive mode (codex) is for conversation-style work. Exec mode (codex exec) runs a single task and exits.

How do I log out of Codex?

Remove the stored auth credentials, or use CodexUse to switch profiles instead.

Download CodexUse for multi-account support