Getting Started with Codex CLI

30 min beginner Lesson 1

Learning Outcomes

  • Launch Codex CLI from your terminal and start an interactive session
  • Explain how Codex CLI differs from ChatGPT and other web-based AI tools
  • Understand the approval flow and how to accept, reject, or modify suggestions
  • Execute a basic task end-to-end using natural language instructions
  • Navigate the Codex CLI interface including status indicators and output panels
NOTE
Tool Version
This lesson covers Codex CLI v0.136+. The tool is actively developed — check github.com/openai/codex for the latest.

Lesson Plan

Segment Duration Topic
Intro 3 min What is Codex CLI, why use a terminal-based AI tool
Explain 5 min Codex CLI vs ChatGPT — key architectural differences
Demo 1 7 min First launch, exploring the interface
Demo 2 6 min Running a simple task with approval flow
Explain 4 min Understanding outputs — diffs, commands, and results
Demo 3 3 min Rejecting and modifying suggestions
Wrap-up 2 min Key takeaways and next lesson preview

Before You Begin

Pre-work:

  • Complete the Installation guide
  • Ensure you're authenticated (ChatGPT sign-in or API key configured)
  • Have a terminal open in a test project directory

Shopping List:

  • Codex CLI installed (codex command available in your terminal)
  • Authentication configured (via codex auth login or CODEX_API_KEY)
  • A test project folder with a few files to work with
  • Basic familiarity with the command line

1 What Is Codex CLI

Codex CLI is OpenAI's open-source command-line coding agent. Unlike ChatGPT (which runs in a browser), Codex CLI operates directly in your terminal with full access to your local file system. It can read your code, write files, and execute shell commands — all under your supervision.

Key characteristics:

Feature ChatGPT Codex CLI
Interface Web browser Terminal
File access Upload only Direct local file system
Code execution Sandboxed remote Local machine (sandboxed)
Context Conversation only Your entire project directory
Workflow Copy-paste code Direct file editing
Open source No Yes (Apache 2.0)

Why use a terminal-based AI tool?

  • Zero context-switching — stay in your terminal workflow
  • Full project awareness — Codex sees your entire codebase
  • Direct execution — changes are applied to your real files
  • Version control integration — works alongside git naturally
  • Scriptable — can be integrated into automated pipelines
  • Multi-provider support — works with OpenAI models and compatible APIs
NOTE
How It Works
Codex CLI sends your prompts and relevant file context to OpenAI's models, receives suggested actions (file edits, shell commands), and presents them for your approval before execution.

2 Launching Your First Session

Let's start Codex CLI for the first time. Open your terminal and navigate to a project directory.

# Navigate to your project
cd ~/projects/my-test-project

# Launch Codex CLI in interactive mode
codex

You should see the Codex CLI interface appear in your terminal with a prompt waiting for your input.

# Navigate to your project
cd ~/projects/my-test-project

# Launch Codex CLI in interactive mode
codex

You should see the Codex CLI interface appear in your terminal with a prompt waiting for your input.

What you see on launch:

The Codex CLI interface displays:

  1. Model indicator — which AI model is being used (default: gpt-5.5)
  2. Working directory — confirms the folder Codex is operating in
  3. Approval policy — shows the current safety level (default: untrusted)
  4. Input prompt — where you type your natural language instructions

Your first interaction:

Type a simple request to see Codex in action:

> List all files in this project and describe the structure

Codex will analyze your directory and provide a summary without making any changes. This is a read-only operation, so no approval is needed.

TIP
Tip
Start with read-only requests (explain, list, describe) to get comfortable with the interface before asking Codex to make changes.

3 The Approval Flow

The approval flow is the core safety mechanism in Codex CLI. When Codex wants to make a change (edit a file, run a command), it shows you exactly what it plans to do and waits for your approval.

The approval cycle:

  1. You give Codex an instruction
  2. Codex proposes an action (file edit or shell command)
  3. You see a preview of the proposed change
  4. You choose: approve, reject, or edit

Try it — ask Codex to create a file:

> Create a file called hello.py that prints "Hello from Codex"

Codex will show you the proposed file content:

╭─ Creating file: hello.py ─────────────────────╮
│                                                │
│  print("Hello from Codex")                     │
│                                                │
╰────────────────────────────────────────────────╯

[a]pprove  [r]eject  [e]dit

Your options:

  • a (approve) — Codex creates the file exactly as shown
  • r (reject) — Codex discards the proposal and you can give new instructions
  • e (edit) — You can modify the proposed content before approving
WARNING
Watch Out
In the default 'untrusted' approval policy, every file write and every shell command requires your explicit approval. Never approve actions you don't understand — ask Codex to explain first.

Understanding the diff view:

For file edits (not new files), Codex shows a diff:

  def greet(name):
-     print(f"Hi {name}")
+     print(f"Hello, {name}! Welcome.")
      return name

Lines with - are being removed. Lines with + are being added. Unchanged lines provide context.


4 Running a Complete Task

