# Claude Code × OpenCode × Codex: a three-way deep comparison

> **Compared**  
> 1. **Claude Code** (Anthropic, closed-source product)—based on this repo’s *Claude Code: how it works (deep dive)* and the `claude-code/` tree  
> 2. **OpenCode** (`anomalyco/opencode`, MIT)—based on `项目分析/opencode-源码分析.md` (commit `62e4641`)  
> 3. **OpenAI Codex CLI** (`openai/codex`, Apache-2.0)—based on `项目分析/openai-codex-源码分析.md` (commit `bb1af23`)  
>
> **Date**: 2026-07-28  
> **Use**: selection + a source-reading route  
> **Footnote**: Xiaomi **MiMo-Code** is a deep fork of OpenCode that also welds a Codex-style GPT tool ABI—it is not a fourth peer product; it is a hybrid specimen of “OpenCode skeleton × Codex tool surface.”

---

## 1. Why one table, not three READMEs

All three READMEs say “a coding agent in the terminal.” The real differences hide in:

- who owns the model and the commercial closed loop  
- which file the loop is written in, and which language constrains the complexity  
- whether permissions are “prompt + software gates” or “an OS sandbox”  
- what the project manual is called, and how it is packed into context  
- whether “open source” means you can change the engine, or only call an API  

Putting them on one table is to avoid “opening Codex with OpenCode expectations” or “looking for Codex’s Grep tool with Claude Code’s tool atlas.”

```mermaid
flowchart TB
  subgraph Same["表面相同"]
    S1["终端 Agent"]
    S2["工具调用循环"]
    S3["项目说明书文件"]
  end
  subgraph Diff["真正分叉"]
    D1["闭源官方 vs 开源社区 vs 开源官方"]
    D2["TS 产品 vs Effect 服务器 vs Rust 机库"]
    D3["软件权限 vs OS 沙箱"]
  end
  Same --> Diff
```

---

## 2. Snapshot table

| Dimension | Claude Code | OpenCode | Codex CLI |
|---|---|---|---|
| **License** | closed-source product | MIT | Apache-2.0 |
| **Language / runtime** | TypeScript product tree | Bun + TS + Effect | Rust workspace (126 crates) |
| **Model binding** | Anthropic strongly bound | multi-provider (models.dev) | OpenAI ecosystem first (Responses API) |
| **Architecture shape** | a single-product agent loop | **the server *is* the body**; TUI is a client | **core + many clients** (TUI/Exec/App-server/MCP) |
| **Agent-loop anchor** | deep-dive Chapter 6 `while` loop | `packages/opencode/src/session/prompt.ts` | `codex-rs/core/src/session/turn.rs` `run_turn` |
| **Tool philosophy** | a finely split full tool family (atlas-grade) | a large toolbox; can switch to the GPT ABI | shell + apply_patch + hosted web_search |
| **Permissions** | multi-layer gates + hooks | Permission ruleset (MiMo also has hardPermission) | `AskForApproval` × `SandboxType` |
| **Sandbox** | product permission model first | rules / optional isolation | **Seatbelt / Landlock / Windows Token** |
| **Project manual** | CLAUDE.md, multi-layer | config + memory, etc. | **AGENTS.md chain** + override |
| **Session persistence** | product session store | storage layer / SQLite, etc. | **rollout** sessions as file assets |
| **Ecosystem** | plugins / skills / MCP (official) | plugins / MCP / skills, community at a sprint | Skills/Plugins/Hooks + app-server |
| **Business** | Anthropic subscription | neutral; can take anyone’s Key | ChatGPT plan / API Key |
| **Typical user** | wants “the best official CC” | wants a kitchen you can black-box or fork | wants official open source + a hard sandbox |

---

## 3. Five fights of idea

### 3.1 Product sovereignty: closed-source peak vs community kitchen vs official open hangar

| | Claude Code | OpenCode | Codex |
|---|---|---|---|
| **The bet** | experience and model vertically integrated | engine replaceable, clients many | official implementation open, sandbox and protocol auditable |
| **Pro** | polished to an extreme; docs and model in sync | freedom, commercially usable MIT, multi-model | readable OS sandbox and turn state machine; Apache-2.0 |
| **Con** | cannot self-host the engine | experience fragments; you pick the model yourself | docs live elsewhere; core is a monolith; binding OpenAI commercially is smoother |

**Who it is for**

- team already in the Anthropic ecosystem → Claude Code  
- want to embed in your own product / multi-model → OpenCode  
- want to audit the sandbox and the Rust implementation / lean OpenAI → Codex  

```mermaid
flowchart LR
  A["要改引擎源码"] --> B{"要多模型中立?"}
  B -->|是| OC["OpenCode"]
  B -->|否·偏 OpenAI| CX["Codex"]
  A2["不要源码·要体验"] --> CC["Claude Code"]
```

