Skip to content

Codex Architecture Overview

源码版本rust-v0.145.0

codex is OpenAI's open-source coding agent. This version is the Rust rewrite (the earlier TypeScript version has been demoted to codex-cli/ as an npm install shim). The main source body lives in codex-rs/, a Cargo workspace that pulls in 90+ independent crates, sliced along responsibility lines. This site breaks it down by crate, and every key file carries a SrcLink that points directly at the corresponding GitHub line.

Overall layering

Major crates at a glance

LayercrateResponsibility
CLI entryclisubcommand dispatch, doctor, login, mcp_cmd
Agent corecoreagent main loop, LLM client, compaction, session thread, tools
Patch protocolapply-patchapply_patch text patch format
Sandboxsandboxing / linux-sandbox / bwrap / windows-sandbox-rscross-platform command isolation
Execution policyexecpolicycommand classification and gating policy
Executionexec / exec-servercommand execution and exec-server protocol
TUItuiterminal UI, chatwidget, diff_model
App-serverapp-server / app-server-protocol / app-server-transportorchestration service and protocol
MCPmcp-server / rmcp-client / codex-mcp / connectorsModel Context Protocol integration
Providermodel-provider / model-provider-info / backend-clientmodel provider abstraction
Cloudcloud-tasks / cloud-configcloud task execution
Configconfig / codex-homeconfig system and home directory
Peripheralprompts / skills / memories / hooks / plugin / rolloutprompts, skills, memories, hooks, plugins, trace

One sentence per layer

  • CLI entry: subcommand dispatch for the codex command; main.rs parses with clap and then routes to each subcommand.
  • Agent core: the agent main loop consumes Op, drives LLM calls and tool execution; CodexThread is the entry point for session orchestration.
  • Sandbox execution: commands run inside a sandbox; execpolicy first classifies the command to decide allow / block / approve, then hands off to the platform sandbox for isolation.
  • TUI / App-server: TUI is the terminal frontend; App-server exposes Agent capabilities to IDEs / external clients via a protocol.
  • Provider / Cloud: model-provider abstracts different backends; cloud-tasks ships tasks to run on the cloud.

Design motivation

codex chose Rust for the rewrite; the core motivation is sandbox and performance. A coding agent runs arbitrary shell commands on the user's machine, and without reliable process isolation there is no safety. Rust's type system and zero-cost abstractions let the 90+ crates be sliced at fine granularity without bringing runtime overhead. The workspace splits crates by responsibility rather than one big crate, for incremental compilation and clear boundaries — touching the TUI does not recompile core.

Common misreadings

  • "codex is a CLI tool" — only half right. The CLI is just the entry point; what actually runs is the combination of core + sandbox + tui / app-server.
  • "The Rust rewrite is for speed" — Rust brings sandbox isolation and process-level security; performance is a byproduct.
  • "apply_patch is codex's private format" — it is the text patch protocol codex uses; the model generates it, and the apply-patch crate parses and applies it.
  1. CLI entry and subcommand dispatch — from the codex command in, see how it dispatches
  2. Agent main loop — how the Agent consumes Op to drive the LLM
  3. LLM client and Responses API — how it talks to the OpenAI backend
  4. Cross-platform sandbox — how commands are isolated
  5. execpolicy command classification — how commands are classified and gated
  6. App-server architecture — how it exposes itself to external clients via a protocol

After reading this thread, you can basically explain the full chain of codex from "user types a command" to "the LLM runs tools and edits files".