“`html
The Agent Loop: How AI Coding Assistants Work
AI coding assistants, like those powered by OpenAI’s Codex and Anthropic’s Claude, operate through a core process known as the “agent loop.” This loop orchestrates interactions between the user, the AI model, and the tools the model uses to perform coding tasks. Both OpenAI and Anthropic have open-sourced the CLI clients for their coding models on GitHub, providing clarity into their implementation-a contrast to their closed-source approaches for ChatGPT and the Claude web interface.
Understanding the Agent loop
The agent loop is a repeating cycle. It begins with user input, which is then formulated into a prompt for the AI model. The model generates a response, which can take one of two forms: a final answer for the user or a request to execute a tool. If a tool call is requested,the agent executes the command,appends the output to the original prompt,and resubmits the combined information to the model. This iterative process continues until the model delivers a final response without requesting further tools.
Constructing the Initial Prompt
The initial prompt sent to the AI model, specifically OpenAI’s Responses API in the case of Codex, is carefully constructed from several components, each assigned a priority level: system, developer, user, or assistant. These components include:
- Instructions: These are defined in a user-specified configuration file or through base instructions bundled with the CLI.
- Tools: This section defines the functions the model can access, such as shell commands, planning tools, web search capabilities, and custom tools via the Model Context Protocol (MCP) servers.
- Input: This encompasses sandbox permissions, optional developer instructions, environment context (like the current working directory), and the user’s message.
Tools Available to AI Coding Assistants
The “tools” component is crucial, enabling AI coding assistants to go beyond simply generating code. These tools allow them to
Keep reading