---

### 3.2 Permission routes: five gates vs Ruleset vs approval × OS sandbox

| | Claude Code | OpenCode | Codex |
|---|---|---|---|
| **Mechanism** | deep-dive Chapter 7: multi-gate permissions + hooks | permission merge on the Effect path | `AskForApproval` (UnlessTrusted/OnRequest/Granular/Never) + `SandboxType` |
| **Hard capability ceiling** | product policy | rule text (MiMo: hardPermission nails plan to no-write) | **kernel/framework-level sandbox** (seatbelt/landlock/token) |
| **Human in the loop** | an explicit ask flow | ask / deny rules | default can be OnRequest; exec commonly Never |

**Pros and cons**

- CC: semantically rich, teaching-friendly; but you cannot see the full proprietary implementation  
- OpenCode: configurable down to the grain; misconfiguration risk sits on the user  
- Codex: even if the user clicked allow, the OS sandbox may still block—**two walls on different layers**

**Who it is for**: strong compliance / untrusted code execution → look at Codex’s sandbox first; want fine-grained product semantics of “may this command run” → CC/OpenCode rule systems.

---

### 3.3 Tool ABI: full family vs large toolbox vs shell+patch

| | Claude Code | OpenCode | Codex |
|---|---|---|---|
| **Shape** | Read/Grep/Glob/Edit… finely split | the same full-family style; GPT models can switch to a microkernel | Shell, ApplyPatch, ViewImage, WebSearch, MCP, multi-agent… |
| **Patch** | Edit/Write | edit/write or apply_patch | **apply_patch fundamentalism** (its own crate + arg0) |
| **Search** | Grep/Glob tools | Grep/Glob or shell | often lands on shell; plus hosted web_search |

**Pros and cons**

- Full family: the model dreams less; permissions can be tagged per tool  
- shell+patch: aligned with some models’ training distribution, narrower implementation surface, but **shell is too wide** and needs the sandbox as a backstop  

MiMo footnote: on OpenCode, hide read/edit from GPT and expose bash/apply_patch/exec—that is **leaning toward the Codex ABI**.

---

### 3.4 Context philosophy: CLAUDE.md vs Memory/Config vs AGENTS.md+constitution

| | Claude Code | OpenCode | Codex |
|---|---|---|---|
| **Manual** | CLAUDE.md stacked from many sources (deep-dive Chapter 5) | config, plugins, memory files (MiMo: FTS5+checkpoint) | AGENTS.md chained from root→cwd + override |
| **Governance** | collected rules + system-reminder | productized memory/compaction | **six AGENTS.md rules**: do not rewrite history, cache, bounded, ≤10K, >1k P0, Fragment type |
| **Compaction** | product compaction policy | compaction/checkpoint | pre_sampling_compact + remote/inline family |

**Who it is for**: want an “external long-term memory workspace” → look at OpenCode/MiMo; want “historical hygiene + typed injection” → look at Codex; want official CC habits → CLAUDE.md.

---

### 3.5 Architecture shape: single product vs server-as-body vs Rust hangar

| | Claude Code | OpenCode | Codex |
|---|---|---|---|
| **Shape** | an integrated product facing terminal/IDE | HTTP/SSE server is the body; TUI is a client | core library + TUI/Exec/App-server/MCP many frontends |
| **Extension** | official plugins/MCP | multi-client by nature | app-server for IDEs; you can also be an mcp-server yourself |
| **Complexity management** | product engineering | Effect layering | **explicit resistance to core bloat** + module line-count discipline |

```mermaid
flowchart TB
  subgraph CC["Claude Code"]
    C1["产品循环"]
  end
  subgraph OC["OpenCode"]
    O1["Server"] --> O2["TUI/Desktop/Web"]
  end
  subgraph CX["Codex"]
    X1["codex-core"] --> X2["TUI"]
    X1 --> X3["Exec"]
    X1 --> X4["App-server"]
    X1 --> X5["MCP server"]
  end
```

---

## 4. Selection decision diagram

```mermaid
flowchart TB
  S["起点：我要本地编程 Agent"] --> Q1{"必须开源可改引擎?"}
  Q1 -->|否| CC["Claude Code"]
  Q1 -->|是| Q2{"必须多模型中立?"}
  Q2 -->|是| OC["OpenCode"]
  Q2 -->|否| Q3{"更要 OS 级沙箱与官方 OpenAI 闭环?"}
  Q3 -->|是| CX["Codex CLI"]
  Q3 -->|否| Q4{"更要 Effect/TS 生态与社区插件?"}
  Q4 -->|是| OC
  Q4 -->|否| CX
```

