Iterative Self-Correction Loops
Run tests, parse failures, ask the model to fix — the inner loop of every modern code agent.
Iterative Self-Correction Loops is a free AI Agents lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the AI Agents learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why Self-Correction?
First attempts often fail. The defining property of a code agent is its ability to:
- Run the code
- Read the error
- Fix the code
- Repeat
This inner loop is what separates real coding agents from text-completion demos.
The Loop
def code_until_pass(task, max_iters=10):
write_initial_code(task)
for i in range(max_iters):
result = run_tests()
if result.passed:
return 'success'
fix_prompt = f'Tests failed:\n{result.stderr}\nRead the code, identify the bug, and fix it with edit_file.'
agent.run(fix_prompt)
return 'gave up'Provide Useful Feedback
Generic "tests failed" is not actionable. Pass full error output, stack traces, expected-vs-actual:
fix_prompt = f'''
The tests failed with this output:
FAILED tests/test_utils.py::test_add
AssertionError: expected 4, got 5
at line 42
Fix the code by editing the file.
'''Re-Read Before Re-Fixing
Models forget. Force them to re-read the current state of the file each iteration:
fix_prompt = 'First, read_file the failing module. Then edit it.'Detect Repeat Failures
If the same test fails 3 iterations in a row with identical errors, the agent is stuck. Replan or escalate:
if same_error_count >= 3:
return 'stuck — replanning needed'Replan When Stuck
replan_prompt = f'''
The current approach is not working. The repeated error is:
{error}
Describe ONE alternative approach to solving the task.
'''Escalate to Bigger Model
If a cheap model is stuck, retry the SAME fix prompt with a stronger model:
if iter > 3 and using_cheap_model:
switch_to_big_model()
# often unblocks 50% of stuck casesVerify After Each Edit
Don't batch many edits then verify — too hard to attribute failures. Edit one thing, verify, edit next.
Include Test Output in Context
Keep recent test output in the agent's context. Without it, the agent doesn't know what it just did:
messages.append({'role': 'tool', 'content': f'After your edit, tests now: {test_summary}'})Avoid Infinite "Fix" Loops
Hard cap iterations. If at MAX_ITERS the task isn't done, escalate to a human:
if iter == MAX_ITERS - 1:
notify_human(task, current_state, error_history)
breakTrack Self-Correction Metrics
- Average iterations to success
- Tasks giving up at max_iter
- Tasks succeeding in 1 iteration vs 5
These metrics drive prompt and tool improvements.
Self-Critique Without Tests
For tasks without a clear test suite, add a critic step: have the agent review its own diff before submitting:
critique_prompt = '''
Review your changes for:
- Off-by-one errors
- Missing error handling
- Breaking other tests
Return FIX LATER or DONE.
'''Self-Correction Inputs
What's the single most important input for an agent's "fix loop"?
Recap
Run -> error -> fix -> repeat. Pass concrete errors. Re-read before re-fixing. Detect stalls. Escalate to bigger models or humans when stuck.
Frequently asked questions
Is the “Iterative Self-Correction Loops” lesson free?
Yes — the full text of “Iterative Self-Correction Loops” is free to read here on the web, and the AI Agents course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the AI Agents course, upgrade to CoddyKit PRO.
What will I learn in “Iterative Self-Correction Loops”?
Run tests, parse failures, ask the model to fix — the inner loop of every modern code agent. You practise AI Agents with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start AI Agents?
No prior experience is required. AI Agents on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Iterative Self-Correction Loops” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this AI Agents lesson?
Yes. Every AI Agents lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Agentic Patterns: Plan-Execute-Verify
- Tool Surfaces for Coding (Read, Edit, Bash)
- Iterative Self-Correction Loops
- SWE-Agent and OpenDevin Architectures