Chapter 4
15 min read
Section 23 of 175

Codex Architecture Overview

How OpenAI Codex Works

Introduction

OpenAI Codex represents a different philosophy from Claude Code. Where Claude Code runs locally on your machine, Codex runs in isolated cloud sandboxes. This chapter explores how Codex works and what we can learn from its architecture.

The Cloud-Native Approach: Codex embraces isolation and parallelism. Each task runs in its own container with full internet access and long-running capabilities. The tradeoff: you work with your code through git, not your local filesystem.

What is Codex

Codex is OpenAI's agentic coding assistant that runs in sandboxed cloud environments. Key characteristics:

  • Cloud execution: Tasks run in isolated containers
  • Git-based: Clones your repos, pushes changes via git
  • Parallel tasks: Multiple codex instances can run simultaneously
  • Internet access: Can browse documentation, search, fetch APIs
  • Long-running: Tasks can run for hours autonomously
  • Powered by o3: Uses OpenAI's reasoning model for complex decisions
πŸ“codex_usage.txt
1# Assign a task to Codex
2$ codex task "Implement user authentication with OAuth2"
3
4# Codex will:
5# 1. Clone your repository
6# 2. Analyze the codebase
7# 3. Research OAuth2 best practices (using internet)
8# 4. Implement the feature
9# 5. Write tests
10# 6. Create a pull request
11
12# Check task status
13$ codex status task-abc123
14
15# View what Codex is doing
16$ codex logs task-abc123

Architecture Overview

πŸ“codex_architecture.txt
1β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
2β”‚                       CODEX PLATFORM                           β”‚
3β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
4β”‚                                                                β”‚
5β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
6β”‚  β”‚   User/API      │───▢│         Task Orchestrator          β”‚ β”‚
7β”‚  β”‚   Interface     β”‚    β”‚   (Queue, Schedule, Monitor)       β”‚ β”‚
8β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
9β”‚                                         β”‚                      β”‚
10β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
11β”‚  β”‚                   SANDBOX MANAGER                         β”‚ β”‚
12β”‚  β”‚  Creates isolated containers for each task                β”‚ β”‚
13β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
14β”‚                                         β”‚                      β”‚
15β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
16β”‚  β”‚              SANDBOX INSTANCE (per task)                  β”‚ β”‚
17β”‚  β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚ β”‚
18β”‚  β”‚  β”‚ Git     β”‚ β”‚ Browser β”‚ β”‚ Shell   β”‚ β”‚ Agent (codex-1) β”‚  β”‚ β”‚
19β”‚  β”‚  β”‚ Client  β”‚ β”‚ Access  β”‚ β”‚ (bash)  β”‚ β”‚ powered by o3   β”‚  β”‚ β”‚
20β”‚  β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚ β”‚
21β”‚  β”‚                                                           β”‚ β”‚
22β”‚  β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”‚ β”‚
23β”‚  β”‚  β”‚              Persistent Workspace                 β”‚    β”‚ β”‚
24β”‚  β”‚  β”‚  (cloned repo, installed deps, state)             β”‚    β”‚ β”‚
25β”‚  β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β”‚ β”‚
26β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
27β”‚                                                                β”‚
28β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Core Components

ComponentPurposeKey Feature
Task OrchestratorManages task lifecycleQueuing, scheduling, status tracking
Sandbox ManagerCreates isolated environmentsContainer provisioning, resource limits
Agent (codex-1)Reasoning and executionPowered by o3 with extended thinking
Persistent WorkspaceTask state and filesSurvives across agent iterations

Cloud-Native Design

Why Cloud-Based?

  • Isolation: Tasks can't affect your local system or each other
  • Parallelism: Run multiple tasks simultaneously
  • Internet: Full access to documentation, APIs, package registries
  • Long-running: Tasks can run for hours without tying up your machine
  • Reproducibility: Clean environment for each task

The Git-Based Workflow

πŸ“git_workflow.txt
1User Request: "Add pagination to the API"
2
31. CLONE
4   └─ git clone user-repo
5   └─ Install dependencies
6   └─ Run existing tests (baseline)
7
82. WORK
9   └─ Agent analyzes codebase
10   └─ Agent makes changes
11   └─ Agent runs tests
12   └─ Agent iterates until passing
13
143. DELIVER
15   └─ git checkout -b feat/pagination
16   └─ git commit -m "Add pagination support"
17   └─ git push origin feat/pagination
18   └─ Create pull request
19
204. CLEANUP
21   └─ Sandbox is archived
22   └─ Logs are preserved
23   └─ Resources are released

Workspace Persistence

Unlike ephemeral functions, Codex workspaces persist across agent iterations:

🐍workspace_concept.py
1# Conceptual workspace management
2class CodexWorkspace:
3    def __init__(self, repo_url: str, task_id: str):
4        self.repo_url = repo_url
5        self.task_id = task_id
6        self.workspace_path = f"/workspaces/{task_id}"
7
8    def setup(self):
9        # Clone repository
10        subprocess.run(["git", "clone", self.repo_url, self.workspace_path])
11
12        # Install dependencies
13        os.chdir(self.workspace_path)
14        if Path("package.json").exists():
15            subprocess.run(["npm", "install"])
16        elif Path("requirements.txt").exists():
17            subprocess.run(["pip", "install", "-r", "requirements.txt"])
18
19    def persist(self):
20        # Workspace survives across agent calls
21        # State, installed packages, modified files all persist
22        pass
23
24    def cleanup(self):
25        # Archive workspace after task completion
26        archive_path = f"/archives/{self.task_id}"
27        shutil.move(self.workspace_path, archive_path)

Codex vs Claude Code

AspectOpenAI CodexClaude Code
ExecutionCloud sandboxLocal machine
File accessCloned repo onlyFull filesystem
InternetFull accessLimited by default
ParallelismMultiple concurrent tasksSequential
DurationHours to daysInteractive session
DeliveryPull requestsDirect file changes
Modelcodex-1 (o3-based)Claude Sonnet/Opus
PrivacyCode sent to cloudStays local

When to Use Each

  • Use Codex: Long-running tasks, research-heavy work, when you want PRs
  • Use Claude Code: Quick changes, local-only code, interactive development
  • Use both: Codex for big features, Claude Code for iteration on PRs

Complementary Tools

Many developers use both: Codex for initial implementation of complex features, Claude Code for quick iterations and fixes once code is local.

Summary

Codex's architecture teaches us:

  1. Cloud-native: Sandbox isolation enables safety and parallelism
  2. Git-based: Code moves through version control, not filesystems
  3. Long-running: Tasks can span hours with persistent workspaces
  4. o3-powered: Reasoning model enables complex decision-making
  5. Different tradeoffs: Cloud vs local, PRs vs direct changes
Next: Let's dive deeper into Codex's sandboxed execution model and how it enables safe, powerful agent behavior.