Codex CLI /goal Command Explained: AI Auto-Loops Until Your Goal Is Complete

Codex CLI adds /goal command enabling AI to autonomously loop until objectives are achieved
OpenAI's Codex CLI 0.128.0 introduces the /goal command, allowing users to set a goal and have the AI automatically loop through task execution until the goal is complete or the token budget is exhausted. The feature implements the Ralph Loop pattern through prompt templates, includes a token budget mechanism as a safety boundary, and marks the evolution of coding agents from single-turn assistive tools to goal-oriented autonomous agents.
Overview
OpenAI's Codex CLI coding agent tool has released version 0.128.0, introducing a noteworthy new feature — the /goal command. This functionality allows users to set a goal, and Codex will continuously auto-loop through task execution until it evaluates the goal as complete, or the configured token budget is exhausted.
Codex CLI is a terminal-based coding agent tool open-sourced by OpenAI in 2025, implemented in Rust, and positioned as a lightweight entry point for developers to collaborate with large language models on writing code in command-line environments. Unlike the ChatGPT web interface or direct API calls, Codex CLI deeply integrates local filesystem operations and shell command execution capabilities, enabling it to directly read and write files, run tests, and execute build commands within a developer's project directory. It plays the role of a "developer workflow native tool" in OpenAI's product portfolio, complementing the consumer-facing ChatGPT.
This design is essentially OpenAI's own implementation of the Ralph Loop pattern, marking a significant step forward in coding agent autonomy.
Core Mechanism of the /goal Command
From Single-Turn Interaction to Goal-Driven Loops
Traditional AI coding assistants typically operate in a "question and answer" interaction mode: the user states a requirement, the AI generates code, the user reviews the result, then provides feedback. While this pattern is safe and controllable, it's inefficient when handling complex tasks.
The /goal command breaks free from single-turn interaction limitations. Users only need to set a high-level goal, and Codex CLI automatically enters loop mode:
- Execute tasks: Generate and execute code based on the goal
- Self-evaluate: Determine whether the current state has achieved the goal
- Continue or stop: If the goal isn't complete, automatically enter the next iteration; if complete or budget is exhausted, stop
This loop structure shares similarities with the ReAct (Reasoning + Acting) framework widely discussed in AI Agent research. The ReAct framework was proposed by Google and Princeton University in 2022, with the core idea of having language models reason before acting, then decide on next steps based on observations. The /goal command's loop is essentially an engineering implementation of a ReAct loop — in each iteration, the model first reasons about current progress, then decides what operation to perform, and finally evaluates the result.
Prompt Template-Driven Implementation
From a technical implementation perspective, the /goal feature is primarily driven by two prompt templates:
goals/continuation.md: Automatically injected at the end of each round, guiding the model to evaluate current progress and decide whether to continue execution. This is the core mechanism enabling the "auto-loop."goals/budget_limit.md: Handles token budget exhaustion scenarios, ensuring the system can gracefully stop even if the goal isn't complete, preventing infinite resource consumption.
This Prompt Engineering-based implementation approach is quite elegant — it doesn't introduce complex external control logic, but instead leverages the LLM's own understanding and judgment capabilities to drive the entire loop process. This design philosophy can be categorized as an application of the "LLM-as-Judge" paradigm: rather than relying on external rule engines or hard-coded conditional logic to determine whether the loop should continue, the evaluation task itself is delegated to the language model. At the end of each round, the model reads instructions from the continuation.md template, combines them with the current conversation context and executed operation results, and autonomously judges whether the goal has been achieved. The advantage of this architecture lies in its extreme flexibility — whether the goal is "refactor a module" or "fix all tests," the same set of prompt templates can adapt, because the judgment logic is dynamically generated by the model based on semantic understanding rather than pre-defined rules. Of course, this also means evaluation accuracy depends entirely on the model's reasoning capabilities, with the possibility of misjudgment (premature stopping or overlooking incomplete subtasks).
Why the Ralph Loop Pattern Matters
The Ralph Loop pattern implemented by /goal is an important design paradigm in the current AI Agent field. Its core idea is: let AI not just execute single-step operations, but continuously work autonomously around a goal.
The Ralph Loop concept was proposed and named by Australian developer Geoffrey Huntley, inspired by a simple but powerful observation: if you let an AI agent automatically check results after completing each step and decide what to do next, rather than waiting for human confirmation every time, the agent's productivity increases by orders of magnitude. The name itself carries some community cultural flavor ("Ralph" is a personified alias), but the technical thinking behind it is serious — it essentially extends the traditional software engineering concept of REPL (Read-Eval-Print Loop) to the AI Agent level. In a traditional REPL loop, humans drive the loop; in a Ralph Loop, the AI itself becomes the loop driver, with humans stepping back to become goal setters and final reviewers. This role shift is a critical step in AI coding tools evolving from "copilot" to "autopilot."
This pattern has clear value in practical development scenarios:
- Complex refactoring tasks: For example, "migrate a project from JavaScript to TypeScript" — such tasks involve multiple files and steps that are difficult to complete in a single interaction
- Automated bug fixing: Set a goal of "fix all test cases" and let the agent automatically locate issues, modify code, run tests, and verify results
- End-to-end code generation: Set feature requirements as the goal and let the agent automatically complete the full workflow from design to implementation
Token Budget Mechanism: Safety Boundaries for Autonomous Execution
OpenAI included a token budget limit as a safety boundary when designing the /goal feature. This is a pragmatic design decision — an autonomously looping AI agent without resource limits could fall into infinite loops or generate exorbitant API call costs.
The "token budget" here refers to the upper limit of total tokens allowed to be consumed throughout the entire goal execution process. In LLM API calls, tokens are the basic unit for billing and computation — approximately every 4 English characters or 1-2 Chinese characters correspond to one token, and each API call is priced separately for input tokens and output tokens. It's important to note that token budget and model context window limits are two different concepts: the context window limits the maximum tokens processable in a single API call (e.g., GPT-4o's context window is 128K tokens), while the token budget limits cumulative consumption across multiple loop iterations. In the /goal autonomous loop scenario, each iteration generates new API calls, and cumulative consumption may far exceed the size of a single context window. Using current OpenAI pricing as reference, a complex refactoring task involving dozens of iterations could consume millions of tokens, corresponding to API costs ranging from a few dollars to tens of dollars. Therefore, the token budget mechanism serves as both a technical safety measure and a cost control tool.
By configuring a token budget, users can find a balance between autonomy and controllability. When the budget is exhausted, the system stops execution and reports current progress rather than continuing to run indefinitely.
Coding Agents: From Assistive Tools to Autonomous Agents
This update reflects an industry trend where coding agent tools are evolving from "assistive tools" to "autonomous agents." An increasing number of AI coding tools are beginning to support goal-oriented autonomous execution modes, rather than merely responding passively to users' step-by-step instructions.
This trend is particularly evident in the 2025 AI coding tool market. Cognition's Devin first attracted attention with its positioning as an "AI software engineer," emphasizing fully autonomous end-to-end development capabilities; Cursor editor launched Agent Mode (Background Agent), allowing users to start autonomous coding tasks in the background; Anthropic's Claude Code similarly supports autonomous execution in headless mode. While these products differ in implementation details and product form — Devin leans toward fully autonomous development in cloud sandbox environments, Cursor focuses on interactive agent experiences within the IDE, and Claude Code emphasizes deep code understanding in terminal environments — they all point in the same direction: AI coding tools are evolving from the assistive role of "completing code snippets" to agent roles capable of understanding high-level goals and autonomously planning execution paths. Codex CLI's /goal command is OpenAI's clear response in this competitive landscape.
As OpenAI's open-source project (implemented in Rust), Codex CLI's feature evolution also provides the community with a reference implementation for Agentic Engineering. Agentic Engineering is a rapidly emerging practice in the AI engineering field over the past year, with the core focus on: how to design, build, and manage AI agent systems with autonomous decision-making capabilities. It encompasses a series of engineering challenges including agent memory management, tool call orchestration, safety boundary setting, and multi-agent collaboration. Codex CLI's approach of implementing agent behavior through prompt templates rather than hard-coded logic demonstrates a lightweight Agentic Engineering methodology — pushing agent behavioral logic down to the prompt layer as much as possible, maintaining code-level simplicity and maintainability, while leveraging continuous improvements in model capabilities to naturally enhance agent performance. This approach is worth referencing for other developers building their own AI Agent systems.
Key Takeaways
- Codex CLI 0.128.0 adds the /goal command, supporting auto-loop execution until a set goal is complete
- This feature is OpenAI's implementation of the Ralph Loop pattern, driven by two prompt templates: continuation.md and budget_limit.md
- Built-in token budget mechanism serves as a safety boundary, preventing infinite loops and excessive resource consumption
- Marks the evolution of coding agent tools from single-turn interaction to goal-oriented autonomous execution mode
Related articles
TutorialsChatGPT Plus Subscription Guide: Are GPT-5.5, image-2, and Codex Worth the Upgrade?
A detailed look at ChatGPT Plus features — GPT-5.5, image-2, and Codex — with a Plus vs Pro comparison and a complete step-by-step subscription guide for users outside the US.
TutorialsHarness AI Engineering in Practice: Using Claude Code to Master Enterprise-Level E-Commerce Development
Deep dive into Harness AI Engineering: master enterprise e-commerce development with Claude Code using the Rules, Skills, Wiki, and Changes framework.
TutorialsCursor + Codex Dual-IDE Collaboration: A Practical Methodology for Open-Source Project Customization
A complete methodology for open-source project customization based on real-world experience, detailing the Cursor+Codex dual-IDE workflow, seven-stage process, MVP validation, and AI source code reading techniques.