Give an agent your shell without giving it your machine
3 months ago
Codex CLI is OpenAI's coding agent, and it runs on your own computer, in your own terminal, with your own shell. It is one Rust workspace, and it is big. A hundred and thirty five crates, about one and a half million lines. Most of what I want to show you is about the boundary between the model and your machine.
Ask about this presentation
Chapters
0:00Codex CLI architecture
0:19It runs where your code is
0:35One executable, five programs
1:00One queue in, one queue out
1:21Four front ends, one agent
1:50What the model can reach for
2:17The model writes JavaScript
2:43Three operating systems, three sandboxes
3:11Rules that carry their own tests
3:35Every session is a file
3:57The shape of it
Show transcriptHide transcript
Codex CLI is OpenAI's coding agent, and it runs on your own computer, in your own terminal, with your own shell. It is one Rust workspace, and it is big. A hundred and thirty five crates, about one and a half million lines. Most of what I want to show you is about the boundary between the model and your machine.
You install one executable and start it inside a repository. This is the screen you get. The model it is using, the directory it is pointed at, and underneath, a plan it keeps rewriting as it works. Everything from here is what sits behind that window.
The thing you install is a single executable that can act as five different programs. It checks the name it was launched under and its first argument. Started one way it applies a patch. Started another way it is the Linux sandbox that will wrap the next command, or the helper that runs processes, or the one that touches files. That keeps the helpers exactly in step with the agent that spawns them.
The core talks to the outside world through two queues, and that is all it does. A client sends a submission, which is an identifier and an operation. The core streams back events, each tagged with the identifier of the submission that caused it. The agent loop doesn't know whether a terminal, an editor, or another agent is on the other end of that pipe.
So the front ends stay thin. There is the terminal interface, built on ratatui, and at two hundred and seventy thousand lines it is the largest crate in the whole repository. There is a one shot mode that doesn't draw an interface, which is what scripts and the TypeScript and Python libraries drive. And there are two server modes. In one, Codex registers itself as a tool that another agent can call. In the other it answers remote procedure calls for the editor extensions and the desktop app.
Here is what the model is actually handed. Notice how much of it is about running things and coordinating, rather than editing code. It runs a command in a shell session that stays alive between calls, and types into it. It edits files through one patch format. And it can start other agents, wait on them, and pass them messages. Have a look for yourself, sort it any way you like. The registry is built fresh at the start of every session, so this list moves with your config.
There is a second way to call tools, and it is my favorite part of this codebase. The model writes a JavaScript program, and Codex evaluates it in a fresh V8 isolate. Every tool shows up as a method on a global object called tools, so one program can do ten calls. The isolate is deliberately bare. It doesn't get Node, a file system, network access, or a console. The only way out of that sandbox is through the tools you allowed.
Commands run inside the operating system's own sandbox, and there is a separate implementation for each one. On macOS it is Seatbelt. The profile is compiled into the binary, it opens by denying everything, and it is adapted from Chromium's renderer policy. On Linux it is Landlock and seccomp, with a vendored copy of bubblewrap as the second path. On Windows it is a restricted token, access control lists on the workspace, and firewall rules for the network.
There is a second layer above the sandbox that reads the command itself and decides between allow, prompt, and forbidden. The rules are written in Starlark. Every rule carries the example invocations it must match and the ones it must not, and those get checked when the policy file loads. So a rule that quietly stops matching fails at startup, rather than letting a command slip through months later.
Every conversation is written to disk while it happens, one JSON object per line. A dated folder in your Codex home, with a SQLite index over the top so the picker stays quick. That file is what resume, fork, and archive all read. Fork is the one I would point at. It branches a session you already had, so you can try a second approach and keep the first one intact.
So that is the shape. A small protocol in the middle, thin front ends around it, one binary that becomes its own helpers, and layer after layer of restriction between the model and your machine. It is Apache licensed and all of it is on GitHub, so you can go and read every one of those layers for yourself.