Part I. The Agent and Its Shape

What Codex is, how its crates fit together, and what happens between a keystroke and a finished change.

1. What Codex Is

Codex is a coding agent: a program that lets a language model read your code, run commands, and edit files on your machine, inside limits you set. The open-source repository holds the whole local agent, written in Rust, and serves it to a terminal UI, a headless mode, editor and desktop clients, and SDKs through one runtime. This book reads that code at one commit and explains how it works.

A chat model answers questions. A coding agent does work. The difference is a loop: the model proposes an action, a program around the model carries it out, and the result goes back to the model, which decides what to do next. Codex is that program. Its own README describes Codex CLI as “a coding agent from OpenAI that runs locally on your computer.”

This chapter sets the scene for the rest of the book. It explains what an agent loop needs beyond a model, where Codex runs, the three words this book uses for its parts (engine, harness, and runtime), how the code base grew to its present shape, and the Rust idioms you will meet when you open the source. Chapters 2 and 3 then give the architecture and one turn end to end.

The picture

Codex is one Rust workspace with five groups of parts: the engine (threads and sessions, the turn loop, the model client, context and compaction), the harness (tools and exec, apply_patch, approvals, OS sandboxes, execpolicy and network control), extension points (MCP servers, skills, plugins and hooks, extensions and agents), the runtime and surfaces (app server, terminal UI, exec and SDKs), and operations (rollouts and state, config and sign-in, telemetry, tests, and release).

The tree is the book’s table of contents seen as a system. The engine on top is the part that talks to the model and keeps the conversation. The harness below it is everything that turns the model’s requests into actions you can trust: the tools, the file-edit format, the approval system, the operating-system sandboxes, and the rules for commands and network access. Extension points let you add tools, instructions, and behavior without changing the engine. The runtime and surfaces serve the agent to people and programs. Operations covers where the agent keeps its state and how the project configures, observes, tests, and ships itself. Each group is one or two Parts of this book.

What an agent needs beyond a model

A model on its own produces text. To act, it needs four things that are not in the model.

Tools. The model needs a vocabulary of actions with exact arguments: run this command, apply this patch, show me this image. Codex declares its tools as JSON schemas and free-form grammars that the model sees with every request (Chapter 9).

A loop. One model response is rarely enough. The agent must run the requested tools, feed the results back, and ask again until the model stops asking for tools. In Codex this loop is run_turn, and a single user request can drive many model requests (Chapter 5).

Memory within limits. The model sees only what fits in its context window. The agent decides what goes in: instructions, project notes from AGENTS.md files, the environment, and the conversation so far. When the conversation outgrows the window, the agent compacts it (Chapters 7 and 8).

Control. An agent that runs commands on your machine must not do whatever the model says. Codex confines commands with the operating system’s sandbox, asks for approval when an action needs more than the sandbox allows, and applies rules to commands and network access (Part IV).

The industry has a word for the machinery around the model: the harness. It appears in the titles of two of OpenAI’s engineering posts from February 2026, “Unlocking the Codex harness: how we built the App Server” and “Harness engineering: leveraging Codex in an agent-first world.” This book uses the word in a narrower sense, defined below.

Where Codex runs

The same agent reaches you through several front ends, which this book calls surfaces. All of them are in the repository or connect to code that is.

Surface How you start it What it is for
Terminal UI codex Interactive work in a terminal
Headless codex exec "task" Scripts and continuous integration (CI); JSON Lines output
App server codex app-server A JSON-RPC server for editor, desktop, and SDK clients
TypeScript SDK @openai/codex-sdk Embedding Codex in Node programs; wraps codex exec
Python SDK openai_codex Embedding Codex in Python; speaks to the app server
Desktop app codex app Opens or installs the desktop app on macOS and Windows
Codex Cloud client codex --cloud Works with hosted cloud threads

You install the command-line program with the standalone installer script, npm (npm install -g @openai/codex), or Homebrew (brew install --cask codex), and you sign in with a ChatGPT account or an API key. Whatever the surface, the agent itself is the same native binary. Chapter 2 shows how the npm package is only a launcher for it.

Engine, harness, runtime

This book divides Codex into three layers, and the words have exact meanings from here on.

The engine is the crate codex-core and its close relatives. It owns threads and sessions, runs the turn loop, builds model requests, manages the context window, and records everything to disk. It knows nothing about terminals or editors. Part II describes it.

