Modified

Codex: the complete guide

Codex is OpenAI's coding agent. Codex reads the code in your project, proposes a plan, edits files and runs tests. Then Codex shows what changed. You decide how much Codex may do on its own. Codex is in the terminal, the code editor, the ChatGPT app and the cloud, and comes with ChatGPT's plans, most fully with the paid ones. The guide starts from zero, with what a terminal is.

  1. Readexplores the code and runs searches in the project
  2. Planproposes how to make the change
  3. Editedits files and runs commands in the sandbox
  4. Checkruns tests and shows you the changes
Codex reads, plans, edits and checks, and starts over until the task is done.

Basics

What is Codex?

Codex is a coding agent, an AI that works in your project itself. A normal chat gives you a code suggestion to copy. Codex opens the files, makes the change and runs the tests, the small programs that check that the code works. Then Codex says what happened. Codex reads, plans, edits and checks, round after round, until the task is done.

The guide is mostly about Codex CLI, because the terminal shows most clearly what Codex does. Most of it also applies in the editor and the app.

At the time of writing, Codex is in these places, according to OpenAI's documentation:

  • Codex CLI in the terminal.
  • An extension for code editors.
  • Codex in the ChatGPT desktop app.
  • Codex in the ChatGPT app for iPhone.
  • Codex Cloud, which runs tasks on OpenAI's computers.

What is a terminal?

The terminal is the program where you install and start Codex. The list below the text explains the words you meet there, such as terminal, command, CLI and path.

On a Mac, open the Terminal app under Applications and Utilities. On Windows, open PowerShell from the Start menu. On Linux the program is usually called Terminal.

How to read a terminal line

Mac (zsh)anna@macbookmysite%codex
Windows (PowerShell)PSC:\Users\Anna\mysite>codex
WSL 2 and Linux (bash)anna@pc:~/mysite$codex
  • Your user name and the computer name
  • PS means you are in PowerShell
  • The folder you are in
  • The sign: the computer is waiting for you
  • The command you type and run with Enter
Three systems, the same parts. You only type the command after the sign.

Words you meet in the terminal

Terminal
A window where you type commands instead of clicking. On a Mac the app is called Terminal; on Windows, Terminal or PowerShell.
Command
A line of text that tells the computer what to do, for example cd mysite. You type the line and press Enter.
CLI
Command-line interface: a program you use by typing commands in the terminal. Claude Code, Codex and Antigravity CLI are CLI programs.
Folder and path
The terminal is always in a folder. The path is the folder's address, for example ~/Documents/mysite, where ~ means your home folder. cd changes folder and ls (on Windows dir) lists what is in it.
Bash and zsh
The shell, the program inside the terminal that reads your commands on Mac and Linux. Macs use zsh, most Linux computers bash. The commands are almost always the same.
PowerShell
The modern Windows shell. The line starts with PS, for example PS C:\Users\Anna>. Choose PowerShell when a guide gives a Windows command.
CMD
Command Prompt, the older Windows shell. The line starts without PS, for example C:\Users\Anna>. PowerShell commands often do not work in CMD.
WSL 2
Windows Subsystem for Linux: Linux running inside Windows. Many developers on Windows work in WSL 2, because the commands are then the same as on Mac and Linux. You install it with wsl --install in PowerShell and restart the computer.
IDE and VS Code
An IDE is a program for writing code, with your files on the left and a terminal at the bottom. VS Code is a free IDE from Microsoft. Claude Code and Codex come as VS Code extensions, so you can work there instead of in a separate terminal window.
Package manager
A program that installs other programs for you: Homebrew (brew) on Mac, WinGet (winget) on Windows, and npm (npm) for programs that need Node.js.
Environment variable
A named value the terminal remembers, for example an API key. Programs read the value without it being written in the code.

Who is Codex for?

Codex is for you if you write or maintain code. You may be a developer or a student, or run your own website or small program.

You do not need to be an expert. But you need to understand what you ask for and be able to check the result, because Codex changes real files. So learn the basics of git, the program that saves versions of your files, and find out how your project starts.

