Cover of The Codex Book: a hand-colored 1828 engraving of a hermit crab in a borrowed shell

The Codex Book

Inside the Engine, Runtime, and Harness of an Open-Source Coding Agent
by Claudex Agent

How the open-source Codex coding agent works inside, as the code stood at commit 081295be9d (December 3, 2026): the Rust engine and its turn loop, the model client and context window, compaction, the tool harness, command execution and apply_patch, approvals and operating-system sandboxes, execpolicy and the network proxy, MCP, skills, plugins, hooks and extensions, multi-agent orchestration, the app server and its clients, persistence, configuration, and the build.

EPUBPDFWordGitHub

For education and learning only. This is an independent, non-commercial study. It is not affiliated with, sponsored by, or endorsed by OpenAI. "OpenAI" and "Codex" are trademarks of OpenAI, used here only to name the project this book describes. The authoritative source is always the openai/codex repository.

Contents

  1. 0. How to Read This Book
  2. Part I. The Agent and Its ShapeWhat Codex is, how its crates fit together, and what happens between a keystroke and a finished change.
    1. 1. What Codex Is
    2. 2. The Architecture at a Glance
    3. 3. One Turn, End to End
  3. Part II. The EngineThe Rust core that owns a conversation: threads, sessions, the turn loop, the model client, the context window, and compaction.
    1. 4. Threads, Sessions, and the Protocol
    2. 5. The Turn Loop
    3. 6. Talking to the Model
    4. 7. Building the Context Window
    5. 8. Compaction and the Token Budget
  4. Part III. The HarnessThe tools the model can call, and the machinery that turns a tool call into a command, an edit, or a result.
    1. 9. The Tool System
    2. 10. Running Commands
    3. 11. Editing Files with apply_patch
    4. 12. Code Mode and the Other Tools
  5. Part IV. SafetyApprovals, sandboxes, command rules, network control, and the reviewer that decides what an agent may do on your machine.
    1. 13. Approvals and Permission Profiles
    2. 14. Operating-System Sandboxes
    3. 15. Execpolicy and Command Safety
    4. 16. The Network Proxy and the Guardian
  6. Part V. Extending CodexMCP servers, skills, plugins, hooks, extensions, and agents that spawn other agents.
    1. 17. MCP: Tools from Other Processes
    2. 18. Skills, Plugins, and Hooks
    3. 19. The Extension API
    4. 20. Many Agents, One Tree
  7. Part VI. The Runtime and Its SurfacesThe app server that hosts threads, and the terminal, headless, and SDK clients that talk to it.
    1. 21. The App Server and Its Protocol
    2. 22. The Terminal UI
    3. 23. Headless Codex: exec and the SDKs
  8. Part VII. Operating CodexWhere a thread is stored, how configuration and sign-in resolve, and how the project observes, tests, and ships itself.
    1. 24. Rollouts, State, and Resume
    2. 25. Configuration, Features, and Sign-In
    3. 26. Observing, Testing, and Shipping
  9. Part VIII. ReferenceThe design patterns that recur across the code base, and a glossary of the terms this book uses.
    1. 27. Design Patterns in Codex
    2. 28. Glossary