Build an autonomous loop that audits your site, reads fix prompts, applies changes, and re-audits until a target score is reached.
The Pattern
The fix loop follows a simple cycle: audit, analyze the weakest criteria, apply fixes, re-audit, and compare. If the score is still below your target, repeat. This pattern works with both the REST API (scripted) and MCP tools (interactive with an AI agent).
TypeScript Implementation
fix-loop.ts
const API = class="code-string">"https:class="code-commentclass="code-string">">//www.aeocontent.ai/api/v1";
const SLUG = class="code-string">"your-domain-com";
const TARGET_SCORE = 80;
const MAX_ITERATIONS = 5;
const headers = {
class="code-string">"Authorization": class="code-string">`Bearer ${process.env.AEO_API_KEY}`,
class="code-string">"Content-Type": class="code-string">"application/json",
};
asyncfunction waitForAudit(slug: string): Promise<void> {
let status = class="code-string">"pending";
while (![class="code-string">"completed", class="code-string">"failed"].includes(status)) {
awaitnew Promise(r => setTimeout(r, 30000));
const res = await fetch(class="code-string">`${API}/audits/${slug}/status`, { headers });
status = (await res.json()).data.status;
}
if (status === class="code-string">"failed") thrownew Error(class="code-string">"Audit failed");
}
asyncfunction getAuditWithFixes(slug: string) {
const res = await fetch(
class="code-string">`${API}/audits/${slug}?include=fix_prompts`,
{ headers }
);
return res.json();
}
class=class="code-string">"code-comment">// Main loop
for (let i = 0; i < MAX_ITERATIONS; i++) {
console.log(class="code-string">`\n--- Iteration ${i + 1} ---`);
class=class="code-string">"code-comment">// Get current audit + fix prompts
const { data } = await getAuditWithFixes(SLUG);
const score = data.audit.overall_score;
console.log(class="code-string">`Current score: ${score}/100`);
if (score >= TARGET_SCORE) {
console.log(class="code-string">"Target reached!");
break;
}
class=class="code-string">"code-comment">// Show top fixes
const fixes = data.fix_prompts.slice(0, 3);
for (const fix of fixes) {
console.log(class="code-string">` [${fix.criterion}] ${fix.current_score}/10 -> apply fix`);
class=class="code-string">"code-comment">// Your code: apply the fix.prompt to your codebase
}
class=class="code-string">"code-comment">// Request re-audit
await fetch(class="code-string">`${API}/audits/${SLUG}/reaudit`, {
method: class="code-string">"POST", headers
});
class=class="code-string">"code-comment">// Wait for completion
await waitForAudit(SLUG);
}
MCP Approach
With MCP tools in Claude Code or Cursor, the same loop happens conversationally. The agent reads your codebase, applies changes directly, and verifies them:
Claude Code prompt
Check the AEO audit for my-domain.com with fix prompts.
Apply the top 3 fixes to my codebase, then re-audit.
Keep going until the score reaches 80.
Re-audits are rate-limited to one at a time per domain. Wait for the current audit to complete before requesting another.
Best Practices
Set a realistic target (80 is achievable for most sites)
Fix the top 3 highest-impact criteria per iteration
Wait for re-audit completion before making more changes
Use version comparison to track progress
Cap iterations to avoid infinite loops on hard-to-fix criteria