By Michael Bolin, Member of the Technical Staff
Overview
This article explains the core agent loop architecture behind OpenAI's Codex CLI, detailing how it orchestrates interactions between users, LLM models, and tools to perform software engineering tasks. It covers prompt construction, the Responses API integration, tool calling mechanics, context window management, prompt caching optimization, and conversation compaction strategies.
What You'll Learn
How an AI agent loop orchestrates user input, model inference, and tool calls to perform software tasks
How to structure prompts for the OpenAI Responses API with instructions, tools, and input fields
Why prompt caching requires exact prefix matching and how to preserve cache hits across conversation turns
How context window compaction works to prevent token exhaustion in long agent conversations
How Codex CLI supports multiple Responses API endpoints including local, cloud, and custom providers
Prerequisites & Requirements
- Basic understanding of LLMs, tokens, and how language models generate text
- Familiarity with REST APIs and JSON payloads
- Understanding of HTTP request/response patterns and Server-Sent Events (SSE)(optional)
- Familiarity with OpenAI's Responses API or similar LLM APIs(optional)
- Experience building or using AI-powered developer tools or coding agents(optional)
Key Questions Answered
What is an agent loop and how does it work in AI coding assistants?
How does Codex CLI build the initial prompt for the Responses API?
What are the message roles in the OpenAI Responses API prompt and their priority?
How does prompt caching work in the Codex agent loop?
How does Codex handle context window exhaustion in long conversations?
What tools does Codex CLI provide to the model by default?
How does Codex CLI support Zero Data Retention (ZDR) customers?
What causes prompt cache misses in an AI agent loop?
Key Statistics & Figures
Technologies & Tools
Some links below are affiliate links. We may earn a commission if you make a purchase.
Key Actionable Insights
1Structure your prompts with static content first and variable content last to maximize prompt cache hits. Place instructions, tool definitions, and system messages at the beginning, and put user-specific or changing content at the end. This ensures exact prefix matching works for caching.The article demonstrates that prompt caching transforms quadratic sampling costs into linear costs. Codex specifically designs its prompt ordering (system → tools → instructions → developer messages → user context → user message) to maximize cache reuse across inference calls.
2When building an agent loop, ensure new prompts are exact prefix extensions of previous prompts rather than modified versions. Append tool call results and new messages to the end of the input array rather than inserting or modifying items in the middle of the conversation history.Codex explicitly maintains this prefix property across all conversation turns. When configuration changes occur mid-conversation (like working directory changes), Codex appends new messages with updated information rather than modifying the original messages, specifically to preserve cache hits.
3Implement automatic conversation compaction to prevent context window exhaustion in long-running agent sessions. Set a token threshold that triggers compaction before hitting the hard context limit, and use the compacted representation as a drop-in replacement for the full conversation history.Codex evolved from manual /compact commands to automatic compaction using the /responses/compact endpoint when the auto_compact_limit is exceeded. The compacted output includes encrypted_content that preserves the model's latent understanding of the original conversation.
4Keep your Responses API requests fully stateless by including the complete conversation history in each request rather than relying on server-side state via previous_response_id. This simplifies the architecture, improves reliability, and supports Zero Data Retention configurations.While this approach means quadratic JSON payload growth, prompt caching makes the actual model sampling cost linear. The trade-off favors statelessness because network traffic cost is dominated by sampling cost, and stateless requests are simpler to debug and operate.
5When integrating MCP tools into an agent loop, enumerate tools in a consistent order across requests and be cautious about honoring tools/list_changed notifications mid-conversation, as they will cause expensive prompt cache misses.Codex encountered a bug where MCP tools were listed in inconsistent order, causing cache misses. MCP servers can dynamically change their tool list, and honoring these changes mid-conversation invalidates the entire prompt cache for that session.
6Design your agent's tool definitions to be modular, separating built-in tools, API-provided tools, and user-configured tools. Only sandbox the tools you control directly; external tools like MCP servers should enforce their own security guardrails.Codex's sandboxing applies only to its built-in shell tool, not to MCP server tools. This separation of concerns allows flexibility while maintaining security for the tools the agent directly controls.