The AI Coding Loop: How to Iterate Without Losing Your Mind

The loop that actually works

You've probably tried this: ask AI to write code, get code, run it, it breaks, ask AI to fix it, get new code, it breaks differently, repeat until you give up and write it yourself.

That's not a loop. That's a spiral. Here's the loop that actually works.

The 5-step loop

    ┌─────────────────────────────────────────────────┐
    │                                                 │
    v                                                 │
 ┌──────┐   ┌──────┐   ┌──────┐   ┌──────┐   ┌──────┐
 │ASK   │──▶│READ  │──▶│TEST  │──▶│FIX   │──▶│COMMIT│
 └──────┘   └──────┘   └──────┘   └──────┘   └──────┘
    │          │          │          │          │
    │          │          │          │          │
    v          v          v          v          v
  Clear     Don't      Run it    You fix    Save the
  task     trust it    yourself   manually    win

Each step has one rule. Break the rule, waste time.

Step 1: ASK (clear task)

The task must be specific enough that you'd accept any correct answer, not just the one you're imagining.

Bad:  "Add authentication"
      (What kind? Where? For what?)

Good: "Add a JWT middleware that checks the
       Authorization header on /api/* routes.
       Return 401 if missing or expired.
       Use the jose library we already have."
      (specific, bounded, uses existing deps)

The rule: If you can't describe "done" in one sentence, the task isn't ready.

Step 2: READ (don't trust it)

The AI gave you code. Great. Now read it. Every line.

This is the step most people skip. They see code that "looks right" and move on. But "looks right" and "is right" are different things.

What to check:
- Does it use the libraries we already have?
- Does it match our code style?
- Are there any magic numbers?
- Does it handle errors?
- Does it import things that don't exist?
- Is it actually doing what we asked?

The rule: Never run code you haven't read. "It compiled" is not a review.

Step 3: TEST (run it yourself)

Run the code. Actually run it. Don't just look at it and say "seems fine."

┌─────────────────────────────────────────┐
│         TESTING CHECKLIST               │
├─────────────────────────────────────────┤
│ [ ] Does it run without errors?         │
│ [ ] Does it do what we asked?           │
│ [ ] Does it handle the happy path?      │
│ [ ] Does it handle edge cases?          │
│ [ ] Does it break anything else?        │
└─────────────────────────────────────────┘

The rule: "The AI said it works" is not testing. Run the tests. Try the edge cases. Break it on purpose.

Step 4: FIX (you do it manually)

Something's wrong? Fix it yourself. Don't ask the AI to fix it.

Why? Because if you ask the AI to fix its own bug, you're back at Step 1 with a new task. The loop restarts. You lose context. You lose time.

AI gave you:
  const users = await db.query('SELECT *')

You see the bug:
  - No WHERE clause (returns everything)
  - SQL injection risk
  - No error handling

Don't: "Fix this query"
Do:   Add the WHERE clause yourself.
      Add the parameterized query.
      Add try/catch.
      Now you understand the code.

The rule: Small fixes you do yourself. Big rewrites you ask the AI for.

Step 5: COMMIT (save the win)

You have working code. You understand it. You tested it. Commit it.

This is not about git (though that's good too). It's about saving your progress before starting the next loop.

Why commit early:
- If the next loop goes bad, you have a checkpoint
- You can review what changed between loops
- You can revert if you need to
- Your brain gets a "done" signal (motivating!)

The rule: Working code > perfect code. Commit the working version, then iterate.

The anti-patterns

Here's what NOT to do:

❌ The Vague Loop
   Ask → Get → Ask → Get → Ask → Get → Give up
   (Never test, never read, just keep asking)

❌ The Trust Loop
   Ask → Get → Ship → Break → Ask → Get → Ship → Break
   (Never test, just ship whatever the AI gives)

❌ The Perfection Loop
   Ask → Get → Ask for fix → Get → Ask for fix → Get...
   (Never commit, always want it "better")

❌ The Context Collapse
   Ask → Get → Ask about different thing → Get confused
   (Mix unrelated tasks in one conversation)

The context strategy

Each loop, you're managing context. Here's how:

┌─────────────────────────────────────────────────────┐
│ CONVERSATION CONTEXT                                │
├─────────────────────────────────────────────────────┤
│ Loop 1: [task] [AI response] [your fix] [commit]    │
│ Loop 2: [new task] [AI response] [your fix] [commit]│
│ Loop 3: [new task] [AI response] [your fix] [commit]│
└─────────────────────────────────────────────────────┘

Option A: New conversation each loop
          - Clean context
          - No confusion from old code
          - Good for unrelated tasks

Option B: Same conversation, clear delimiters
          - Keeps related context
          - Use "Now let's move to..." to separate
          - Good for related changes

The rule: If the AI seems confused, start a new conversation. Context pollution is real.

The speed tricks

Tricks that make the loop faster:

1. Paste the error, not the code
   "TypeError: Cannot read property 'map' of undefined"
   → AI knows exactly what's wrong

2. Give the expected output
   Input: "hello world"
   Expected: "Hello World"
   → Clear success criteria

3. Reference existing patterns
   "Follow the same pattern as UserService.ts"
   → AI copies the style

4. Use comments as instructions
   // TODO: add error handling for null values
   → AI fills in the blank

5. Ask for the diff, not the whole file
   "Show me only the changes, as a diff"
   → Easier to review

The review checklist

Before you commit, ask yourself:

[ ] Does this code do what I asked?
[ ] Did I read every line?
[ ] Did I run it?
[ ] Did I test the edge cases?
[ ] Would I write it this way myself?
[ ] Can I explain it to someone else?
[ ] Is it consistent with our codebase?
[ ] Did I fix the small things myself?

If any answer is "no," fix it before moving on.

The meta-skill

The loop isn't about the AI. It's about you.

  • Step 1 teaches you to think clearly about what you want
  • Step 2 teaches you to read code critically
  • Step 3 teaches you to test your assumptions
  • Step 4 teaches you to fix things yourself
  • Step 5 teaches you to ship incrementally

The AI is a tool. The loop is a habit. Habits stick.

When to break the loop

Sometimes you should stop looping and just write the code yourself:

  • The task is simpler than explaining it to the AI
  • The AI keeps making the same mistake
  • You don't understand the code the AI is giving you
  • The code is security-sensitive
  • You're debugging a production issue

The rule: If the loop takes longer than just writing it, write it.

The 30-second summary

ASK    → Clear, specific task
READ   → Check every line
TEST   → Run it yourself
FIX    → Small fixes you do
COMMIT → Save working code

Repeat. Don't trust. Don't skip steps.

The AI doesn't know if the code works. You do. The loop makes sure you check.

Further reading: GitHub Copilot Best Practices, Cursor Documentation, The Pragmatic Programmer, and Refactoring by Martin Fowler.