The Primitives of Loop Engineering
Loop engineering is the practice of creating autonomous, self-correcting feedback cycles where an AI agent repeatedly acts, tests results, and adjusts until a goal is met. Modern tools like Claude Code provide two primary primitives to manage this:
- /goal: Drives a single bounded task toward a measurable finish line. It uses an evaluator model to check if specific success criteria (e.g., passing tests, meeting performance thresholds) are met, iterating until the goal is achieved or a turn limit is reached.
- /loop: Acts as a scheduler (similar to a cron job) that re-runs a prompt on a fixed interval. It is ideal for monitoring external states, such as checking for new GitHub issues or PR status updates.
Designing Effective Agentic Workflows
Success in loop engineering depends on defining clear, deterministic stopping conditions. Vague objectives like "make the UI look good" are unsuitable for automation because they require human taste and subjective judgment. Instead, use loops for tasks where "done" can be objectively measured, such as clearing a backlog, enforcing contribution guidelines, or optimizing performance metrics.
To build robust systems, follow these best practices:
- Separate Drafting from Verification: Never let the agent that performs the work be the sole judge of its quality. Use a separate agent or a rigorous verification script to test changes against real-world conditions (e.g., checking browser console errors or running performance audits).
- Maintain Human Judgment: While you can delegate tasks, you cannot delegate taste. Always review agent-generated implementations, especially for sensitive areas like security, authentication, or complex product logic.
- Compose Patterns: Combine primitives to handle complex workflows. For instance, use
/loopto check for new bug reports on a schedule, then trigger a/goalto implement and verify a fix automatically.
Operational Realities
When deploying loops, be mindful of their lifecycle. Loops are session-scoped and typically expire after seven days. If a task requires persistence beyond a local session, use /schedule to run the routine in the cloud. A common failure mode is the "spinning loop," where an agent repeatedly attempts the same failed command; if an agent fails to make progress after consecutive attempts, it is time to intervene manually.