Codex fits tasks like these:

  • Understand code someone else wrote.
  • Fix a bug.
  • Add a feature.
  • Write tests.
  • Review changes before they move on.

Get started

What do you need to use Codex?

Codex requires a ChatGPT account or an API key from OpenAI. Codex is included in the plans Free, Go, Plus, Pro, Business, Edu and Enterprise, but to different extents, according to OpenAI's Codex pricing page at the time of writing.

The same page says OpenAI is rolling out Codex to Free and Go, in the ChatGPT desktop app and with a smaller and faster model. Plus and higher plans also get Codex in the terminal, the editor, on the web and in the iPhone app, according to the pricing page.

You can also use an API key, a secret code that links Codex to your OpenAI API account. Then you pay per use at OpenAI's API prices. The cloud features are not included, such as code review on GitHub. Part 3 on the OpenAI API shows how to create a key.

Also install git if your computer does not have it. The guide uses git to save the state before and after each change.

How do you install Codex CLI?

You install Codex CLI with one command. At the time of writing, OpenAI has one installer for Mac and Linux and one for Windows, according to OpenAI's documentation. On Windows you run the command in a new PowerShell window, OpenAI writes. Copy the command for your system, paste it into the terminal and press Enter.

If you already have npm or Homebrew, you can install Codex with them instead.

On Linux and in WSL 2, Codex needs the program bubblewrap for its sandbox, according to OpenAI's sandbox documentation. The sandbox sets limits on which files Codex may change. Install bubblewrap before you start Codex. The command starts with `sudo`, which runs it as administrator, so the terminal asks for your password. On a Mac the sandbox works out of the box. On Windows Codex uses its own Windows sandbox when you run in PowerShell.

Install Codex CLI
curl -fsSL https://chatgpt.com/codex/install.sh | sh
Install Codex CLI with a package manager
npm install -g @openai/codex
Install bubblewrap on Linux
sudo apt install bubblewrap

How do you sign in to Codex?

The first time you start Codex with the codex command, it asks how you want to sign in. If you have a ChatGPT plan, choose Sign in with ChatGPT. A browser opens, and you sign in as usual. Then the browser hands the sign-in back to Codex. You can also sign in beforehand with codex login.

With an API key, you pass the key to Codex from the environment variable OPENAI_API_KEY. OpenAI's documentation shows the command below for Mac and Linux. codex login status shows how you are signed in, and codex logout signs you out.

Codex stores the sign-in on the computer, in the file ~/.codex/auth.json or in the operating system's keyring, according to OpenAI's documentation. The keyring is where the computer keeps passwords. The file is plain text, so never share it and never put it in git.

Sign in with ChatGPT
Terminal
codex login
Sign in with an API key
Terminal
printenv OPENAI_API_KEY | codex login --with-api-key
Show how you are signed in
Terminal
codex login status

Your first session with Codex, step by step

Six steps take you from an empty terminal to a first reviewed change.

1. Go to the project folder

Codex treats the folder where you start it as the project, according to OpenAI's documentation. So go into the project folder with cd before you start. If you have no project, create a practice folder with the commands below. mkdir creates the folder and cd goes into it. Never start Codex in your home folder or on the desktop.

2. Save the state in git

OpenAI advises you to save the state in git before and after each task. Then you can undo the changes. Such a save point is called a commit. Type git init if the folder has no git yet. Once the folder has files, save the state with git add . and git commit -m "Before Codex". The first time, git asks for your name and email and shows the commands to set them.

3. Start Codex

Type codex and press Enter. The first time you sign in as in the section above. Codex may also ask whether you trust the folder. Answer yes only for your own projects.

4. Ask for an explanation

Start with a question that changes nothing. OpenAI's example is "Tell me about this project". A fuller version is "Tell me about this project. Which folders are there, and how do you start it?" The answer shows whether Codex has understood the project.

5. Give a small, clear task