The harness is the set of crates the engine calls to act on the machine: the tool router and handlers, command execution, apply_patch, the approval logic, the sandboxes for macOS, Linux, and Windows, execpolicy, and the network proxy. Parts III and IV describe it. In this book the harness is the part that turns model output into safe actions, which is narrower than the industry’s use of the word.

The runtime is the app server, codex-app-server, plus its transports and client library. It hosts threads for clients and speaks one JSON-RPC protocol to all of them, whether it runs inside the same process, as a shared local daemon, or on another host. Part VI describes it, together with the surfaces that use it.

Two smaller groups complete the picture. Extension points (Part V) add tools and behavior: Model Context Protocol (MCP) servers, skills, plugins, hooks, extensions built on a typed API, and agents that spawn other agents. Operations (Part VII) covers persistence, configuration, sign-in, telemetry, testing, and release.

What the repository holds

Most of the code is the Rust workspace under codex-rs/, but the repository holds more than Rust, and some of the rest explains design decisions you will meet later. The table lists the top-level areas.

Path Holds
codex-rs/ The Rust workspace: 175 crates, from the engine to the terminal UI
codex-cli/ The npm launcher that finds and starts the native binary
sdk/ The TypeScript SDK, the Python SDK, and a Python package that pins the binary
codex-rs/vendor/bubblewrap/ A vendored copy of bubblewrap, the Linux sandbox tool
third_party/ Build wiring for OpenSSL, SQLite, V8, voice, and other native dependencies
MODULE.bazel, patches/ The Bazel build and the patches it applies to third-party code
.github/workflows/ 28 continuous integration and release workflows
docs/ Mostly one-line pointers to the online documentation, plus install, contributing, and Contributor License Agreement notes

Two entries deserve a note now. The vendored bubblewrap source means the Linux sandbox does not depend on whatever version a distribution ships (Chapter 14). The V8 wiring exists because code mode runs model-written JavaScript in V8 (Chapter 12). The repository also builds with two systems, Cargo for everyday development and Bazel for hermetic and remote builds, which Chapter 26 compares.

Try it. With Codex installed, codex --help lists the subcommands, codex features list lists the feature flags with their stages and effective state, and codex exec --json "list the files in this directory" runs one headless turn and prints its events as JSON Lines. Run the last one in a scratch repository, because the agent will act.

How the code base got here

Codex is young and moves fast. The table gives dates from the repository’s own history, each found with git log at the pinned commit.

Date Event in the repository
2025-04-16 First commit
2025-04-24 Rust implementation imported as codex-rs
2025-05-07 Rollout recording in the Rust CLI
2025-06-04 Sign in with ChatGPT (codex-rs/login)
2025-08-08 TypeScript implementation removed
2025-09-10 Unified exec (long-lived terminal sessions)
2025-09-30 App server split out as its own subcommand
2026-01-06 First multi-agent controller
2026-01-28 SQLite state database
2026-03-07 Guardian approval reviewer
2026-03-20 Code mode on the V8 JavaScript engine
2026-05-11 Typed extension API under codex-rs/ext
2026-09-05 codex mcp-server removed

The first version was a TypeScript program. Eight days after the first commit, a Rust implementation arrived, and by August 2025 the TypeScript code was gone. The rest of the history is a steady move of features into clearer boundaries: a protocol server in September 2025, persistence in SQLite, approvals reviewed by a model, code execution as a tool mode, and an extension API that moved features out of the engine.

The pace shows in the numbers. A count at the pinned commit finds 14,582 commits: 2,601 dated 2025 and 11,981 dated 2026, from 762 distinct author names. The busiest month was September 2026, with 1,617 commits.

That pace has a consequence for anyone who reads about Codex. OpenAI’s January 23, 2026 post, “Unrolling the Codex agent loop,” described a fully stateless request with base instructions in the instructions field, a tool named shell, and a dedicated compaction endpoint. At the pinned commit, base instructions travel as a developer message, the shell tool is exec_command, compaction happens through a compaction_trigger item in an ordinary model request, and the WebSocket transport sends incremental requests with a previous_response_id. None of that makes the post wrong for its date. It does mean that any description of Codex, this one included, is a description of one commit. That is why every chapter ends with a code map that a script re-checks.

Why Rust, as the code shows it