**Follow-up questions**

- Already on ChatGPT Team/Enterprise? → Codex’s login path is the shortest  
- Already on a Claude subscription? → Claude Code  
- Building a “host” that drives several CLIs (a design agent)? → look at Open Design first; it treats `claude` / `codex` / `opencode` as engine sockets  

---

## 5. A learner’s source-reading order

### 5.1 If you have only one weekend

1. *Claude Code: how it works (deep dive)* Chapters 4–7 (input, context, loop, permissions)—build the concept dictionary  
2. `opencode` analysis Chapters 2, 6, 7—see the open-source server shape  
3. Codex: `tasks/regular.rs` + the first 350 lines of `turn.rs` + `AskForApproval` + `SandboxType`  

### 5.2 If you are writing your own agent

| The part you want to copy | Where to look |
|---|---|
| Input sorting / slash commands | Claude Code deep dive Chapters 4, 9 |
| Multi-client protocol | OpenCode server / Codex app-server |
| OS sandbox | Codex `sandboxing` + `linux-sandbox` |
| Project-manual loading | Codex `agents_md.rs` or CC CLAUDE.md rules |
| Model-agnostic provider | OpenCode |
| GPT microkernel tool surface | Codex handlers + MiMo `usesGPTToolset` |

### 5.3 If you have to do a “comparison defense”

Prepare three unbeatable sentences:

1. “CC wins on integrated experience, loses on an engine you cannot fork.”  
2. “OpenCode wins on neutrality and clients, loses on you having to be the integrator.”  
3. “Codex wins on official open source + OS sandbox, loses on docs living elsewhere and OpenAI gravity.”  

---

## 6. Honest boundaries

| Boundary | Note |
|---|---|
| Claude Code visibility | this repo’s analysis is based on the available source tree and the deep dive; public product behavior may differ by version |
| OpenCode drift | upstream updates daily; the commit pinned in the analysis document is the authority |
| Codex docs | user docs mostly live on developers.openai.com; repo `docs/` is often a redirect |
| Star counts | they change; the comparison uses the magnitude on the analysis day |
| MiMo | do not put it in a fourth peer column; it is a differential specimen of an OpenCode fork |

---

## 7. Closing

The three are not “who eliminates whom.” They are three legitimate product equations:

- **Claude Code** = model company × closed-source experience  
- **OpenCode** = community × neutral engine × many clients  
- **Codex** = model company × open hangar × OS sandbox  

See the equation clearly, then talk about which layer to copy—prompts, tool ABI, permissions, or the protocol surface.

> Companion preview: `openai-codex-解析.html` · `项目分析/openai-codex-源码分析.md`. Confirm before deciding whether to write it into the catalog.


---

## 8. Scenario scripts (turn the table into a story)

### 8.1 A weekend personal project

You are one person changing a side project, already on Claude Pro:

- **Prefer Claude Code**: shortest path; the tool atlas is teaching-friendly  
- If you want to look at an open implementation to practice: clone Codex, run `doctor`, read the first 350 lines of `run_turn`  

### 8.2 The company wants multi-model and self-hosting

Legal wants MIT; the model has to be swappable to domestic / third-party:

- **Prefer OpenCode**  
- Use Codex as a “sandbox reference implementation” and copy the Seatbelt/Landlock idea, rather than locking OpenAI  

### 8.3 Must run an agent non-interactively in CI

- Codex: `codex exec` + `AskForApproval::Never` + a tight sandbox  
- OpenCode: headless/serve + permission rules  
- Claude Code: look at official non-interactive / CI guidance (product-facing)  

### 8.4 Building a design host (driving several CLIs)

- Look at **Open Design** first: it treats `claude` / `codex` / `opencode` as engines  
- This comparison helps you understand the temperament of the three engines on “the other end of the socket”  

### 8.5 Already using MiMo

You see a GPT ABI on an OpenCode fork—go back to Codex and read `apply_patch` / `shell` / `view_image`, then back to MiMo and read `usesGPTToolset`, and a closed loop forms.

```mermaid
sequenceDiagram
  participant Dev as 你
  participant CC as Claude_Code
  participant OC as OpenCode
  participant CX as Codex
  Dev->>CC: 建立词典（输入/循环/权限）
  Dev->>OC: 看开源服务器形态
  Dev->>CX: 看 OS 沙箱与 turn 工业态
  Dev->>Dev: 决定抄哪一层
```

---

## 9. “Evidence cards” for a comparison defense

**Card A · Open source**  
Codex: `LICENSE` Apache-2.0 · Copyright 2025 OpenAI  
OpenCode: MIT  
Claude Code: closed-source product  