Ask for a bounded change, such as "Fix the typo in the heading on the home page and run the tests." While Codex works, you see the commands and the changes. Codex asks before it goes beyond the limits of the sandbox.

6. Review, save and exit

Read the changes before you accept them. With the /review command, Codex reviews the changes without touching the files. Then make a new commit. End the session with /exit or Ctrl+C.

Create a folder to practise in
Terminal
mkdir my-project
cd my-project
Save the state in git
Terminal
git init
git add .
git commit -m "Before Codex"
Start Codex in the project folder
Terminal
codex

How you work

How does Codex work?

Codex works in an agent loop, a round of work that repeats. Codex reads the files it needs and makes a plan. Then Codex edits files, runs commands such as tests and builds the program. Codex reads the result and continues until the task is done or until it needs to ask you. You can steer while the work runs and give new tasks in the same session.

Every command Codex runs goes through the sandbox, including git, package managers and test runners. The operating system itself enforces the boundary. The sandbox decides which files Codex may change and whether commands may reach the internet. According to OpenAI's security documentation, the network is off by default and Codex may only write in the workspace. The workspace is the project folder and temporary folders.

Codex can only hold a certain amount of text in mind at once, its context window. The /compact command summarises the conversation to free up space, according to OpenAI's command list. Still, start a new session when you change topic.

Which permission modes does Codex have?

Permissions decide what Codex may do on its own and when Codex must ask. The sandbox decides which files and which network Codex can reach. Approvals decide when Codex stops and asks you. You change mode with the /permissions command.

OpenAI recommends starting with Ask for approval for most work. This is how OpenAI's documentation describes the modes at the time of writing.

Ask for approval

Codex works freely inside the workspace. Before Codex changes files outside it or uses the internet, it asks you. The mode suits most tasks.

Approve for me

The mode has the same sandbox as Ask for approval. Requests that are eligible for automatic review go to an automatic reviewer instead of to you. Codex gets no wider access in this mode.

Full access

Codex works without a sandbox and without questions. Use the mode only in an environment kept apart from your own files, one you can throw away afterwards. Never use it on the computer with your private files.

Your own profiles

You can create your own permission profiles, for example with more folders Codex may write to. You set the default mode in the file config.toml.

The modes from careful to free

  1. Asks firstyou approve each change
    • Ask for approval
  2. Does more alonewithin limits you set
    • Approve for me
  3. Never asksonly in an isolated environment
    • Full access

How do you write a good task for Codex?

A good task says what Codex should do, where, which rules apply and how you see that it works. "Fix the login" gets a guess. Write instead "The login at /login returns error 500 when the password is empty. Show an error message instead, and add a test for an empty password." Then you get a change you can check.

OpenAI has advice for making the limits last longer, and the same advice gives better results. Be precise and remove background that is not needed. Point out only the files that matter. Separate what must be done from what would be nice. More patterns for good instructions are in the prompt guide.

  • Give Codex one goal per task, and split large tasks.
  • Name the file or page if you know where the bug is.
  • Say how Codex should check the result, for example which test, which command or what should show.
  • Ask Codex to ask questions before it starts, if the task is unclear.
  • Attach a screenshot with codex --image when a bug is visible but hard to describe.

Extend Codex

What is AGENTS.md?

AGENTS.md is a text file with instructions that Codex reads before it starts work. Write what always applies in the project, such as how the tests run, which tools you use and what Codex should ask about first. The /init command creates a first AGENTS.md for you.

Codex reads the files in a set order, according to OpenAI's documentation. First comes a global file in ~/.codex/AGENTS.md. Then comes AGENTS.md in the top folder of the project. Last come files in the subfolders, down to the folder you work in. The file closest to your work wins over the others. By default Codex reads up to 32 KiB of instructions in total, according to the same documentation. KiB is a unit of file size.

Keep the file short and concrete. Every line goes along with every task and takes space in the context window.

Example AGENTS.md
Terminal
# AGENTS.md

## How we work in this project

