7 AI Loops to Automate Your Code
Loops let AI agents work autonomously toward defined goals by combining a trigger (manual, scheduled, or action-based) with a goal (verifiable or LLM-judged). Seven production-ready loops span performance optimization, documentation, architecture refactoring, logging, error fixing, SEO audits, and product testing—but they're best for well-defined problems and require careful token budgeting.
What Loops Are and How They Work
Definition: Autonomous Goal-Seeking
A loop is a mechanism that allows an AI coding agent to work autonomously toward a specified goal without human intervention, removing the need for manual step-by-step direction and enabling faster execution.
Two Core Components
Every loop requires a trigger (what kicks it off) and a goal (what success looks like). The trigger can be manual, scheduled, or action-based; the goal can be verifiable (concrete, testable) or LLM-judged (the model decides when it's met).
Seven Production Loops
Sub-50ms Page Load Loop
Continuously optimizes every page, modal, and window in an application until each loads under 50 milliseconds. This is a verifiable goal with concrete metrics, making it ideal for loops. Trigger can be manual, scheduled, or tied to PR opens to prevent regressions.
Overnight Docs Sweep
Each night, reviews the entire codebase and updates documentation to reflect changes from the previous day, then opens a pull request. Uses LLM as judge since 'complete documentation' is subjective and requires model judgment.
Architecture Satisfaction Loop
Refactors code until the architecture is satisfactory, with the model deciding when it's 'happy.' After each step, it live-tests, runs auto-review, and commits. Can run nightly to keep the codebase clean and DRY.
Logging Coverage Loop
Reviews the system's logging and adds missing coverage until every important path produces useful, tested logs. Uses LLM as judge because 'important path' is non-deterministic and requires model judgment.
Production Error Sweep
Every night, reviews production logs for errors, traces root causes, fixes them, verifies fixes, opens pull requests, and notifies via Slack. Goal is concrete: no unaddressed errors in logs. Runs autonomously through all errors one by one.
SEO/GEO Visibility Loop
Runs an audit across crawlability, indexation, page intent, titles, internal links, structured data, and citations. Ranks gaps, fixes highest-leverage issues, reruns the crawl, and repeats until no critical technical issues remain. Ideal for weekly runs.
Full Product Evaluation Loop
Creates realistic scenarios covering every major capability, defines success criteria, runs all scenarios under identical conditions, and iteratively fixes anything that doesn't meet the bar. Non-deterministic and LLM-judged; can take 12+ hours but produces strong optimizations across the entire product.
Implementation and Tools
Using /goal in Codeex and Claude
To trigger a loop, paste the loop prompt into Codeex or Claude and add '/goal' at the beginning or end. This tells the tool to continue working autonomously until the specified goal condition is met, regardless of how long it takes.
Loop Library: Free Resource
A free library of production-ready loops that can be copied directly or used as inspiration for custom loops. Hosted on Vercel and maintained with new discoveries.
Caveats and Limitations
Not Every Problem Fits Loops
Loops work best when goals are verifiable (like 'under 50ms') or clearly definable. They struggle with subjective LLM judgment and are poor for building new features from scratch because the model's direction is unpredictable and feature decisions are ambiguous.
Token Cost and Budget Constraints
Loops churn through tokens autonomously and can run for 10 minutes to days. Some loops (like full product evaluation) routinely exceed 12 hours. This makes them expensive and unsuitable for teams without substantial token budgets or cost tolerance.
Feature Parity Example: Excel Clone
Attempting to build feature parity with Excel using a loop ran for days, using computer vision to click through Excel and verify features. Not recommended due to unpredictable direction and extreme token consumption.
Notable quotes
Loops are the frontier of AI workloads. — Matthew Berman
The hard part of building AI applications is not using the model. It's everything around the model. — Matthew Berman
I do not recommend doing that. — Matthew Berman
Action items
- Visit the loop library (link in video description) and copy a loop matching your use case.
- Paste the loop prompt into Codeex or Claude and add '/goal' to trigger autonomous execution.
- Start with a verifiable, well-defined goal (e.g., performance metric or error count) rather than subjective ones.
- Monitor token usage closely, especially for longer-running loops; set time/token limits if needed.
- Schedule loops to run off-peak (e.g., nightly) to avoid disrupting development.
- For custom loops, define success criteria clearly before execution to avoid unpredictable behavior.
- Apply for free consulting sessions (link in video) if you need hands-on help designing loops for your codebase.