0. How to Read This Book

This book explains Codex, an open-source coding agent, as three layers that fit together: an engine that runs conversations with a model, a harness that turns the model’s requests into safe actions on your machine, and a runtime that serves both to every client. Every claim was read in the Codex repository at one pinned commit, and every chapter ends with a table of where its claims live in the code.

A coding agent looks simple from the outside. You type a request, the agent reads files, runs commands, edits code, and reports back. Inside, the same request passes through a conversation engine, a model client, a context manager, a tool router, an approval system, an operating-system sandbox, a persistence layer, and a protocol server, each with its own rules. This book is written for the engineer who wants to see all of that from the inside: to extend Codex, to operate it, to debug it, or to build something like it.

The book studies the open-source Codex repository, which holds the Codex command-line interface (CLI), its terminal user interface (TUI), the codex exec automation mode, the app server that desktop and editor clients use, and the software development kits (SDKs). It is an independent study. It is not affiliated with or endorsed by OpenAI, and it describes only what the open-source code shows.

What the book covers

Part I gives the whole picture: what Codex is and where it runs (Chapter 1), the architecture on one map (Chapter 2), and one turn from keystroke to finished change (Chapter 3). Part II opens the engine: threads and sessions, the turn loop, the model client, the context window, and compaction. Part III covers the harness: the tool system, command execution, file editing with apply_patch, and the other tools. Part IV covers safety: approvals, operating-system sandboxes, command rules, the network proxy, and the model-based reviewer. Part V covers extension: MCP servers, skills, plugins, hooks, the extension API, and multi-agent work. Part VI covers the runtime and its clients: the app server, the terminal UI, and headless use. Part VII covers operations: persistence, configuration and sign-in, observability, testing, and release. Part VIII collects the design patterns and a glossary.

The book leaves some things out on purpose. It does not describe how OpenAI’s servers implement the model API; it describes only the requests Codex sends and the responses it parses. It does not cover the Codex cloud product, the desktop application’s own source, or the editor extensions, except where the open-source code shows how they connect. It does not teach Rust, though it explains the Rust idioms that carry design weight.

Reading paths

The book reads front to back, but most readers arrive with a question. The table maps common starting points to chapters.

If you are Read
New to Codex internals Chapters 1 to 3, then Part II
Building your own agent Chapters 2, 3, 5, 7, 8, 9, and 27
Securing or auditing Codex Chapters 3, 9, 10, and Part IV
Writing an MCP server, skill, or plugin Chapters 7, 9, and Part V
Building a client or integration Chapters 2, 4, and Part VI
Operating Codex for a team Chapters 13, 14, 25, and 26

Conventions

Source. Every claim comes from the Codex repository at commit 081295be9d, committed on December 3, 2026. A path such as codex:codex-rs/core/src/session/turn.rs names a file in that repository. Unless a sentence says otherwise, a claim was read in code, not run.

Status labels. Codex gates much of its behavior behind feature flags, and each flag has a stage in the code: STABLE, EXPERIMENTAL, UNDER DEVELOPMENT, DEPRECATED, or REMOVED. Status boxes give the stage and whether the flag is on by default, at the pinned commit. Other labels are IMPLEMENTED (wired into a shipping path), PLATFORM-SPECIFIC (one operating system only), and TEST-ONLY. A label describes the code; it is not a claim about any particular release or account.

Numbers. A number read from code is stated as fact with its source. A number that a document or doc comment states about itself is attributed to that document. A number the book computed, such as a crate count or a worked budget, says so.

Excerpts. Code excerpts are short and faithful to the source, which is Copyright OpenAI and licensed under the Apache License 2.0. A // ... line marks trimmed text.

Terms. A thread is one conversation, persisted on disk. A session is the engine state that serves one thread in memory. A turn is one user input and everything the agent does to answer it. A sampling request is one call to the model inside a turn. An item is a typed unit of the conversation, such as a message, a reasoning summary, a tool call, or a tool output. Chapter 28 defines the rest.

How each chapter is built

Every chapter opens with a box that states its big idea, then a diagram and a walkthrough of it. The teaching sections follow, with code excerpts, worked examples, and formulas where the code computes something. Near the end come a status box where feature flags or platform limits apply, a “What breaks” table of symptoms, causes, and checks, a “Code map” table of where the chapter’s claims live, and five recall questions with one-line answers. Callout boxes marked “In practice,” “Pitfall,” and “Try it” hold practitioner advice.

The code map tables are checked by a tool in this book’s repository: every identifier must still appear in the named file at the pinned commit. Codex changes quickly. When the code moves on, a failing row is the first sign that a statement here has gone stale.

Diagrams

All architecture, sequence, and data-flow diagrams were drawn with the archify tool from JSON sources in diagrams/src/. Each has an interactive HTML version in diagrams/html/. The whole-system diagram from Chapter 2 is also at the repository root as codex-architecture.html.