01
Anatomy of an AI Coding Agent — Article 1 of 8

The 10,000-Foot ViewWhat Is Codex CLI?

Dissecting the architecture of a local-first AI coding agent — four modular buildings, one shared core, and the queue that powers everything.

The Very Smart Intern in a Padded Room

Imagine you hire an incredibly talented software engineer. They understand every programming language. They can design systems, write tests, debug issues, and even explain their work. But there's one catch: they're not allowed to directly touch your computer. They work in a completely isolated sandbox. Everything they do is monitored, logged, and can be instantly reverted.

That's Codex CLI.

When you type a natural language prompt—"Add a login form to my Next.js app" or "Fix this memory leak in my Rust code"—Codex CLI springs into action. It reads your files, reasons about your codebase, writes code, runs commands, edits files, tests changes, and iterates until the job is done. All while operating in a tightly controlled sandbox that prevents it from doing anything malicious or destructive.

Codex CLI is open-source. It's built from Rust, Node.js, TypeScript, and Python. Its architecture is modular, elegant, and carefully designed to run a sophisticated AI agent on your machine, without requiring cloud calls for every keystroke. This series will dissect that architecture piece by piece.

What Does Codex CLI Actually Do?

Let's demystify the black box with a concrete example. You run this command:

codex "add a dark mode toggle to my React app"

Behind the scenes, here's what happens:

1. Understand the context
Codex reads your project structure, your package.json, your existing components, and any files you've explicitly shown it.
2. Plan the work
The AI reasons about the changes needed: "I need to update the theme provider, add a toggle button, modify the CSS, and test the feature."
3. Execute the plan
Codex writes code in temporary locations, runs your build system, tests the changes, and checks for errors.
4. Refine and iterate
If tests fail, Codex sees the errors, understands what went wrong, and fixes it. This loop continues until everything works.
5. Show you the results
You see the final changes, can review them, ask for adjustments, and merge them when you're satisfied.
Key Insight

All of this happens locally on your machine. No data is sent to the cloud except the minimum necessary API calls to OpenAI's language models. Your proprietary code never leaves your computer.

Enter: The Four Buildings

Codex CLI isn't a monolith. It's a carefully orchestrated collection of modular components. We think of them as four architectural "buildings," each with a distinct purpose:

01
The Rust Core
codex-rs · 60+ crates
The heart of everything. The engine room of the ship. Core AI orchestration, terminal UI, sandboxing, language tooling, secrets management, API communication, and dozens more.
02
The CLI Launcher
codex-cli · Node.js
A thin shell. Handles platform-specific binary distribution, update checks, shell integration, and config loading. Gets out of the way and lets the core do its thing.
03
The SDK
TypeScript + Python
Where embedability lives. TypeScript SDK for IDE integration, Python SDK for custom agents and tools, and a Python runtime for safe code execution.
04
The MCP Server
shell-tool-mcp · Model Context Protocol
Exposes Codex's capabilities as well-defined tools. How IDEs and other apps talk to the core. A single protocol, multiple clients, one underlying engine.

The Rust Core: Why Rust?

Performance, safety, and control. The core needs to be blazingly fast and memory-safe. Rust forces you to think about resource ownership and thread safety at compile time. No garbage collection pauses, no surprise memory leaks. When you're sandboxing code execution, safety is non-negotiable.

codex-core codex-tui linux-sandbox process-hardening lsp-bridge secrets rmcp-client

The CLI Launcher: Why Not Just Rust?

This thin launcher handles platform-specific binary distribution (macOS arm64, Linux x86_64, etc.), update checks, shell integration, and configuration file loading. It's small, fast, and does one job well: get out of the way and let the core do its thing.

The SDK: Embedability

Want to build a Slack bot that uses Codex? Want to integrate code generation into your own CI/CD pipeline? The SDK abstracts the core's complexity behind clean, language-specific APIs. TypeScript for IDEs, Python for custom agents.

The MCP Server: Universal Protocol

MCP stands for Model Context Protocol—OpenAI's standard for tools and resources. This building exposes Codex's capabilities as a set of well-defined tools. It's how IDEs and other applications talk to the core. A perfect example of architecture that scales.

The Architecture in One Diagram

Here's how these four buildings talk to each other:

MULTIPLE FRONTENDS TUI Client Headless Exec IDE Plugin (MCP) SHARED CORE ENGINE codex-rs: 60+ crates Orchestration & AI Reasoning Sandboxing & Execution File & Memory Management OpenAI API (Language Models) Sandboxed Process (Linux / Seatbelt)

Every frontend—whether it's the interactive terminal UI, a headless invocation, or an IDE plugin—connects to the same battle-tested core. The core orchestrates everything: file I/O, subprocess execution, API calls, and sandbox management.

The Queue That Powers Everything

If the core is the engine, the Submission Queue and Event Queue are the transmission. Here's a simplified mental model:

Submission Queue → Processing → Event Queue
You type a prompt
Parse & plan
Call the AI
Execute command
Return results

Click to replay animation

This queue-based architecture is resilient. If something fails, Codex can see the failure event and decide whether to retry, ask for help, or escalate. If you interrupt a long-running operation, the queue cleanly stops processing new events.

Why This Matters

For Users
Codex CLI is fast, local, and safe. You're not uploading your codebase to a cloud service. You're running a sophisticated AI agent on your own hardware, with full transparency.
For Developers
The architecture is genuinely beautiful. It's modular, testable, and extensible. If you want to understand how modern AI agents work—beyond the hype—this is the case study.
For Open Source
This is a real, production-grade codebase. The decisions made here reflect hard-won lessons from running AI agents at scale. Not a toy or proof-of-concept.

The Puzzle Starts Here

You now have the map. You know the four buildings. But we've left several mysteries unsolved:

?
What is the Protocol? How does the core actually communicate with all those frontends? What's the message format?
?
How does sandboxing actually work? How do you prevent a subprocess from accessing the filesystem? What about Windows vs. Linux vs. macOS?
?
What's inside the AI orchestrator? How does Codex decide what action to take next? How does it handle ambiguity?
?
How do you handle context? Your codebase is huge, but the context window is limited. How does Codex decide what to read?
?
How does the queue work in practice? Let's trace a real submission from start to finish.

These questions are the spine of this series. In Article 2, we'll open up the Protocol layer and see how information flows.

Until Next Time

You're now equipped with the mental model you need. Codex CLI is four modular buildings sharing a common core, with a queue-based event system orchestrating the work. It's a deeply engineered system designed for safety, performance, and clarity.

In the next article, we'll get into the Protocol—the language that binds everything together. We'll see how a prompt becomes a series of discrete steps, and how each step feeds back into the system.

Buckle up. It gets more interesting from here.

Article 1 of 8 Next: The Nervous System →