The repository does not record one rationale for the language choice, so this section describes what the code gets from Rust rather than why the maintainers chose it.

A single static binary. Linux releases target musl, so one executable runs across distributions, and the npm package only has to pick the right one of six platform builds. A program that becomes its own sandbox helper needs to be self-contained, and Chapter 2 shows how Codex re-executes itself under helper names.

Close work with the operating system. The sandboxes build bubblewrap namespaces and seccomp filters on Linux, sandbox-exec profiles on macOS, and restricted tokens and access control lists on Windows. These are system interfaces that Rust reaches directly, with cfg(target_os = ...) attributes choosing the code per platform at compile time.

Concurrency with ownership. The engine runs every thread as an actor on the Tokio runtime, streams model output while tools run in parallel, and pauses tool calls for approvals without blocking threads. Rust’s ownership rules and Send and Sync bounds make data races in that design compile-time errors rather than production incidents. This last point is an inference from the design, not a claim the repository makes.

Reading the code: idioms you will meet

You do not need to be a Rust expert to follow this book, but six idioms carry design weight. The table says where each appears and why it matters.

Idiom Where you meet it Why it matters here
async fn and Tokio tasks The submission loop, turn loop, tools Many things wait at once: the model stream, processes, approvals
Channels (async_channel, mpsc, oneshot) Between clients, sessions, and tools Components talk by message, not by shared locks
Enums with serde tags Op, EventMsg, ResponseItem, protocol messages One type per message family, checked at compile time
Traits as extension points SessionTask, ToolHandler, extension contributors New tasks, tools, and extensions plug in without edits to callers
Arc and CancellationToken Sessions, turns, tool calls Shared ownership and cooperative cancellation down a tree
cfg attributes Sandboxes, platform helpers One code base, three operating systems

One more convention is worth knowing early. Codex gates much of its behavior behind feature flags declared in one file, codex-rs/features/src/lib.rs. Each flag has a stage and a default, so the code can carry unfinished work safely. The status box below counts them, and status boxes throughout the book quote the flags that matter for each chapter.

Status at commit 081295be9d. codex-rs/features/src/lib.rs declares 237 feature specs. By stage: 100 UNDER DEVELOPMENT, 75 STABLE, 52 REMOVED (kept only so old configuration still parses), 6 EXPERIMENTAL on macOS, Linux, and Windows (5 everywhere, plus prevent_idle_sleep, which is experimental only on those three), and 4 DEPRECATED. Of the 237, 81 are on by default. Experimental flags are the ones a user can turn on from the /experimental menu.

What breaks

Symptom Cause Check
codex from npm says the native binary is missing The platform package for your target was not installed Reinstall with your package manager; the launcher prints the exact command
A feature described online is not there The description predates or postdates the commit you run Run codex features list and compare with the flag’s stage
codex exec refuses to run in a directory The directory is not inside a Git repository and --skip-git-repo-check was not given Run inside a Git repository or pass the flag deliberately
An experimental feature cannot be enabled from the menu The flag is UNDER DEVELOPMENT, not EXPERIMENTAL Look up its stage in codex-rs/features/src/lib.rs

Code map

What it is Where Identifier
Product description codex:README.md coding agent from OpenAI
The engine crate codex:codex-rs/core/src/lib.rs codex-core
The command-line entry codex:codex-rs/cli/src/main.rs enum Subcommand
The npm launcher codex:codex-cli/bin/codex.js findCodexExecutable
Feature stages codex:codex-rs/features/src/lib.rs pub enum Stage
Experimental idle-sleep flag codex:codex-rs/features/src/lib.rs prevent_idle_sleep
Headless trust check codex:codex-rs/exec/src/lib.rs --skip-git-repo-check

Recall check

What does a coding agent add to a model?
Tools, a loop that runs them and feeds back results, a managed context window, and control over what actions may run.

What does this book mean by the harness?
The crates that turn model output into safe actions: tools, exec, apply_patch, approvals, sandboxes, execpolicy, and the network proxy.

When did the TypeScript implementation leave the repository?
On August 8, 2025, after the Rust implementation arrived on April 24, 2025.

Why does every chapter pin a commit?
The code changes fast; even OpenAI’s January 2026 description of the agent loop no longer matches the code in four places.

How many feature flags are on by default at the pinned commit?
81 of 237.