Let's run through a complete task to see the full Codex workflow in action. We'll ask Codex to do something slightly more involved.

Task: Create a Python utility function with documentation

> Create a Python file called utils.py with a function that converts
> temperatures between Celsius and Fahrenheit. Include docstrings and
> type hints.

What happens step by step:

  1. Codex analyzes your request and determines it needs to create a file
  2. It generates the content and presents it for approval:
╭─ Creating file: utils.py ──────────────────────╮
│                                                 │
│  def celsius_to_fahrenheit(celsius: float)      │
│          -> float:                              │
│      """Convert Celsius to Fahrenheit.          │
│                                                 │
│      Args:                                      │
│          celsius: Temperature in Celsius.       │
│                                                 │
│      Returns:                                   │
│          Temperature in Fahrenheit.             │
│      """                                        │
│      return (celsius * 9/5) + 32                │
│                                                 │
│  def fahrenheit_to_celsius(fahrenheit: float)   │
│          -> float:                              │
│      """Convert Fahrenheit to Celsius.          │
│                                                 │
│      Args:                                      │
│          fahrenheit: Temperature in Fahrenheit. │
│                                                 │
│      Returns:                                   │
│          Temperature in Celsius.                │
│      """                                        │
│      return (fahrenheit - 32) * 5/9             │
│                                                 │
╰─────────────────────────────────────────────────╯

[a]pprove  [r]eject  [e]dit
  1. Press a to approve
  2. Codex creates the file and confirms: Created utils.py

Following up:

You can continue the conversation with follow-up instructions:

> Now add a main block that demonstrates both functions with example values

Codex will propose an edit to the existing utils.py file, showing the additions in diff format.

TIP
Tip
Codex maintains conversation context within a session. Each follow-up instruction builds on what came before, just like a chat conversation.

5 Essential Commands and Navigation

Beyond natural language prompts, Codex CLI has several built-in commands and keyboard shortcuts for navigation.

Quick command reference:

Command Action
/help Show available commands
/model Display or change the current model
/history View conversation history
/clear Clear the conversation context
/exit or Ctrl+C Exit Codex CLI

One-shot mode (non-interactive):

You can also run Codex without entering interactive mode by passing your prompt directly:

# Run a single task and exit
codex "Explain what this project does based on the README"

# Specify a different model
codex --model gpt-5.4 "Add error handling to main.py"
# Run a single task and exit
codex "Explain what this project does based on the README"

# Specify a different model
codex --model gpt-5.4 "Add error handling to main.py"

Quiet mode for scripting:

# Suppress interactive UI, output result to stdout
codex --quiet "What is the main entry point of this project?"

Useful keyboard shortcuts during a session:

  • Up/Down arrows — Navigate through prompt history
  • Ctrl+C — Cancel current generation or exit
  • Ctrl+L — Clear the screen (keeps session active)
NOTE
How It Works
One-shot mode is useful for quick questions or when integrating Codex into scripts. Interactive mode is better for multi-step tasks where you need back-and-forth conversation.
TIP
Tip
If Codex seems confused about your project, make sure you launched it from the correct directory. Codex uses your current working directory as its project root.

Questions & Answers

Q: Does Codex CLI send my code to OpenAI's servers?
Yes — Codex CLI sends relevant file contents and your prompts to OpenAI's API for processing. Only files relevant to your request are included, not your entire codebase. If you work with sensitive code, review OpenAI's data usage policies and consider which files you reference in prompts.
Q: What's the difference between Codex CLI and GitHub Copilot?
GitHub Copilot works inside your editor as an autocomplete tool — it suggests code as you type. Codex CLI is an agent that operates in your terminal — you give it high-level instructions and it plans and executes multi-step tasks (file creation, editing, running commands). They complement each other: Copilot for line-by-line coding, Codex CLI for larger operations.
Q: Can I use Codex CLI with models other than OpenAI's?
Yes. Codex CLI supports any provider with a Chat Completions or Responses API endpoint, including AWS Bedrock and compatible self-hosted models. Configure additional providers in your config.toml file. See Lesson 8 for full configuration details.
Q: What happens if I accidentally approve something I didn't want?
If you're using version control (git), you can easily revert changes with `git checkout -- filename` or `git restore filename`. This is why it's strongly recommended to work in a git-initialized project. Without git, you'd need to manually undo the changes or rely on file system backups.

Key Takeaways

  1. Terminal-native AI: Codex CLI brings AI assistance directly into your terminal workflow with full local file access
  2. Approval flow: Every destructive action requires your explicit approval — you stay in control
  3. Interactive or one-shot: Use interactive mode for complex tasks, one-shot mode for quick questions
  4. Project-aware: Codex sees your entire working directory and maintains context across a session
  5. Read first, write later: Start with read-only questions to understand the interface before approving changes
  6. Git safety net: Always work in a git repo so you can revert any unwanted changes

Next Steps: In Lesson 2 — Sandboxed Execution, you'll learn how Codex CLI's sandboxing keeps your system safe and explore the different approval policies in depth.