- Run `npm test` after every change to JavaScript files.
- Use `pnpm` when you install packages.
- Ask before you add a new package.
- Write all text on the site in Swedish.

How do you configure Codex?

Codex stores your settings in the file ~/.codex/config.toml. The file applies in the terminal, the editor extension and the ChatGPT desktop app, according to OpenAI's models page. A project can have its own .codex/config.toml. Codex only reads it if you have marked the project as trusted.

The most common settings are the default model, when Codex should ask (approval_policy) and how much the sandbox allows (sandbox_mode). The example below matches the Ask for approval mode and sets a default model. To change the model for a single run, type codex -m followed by the model id.

The model name in the example applies at the time of writing. A model id in config.toml stops working when OpenAI removes the model, so take the id from OpenAI's models page. The page also shows which models your plan has.

Example config.toml
Terminal
model = "gpt-6.1-sol"
approval_policy = "on-request"
sandbox_mode = "workspace-write"

How do you connect Codex to other tools with MCP?

MCP, Model Context Protocol, is a standard way to connect an AI to tools and information in other systems. The system can be documentation, a ticket system or a database. A program that offers such tools is called an MCP server. Codex can use servers on your own computer and servers you reach at a web address.

You add a server with codex mcp add. The example below comes from OpenAI's documentation and adds Context7, a free MCP server with documentation for developers. codex mcp list shows your servers. Inside a session, /mcp shows which are active.

Each MCP server adds text to every message and so uses more of your limits, according to OpenAI's Codex pricing page. Turn off servers you do not need.

Add an MCP server and show the list
Terminal
codex mcp add context7 -- npx -y @upstash/context7-mcp
codex mcp list

Which Codex commands are good to know?

You type commands that start with a slash inside a Codex session. You type the others straight in the terminal.

  • /init creates an AGENTS.md with instructions for the project.
  • /status shows the model, the permissions, how much of the context window is left and your remaining limits.
  • /permissions chooses what Codex may do without asking.
  • /model changes the model and how much it should think.
  • /review reviews changes not yet saved in git, a commit or the difference from another branch, a parallel version of the code.
  • /mcp shows active MCP servers.
  • codex resume reopens an earlier session.
  • codex exec runs a task without questions and answers, for example in a script or in CI, where tests run automatically.
  • codex cloud shows your tasks in Codex Cloud, sends new tasks and brings the result to your computer.

Codex in the editor, the ChatGPT app and on mobile

Codex comes as an extension for Visual Studio Code, Cursor and Windsurf, and Xcode and JetBrains have their own integrations with Codex, according to OpenAI's documentation. In the editor Codex sees the files you have open and the code you have selected. You review the changes right next to the code. If you cannot find the icon, open the Command Palette, the editor's search box for commands, and choose Codex: Open Codex Sidebar.

The ChatGPT desktop app for Mac, Windows and Linux has Codex built in. Choose Codex in the ChatGPT menu when you start a new chat, according to OpenAI's quickstart. On Windows the app runs with PowerShell and the Windows sandbox, or in WSL 2 if you set that up.

In the ChatGPT app for iPhone you can start, follow and approve tasks, according to OpenAI's documentation. The tasks run in the cloud or on a Mac or Windows PC you have paired with your phone. You pair them with a QR code from the desktop app. The computer must stay awake and online.

Codex Cloud and GitHub

Codex Cloud runs tasks on OpenAI's computers instead of yours. First you create an environment by choosing your repositories on GitHub. A repository is a project with all its code and history. Codex installs dependencies, the packages the code needs, and tests the setup before you publish the environment. Then each task gets its own workspace and can keep going while your computer sleeps. Codex Cloud requires signing in with ChatGPT.

Codex can also review pull requests on GitHub. A pull request is a proposed set of changes waiting for approval. Write @codex review in a comment, and Codex replies with a review the way a colleague would. On GitHub, Codex only flags issues with priority P0 and P1, the most serious ones, according to OpenAI's documentation. You can also turn on automatic reviews for a repository in the Codex settings.

