This article introduces "loop engineering," a new paradigm for designing AI agent systems that autonomously execute complex tasks without continuous human prompting. It delves into the architectural shift from single-shot prompts to persistent, goal-driven loops, highlighting how major AI coding harnesses like OpenAI's Codex, Hermes, and Claude Code have integrated this approach. The discussion focuses on the underlying system design principles, such as state persistence, context management, and sub-agent coordination, to achieve self-correcting and continuously working AI agents.
Read original on The Pragmatic EngineerLoop engineering represents an evolution in interacting with AI agents, moving beyond simple, one-off prompts to designing systems where agents operate autonomously within a defined goal. Instead of a human continuously guiding the agent, the focus shifts to architecting a control loop that manages the agent's execution, state, and progress towards a persistent objective. This approach is particularly relevant in scenarios where tasks are complex, multi-step, and require iterative refinement.
The concept originated with the "Ralph Wiggum" loop, a manual Bash-based system where an agent would continuously process a prompt, generate output, and then restart with updated context if the goal wasn't met. This early method highlighted the need for architectural components to manage:
Major AI harnesses like OpenAI's Codex, Hermes, and Claude Code have abstracted the "Ralph loop" into a `/goal` command, offering a more robust and integrated solution. This feature encapsulates the complexities of managing persistent objectives and agent lifecycles. Architecturally, these systems provide:
Example of /goal in Codex
`/goal Reduce p95 checkout latency below 120 ms on the checkout benchmark while keeping the correctness suite green` This command tells the agent to autonomously work towards the latency goal, implying the system handles task decomposition, code generation, testing, and iteration until the condition is met.
The core of these goal-oriented systems involves several architectural components working in concert: persistent storage for state and logs, a controller for orchestrating agents and sub-agents, execution environments for running tests and code, and mechanisms for lifecycle control (start, stop, pause, budget). This forms a robust framework for managing complex, long-running AI-driven tasks, enabling more sophisticated automation and reducing direct human intervention.