We have just entered the decade of agents, especially coding agents. I think most developers have used coding agents like Copilot, Cursor… and have wondered how they work internally.
We know that in the background, they connect to an LLM to write code, but LLM models have a limited context window and API requests take time. However, the experience with Copilot and Cursor is very smooth.
Additionally, sending a bunch of text to the API costs a lot of money. So, improving what needs to be sent to the API is very important.
These agents are commercial projects, so they are not easy to reverse engineer. Fortunately, OpenAI open-sourced the Codex CLI coding agent last month on GitHub.
It is totally different from the OpenAI Codex Cloud, but it’s a good start to understanding how a coding agent works.
Core component of OpenAI Codex CLI
Basically, it’s a CLI tool that uses an LLM API to work with your code. It’s a React app with a lot of UI components and other logic processing, but I will just focus on the Agent’s components. All the stuff related to how the Agent is built is inside the file: agent-loop.ts
export class AgentLoop {private model: string;
private provider: string;
private instructions?: string;
…