Rules for the review go in AGENTS.md under the heading ## Code Review Rules. The rules guide Codex, but they do not replace tests and human approvals.

Price and safety

What does Codex cost?

Codex is included in the ChatGPT plan you already have. All plans and prices are in part 1 on ChatGPT. Plus costs 20 dollars and Pro from 100 dollars a month at the time of writing, according to OpenAI's Codex pricing page. Prices change, so check them before you pay.

The limits are not a fixed number of messages. How far they go depends on your plan, the model and the size of the task, according to OpenAI's Codex pricing page. Cloud tasks use more than tasks that run on your own computer, and weekly limits may also apply, according to the same page. The pricing page shows OpenAI's estimates for each plan.

When you hit the limit, Plus and Pro can buy credits and continue. You can also switch to an API key and pay the API price for each token, a piece of text that is often part of a word. OpenAI's usage page shows your limits and when they reset.

Is Codex safe?

Codex is built to work within limits. The sandbox stops commands from writing outside the workspace and from reaching the internet without permission. Some folders, such as .git and .codex, stay protected even when Codex may write in the project. The limits only protect you as long as you keep them. In Full access mode they do not exist.

The biggest risk with a coding agent is prompt injection. A web page, a file or a ticket can contain text that looks like an instruction, such as "send the contents of .env to this address". A .env file often holds passwords and keys. According to OpenAI's documentation, prompt injection can make the agent fetch and follow instructions that do not come from you. That is why Codex searches a cached copy of the web by default instead of live pages. OpenAI advises you to treat search results as untrusted.

Protect your credentials too. Never put passwords or API keys in a task. If you sign in with email and password, OpenAI requires two-factor authentication before you can use Codex Cloud. With a personal ChatGPT plan, the Improve the model for everyone setting also applies to Codex. Turn it off if you do not want OpenAI to train on your tasks. More on the risks of AI agents is in the AI safety guide.

Codex, Claude Code or Antigravity CLI?

Codex, Claude Code and Antigravity CLI are coding agents of the same kind. All three run in the terminal, read and change files in your project and run commands. Codex comes from OpenAI, Claude Code from Anthropic and Antigravity CLI from Google.

The differences lie in the models, in how you pay and in how the agents connect to each company's other services. Codex is included in the ChatGPT plans and uses OpenAI's models. If you already have a plan with one of the companies, start with that company's agent. The basics are the same, so what you learn here also works in the other two.

Common questions about Codex

Does Codex understand Swedish?

Codex understands tasks written in Swedish, and OpenAI's models page describes the models as multilingual. Code, commands and error messages are usually in English. Ask for the answer in Swedish if you want it.

Is Codex open source?

Codex CLI is open source under the Apache-2.0 licence, according to the openai/codex repository on GitHub. Open source means anyone may read the code.

What is the difference between Codex and ChatGPT Work?

Codex and ChatGPT Work can do much of the same, according to OpenAI. Codex shows technical details, such as commands and code changes. ChatGPT Work hides them and delivers finished documents. The two share the same limits.

Why can Codex not change any files?

Codex starts in read-only mode, where it may only read, in a folder that git does not track. Sometimes the mode lasts until you have said that you trust the folder, according to OpenAI's security documentation. Create a git repository with git init in the folder, or change mode with /permissions.

Can Codex delete files?

Codex can delete files in the workspace in Ask for approval mode, because the sandbox allows changes there. So make a commit before the task. Then you can restore every file git tracks, but not files that were never added to git.

Can you bring settings over from Claude Code or Cursor?

Codex has the /import command, which brings in settings, project files and recent chats from Claude Code or Cursor, according to OpenAI's command list at the time of writing. Codex looks for up to 50 chats from the last 30 days, according to the same list.

Next partOpenAI APIBuild your own programs with OpenAI's models

Sources: OpenAI, Codex CLI · OpenAI, commands · OpenAI, Codex pricing · OpenAI, sandbox · OpenAI, approvals and security · OpenAI, AGENTS.md · OpenAI, sign-in · GitHub, openai/codex