**Card B · Agent loop**  
Codex: `RegularTask` → `run_turn` (`tasks/regular.rs` / `session/turn.rs:153`)  
OpenCode: `session/prompt.ts`  
Claude Code: deep-dive Chapter 6  

**Card C · Sandbox**  
Codex: `SandboxType` four values (`sandboxing/src/manager.rs:35`)  
The other two: product permission model first (details in each analysis)  

**Card D · Manual**  
Codex: `agents_md.rs` chained AGENTS.md  
Claude Code: CLAUDE.md five sources  
OpenCode/MiMo: memory + checkpoint, etc.  

---

## 10. Version pins

| Project | Pinned in this lecture |
|---|---|
| Codex | `bb1af235ea2822d7a40f75ef52e4d6a2cde84da2` |
| OpenCode analysis | commit `62e4641` (see the head of opencode-源码分析.md) |
| Claude Code | the deep dive corresponding to this repo’s `claude-code/` analysis snapshot |

After upstream moves, re-check line numbers against the commit at the head of the lecture.


---

## 11. Point-by-point (deepened): the same user story, three endings

### 11.1 “Delete the build directory”

| Stage | Claude Code | OpenCode | Codex |
|---|---|---|---|
| Tool shape | Bash tool + permission gates | bash + permission ruleset | shell + ExecApprovalRequirement + SandboxType |
| Human review | multi-layer ask | ask/deny rules | OnRequest/UnlessTrusted/… |
| Capability ceiling | product policy | rules | **the OS sandbox may still block** |
| Teaching-friendly | high (tool atlas) | medium | medium (you have to understand two walls) |

### 11.2 “A long session changing a week of requirements”

| | CC | OpenCode/MiMo | Codex |
|---|---|---|---|
| Memory | product memory/compaction | checkpoint + FTS (MiMo) | historical hygiene + compact family + rollout |
| Risk | closed source, hard to audit | fork drift | turn monolith hard to read but auditable |

### 11.3 “Embed it in your own IDE”

| | CC | OpenCode | Codex |
|---|---|---|---|
| Path | official extension | server SDK, many clients | app-server protocol cluster |
| Autonomy | low | high | medium-high (open protocol + official gravity) |

### 11.4 “Must be multi-model”

Only OpenCode (and forks) treat multi-provider as first-class; CC/Codex each anchor their own model ecosystem (Codex has crates such as ollama, but the main narrative is still OpenAI).

```mermaid
flowchart TB
  S["用户故事"] --> T1["删目录"]
  S --> T2["长会话"]
  S --> T3["嵌 IDE"]
  S --> T4["多模型"]
  T1 --> X1["看权限×沙箱"]
  T2 --> X2["看压缩×记忆"]
  T3 --> X3["看协议面"]
  T4 --> X4["几乎只能 OpenCode"]
```

---

## 12. A “parts list” when you copy code

| Part | Copy whom first | Watch |
|---|---|---|
| Input sorting / slash | Claude Code deep dive | concept dictionary |
| OS sandbox | Codex sandboxing/linux-sandbox | large platform differences |
| Multi-key approval cache | Codex with_cached_approval | fits multi-file patches |
| Multi-provider | OpenCode | Effect learning curve |
| Project-manual loading | Codex agents_md.rs or CC CLAUDE.md | the filename is already an industry standard |
| Stop / quality gate | Codex stop hooks or OD Jury | different product goals |
| GPT tool ABI | Codex handlers + MiMo usesGPTToolset | coupled to the model distribution |

---

## 13. Deepened conclusions (pasteable into a review)

1. **The three solve the same problem under different constraints**: experience integration (CC), neutral embeddability (OpenCode), an auditable hangar + a hard sandbox (Codex).  
2. **Permissions cannot be compared only on “is there an ask”**: Codex makes ask orthogonal to the OS sandbox; copying the wrong layer is running naked.  
3. **The agent loop cannot be found by searching only for `while`**: Codex’s halt is follow-up × compact × stop hook; OpenCode/MiMo also have Goal/doom; CC has a complete set of termination reasons.  
4. **Project-manual filenames are converging**: CLAUDE.md / AGENTS.md / memory—the load algorithm matters more than the filename.  
5. **MiMo is a hybrid footnote**: OpenCode skeleton × Codex-style ABI, not a fourth sovereign peer product.

---

*Factual basis: *[Claude Code: how it works · deep dive](../Claude-Code-工作原理科普-深入版.md)* · *[opencode source analysis](opencode-源码分析.md)* (commit `62e4641`) · *[openai-codex source analysis](openai-codex-源码分析.md)* (commit `bb1af23`). All three source drafts have been checked file-by-file and marked `file:line`.*

**Companions** → [Codex interactive analysis station](../openai-codex-解析.html) · [catalog](../总目录.md)
