Iterative Refinement
Iterative Refinement
Section titled “Iterative Refinement”Confidence: Tier 2 — Validated pattern observed across many Claude Code users.
Prompt, observe, reprompt until satisfied. The core loop of effective AI-assisted development.
Table of Contents
Section titled “Table of Contents”- TL;DR
- The Loop
- Feedback Patterns
- Autonomous Loops
- Integration with Claude Code
- Script Generation Workflow
- Iteration Strategies
- Anti-Patterns
- Community Patterns & Known Limitations
- See Also
1. Initial prompt with clear goal2. Claude produces output3. Evaluate against criteria4. Specific feedback: "Change X because Y"5. Repeat until doneKey insight: Specific feedback > vague feedback
The Loop
Section titled “The Loop”Step 1: Initial Prompt
Section titled “Step 1: Initial Prompt”Start with clear intent and constraints:
Create a React component for a user profile card.- Show avatar, name, bio- Include edit button- Use Tailwind CSS- Mobile-responsiveStep 2: Evaluate Output
Section titled “Step 2: Evaluate Output”Claude produces code. Evaluate:
- Does it meet requirements?
- What’s missing?
- What’s wrong?
- What could be better?
Step 3: Specific Feedback
Section titled “Step 3: Specific Feedback”Provide targeted corrections:
Good start. Changes needed:1. Avatar should be circular, not square2. Edit button should only show for own profile (add isOwner prop)3. Bio should truncate after 3 lines with "Show more"Step 4: Repeat
Section titled “Step 4: Repeat”Continue until satisfied:
Better. One more thing:- Add loading skeleton state for when data is fetchingFeedback Patterns
Section titled “Feedback Patterns”Effective Feedback
Section titled “Effective Feedback”| Pattern | Example |
|---|---|
| Specific location | ”Line 23: change === to ==” |
| Clear action | ”Add error boundary around the form” |
| Reason given | ”Remove the console.log because it leaks user data” |
| Priority marked | ”Critical: fix the SQL injection. Nice-to-have: add pagination.” |
Ineffective Feedback
Section titled “Ineffective Feedback”| Anti-Pattern | Why It Fails | Better Alternative |
|---|---|---|
| ”Make it better” | No direction | ”Improve readability by extracting the validation logic" |
| "This is wrong” | No specifics | ”The date format should be ISO 8601, not Unix timestamp" |
| "I don’t like it” | Subjective | ”Use functional components instead of class components" |
| "Fix the bugs” | Too vague | ”Fix: 1) null check on line 12, 2) off-by-one in loop” |
Autonomous Loops
Section titled “Autonomous Loops”Claude can self-iterate with clear completion criteria.
The Ralph Wiggum Pattern
Section titled “The Ralph Wiggum Pattern”Named after the self-improvement loop pattern:
Keep improving the code quality until:1. All tests pass2. No TypeScript errors3. ESLint shows zero warnings
After each iteration, run the checks and fix any issues.Stop when all criteria are met.Completion Criteria Examples
Section titled “Completion Criteria Examples”Iterate until:- Response time < 100ms for 95th percentile- Test coverage > 80%- All accessibility checks pass- Bundle size < 200KBIteration Limits
Section titled “Iteration Limits”Always set limits to prevent infinite loops:
Improve the algorithm performance.Maximum 5 iterations.Stop early if improvement < 5% between iterations.Integration with Claude Code
Section titled “Integration with Claude Code”With Task Tool
Section titled “With Task Tool”Track refinement iterations using TaskCreate and TaskUpdate:
TaskCreate: "Implement initial version"TaskCreate: "Fix: handle empty arrays"TaskCreate: "Fix: add input validation"TaskCreate: "Optimization: memoize expensive calculations"# Mark completed as you progress with TaskUpdateWith Hooks
Section titled “With Hooks”Auto-validate after each change using Claude Code hooks (configured via /hooks command or settings.json). For example, a PostToolUse hook on the Edit tool can run linting and tests automatically. Claude sees failures and can self-correct.
With /compact
Section titled “With /compact”When context grows during iterations:
/compact
Continue refining the search algorithm.We've made good progress, focus on the remaining issues.Checkpointing
Section titled “Checkpointing”After significant progress:
Good progress. Let's checkpoint:- Commit what we have- List remaining issues- Continue with the next priorityScript Generation Workflow
Section titled “Script Generation Workflow”Script and automation generation delivers the highest ROI for iterative refinement—70-90% time savings in practitioner reports. Scripts are self-contained, testable in isolation, and yield immediate value.
The 3-7 Iteration Pattern
Section titled “The 3-7 Iteration Pattern”Most production-ready scripts emerge after 3-7 iterations:
| Iteration | Focus | Prompt Pattern |
|---|---|---|
| 1 | Basic functionality | ”Create a script that [goal]“ |
| 2-3 | Constraints + edge cases | ”Add [constraint]. Handle [edge case].“ |
| 4-5 | Hardening | ”Add error handling, logging, input validation” |
| 6-7 | Polish | ”Optimize for [metric]. Add usage docs.” |
Example: Kubernetes Pod Manager (PowerShell)
Section titled “Example: Kubernetes Pod Manager (PowerShell)”Iteration 1 — Basic
Create a PowerShell function to list pods in a Kubernetes namespace.Iteration 2 — Add filtering
Add: filter by label selector and pod status.Show: pod name, status, age, restarts.Iteration 3 — Add actions
Add: ability to delete pods matching filter.Require: confirmation before deletion.Iteration 4 — Error handling
Handle: kubectl not found, invalid namespace, permission denied.Add: verbose logging with -Verbose flag.Iteration 5 — Production ready
Add: dry-run mode, output to JSON for piping, help documentation.Ensure: works on Windows, Linux, macOS.Common Pitfalls
Section titled “Common Pitfalls”| Pitfall | Example | Mitigation |
|---|---|---|
| Hallucinated commands | apt-get on macOS | Specify OS: “Ubuntu 22.04 only” |
| Security gaps | No input validation | Always request: “validate all user inputs” |
| Over-engineering | Adds unnecessary libs | Request: “minimal dependencies, stdlib preferred” |
| Context drift | Forgets requirements after iteration 5 | Checkpoint prompt: “Recap current requirements before next change” |
| Platform assumptions | Assumes bash features in sh | Specify: “POSIX-compliant” or “bash 4+“ |
Script Iteration Template
Section titled “Script Iteration Template”Current script: [paste or reference]
Iteration goal: [specific improvement]
Constraints:- Must preserve: [existing behavior to keep]- Must not: [things to avoid]- Target environment: [OS, shell, runtime]
Success criteria: [how to verify this iteration works]Iteration Strategies
Section titled “Iteration Strategies”Breadth-First
Section titled “Breadth-First”Fix all issues at same level before going deeper:
First pass: Fix all type errorsSecond pass: Fix all lint warningsThird pass: Improve test coverageFourth pass: Optimize performanceDepth-First
Section titled “Depth-First”Complete one area fully before moving on:
1. Perfect the authentication flow (all aspects)2. Then move to user management3. Then move to settingsPriority-Based
Section titled “Priority-Based”Address by importance:
Iterate in this order:1. Security issues (critical)2. Data integrity bugs (high)3. UX problems (medium)4. Code style (low)Anti-Patterns
Section titled “Anti-Patterns”Moving Target
Section titled “Moving Target”# Wrong"Actually, let's change the approach entirely..."(Repeated 5 times)
# RightCommit to an approach, iterate within it.If approach is wrong, explicitly restart.Perfectionism Loop
Section titled “Perfectionism Loop”# WrongKeep improving forever
# RightSet clear "good enough" criteria:- Tests pass- Handles main use cases- No critical issues→ Ship it, improve laterLost Context
Section titled “Lost Context”# WrongAfter 50 iterations, forget what the goal was
# RightPeriodically restate the goal:"Reminder: we're building a rate limiter.Current state: basic implementation works.Next: add Redis backend."Review Auto-Correction Loop
Section titled “Review Auto-Correction Loop”Specialized iterative pattern for code review where Claude reviews → fixes → re-reviews until convergence.
Pattern
Section titled “Pattern”┌─────────────────────────────────────────┐│ Review Auto-Correction Loop ││ ││ Review (identify issues) ││ ↓ ││ Fix (apply corrections) ││ ↓ ││ Re-Review (verify fixes) ││ ↓ ││ Converge (minimal changes) → Done ││ ↑ ││ └──── Repeat (max iterations) │└─────────────────────────────────────────┘Prompt Template
Section titled “Prompt Template”Review this PR with auto-correction:1. Multi-agent review (3 scope-focused agents)2. Fix all 🔴 Must Fix issues3. Re-review to verify fixes didn't introduce new issues4. Fix all 🟡 Should Fix issues5. Re-review one final time6. Stop when only 🟢 Can Skip remain
Max iterations: 3Stop early if iteration produces <5 lines changedSafeguards
Section titled “Safeguards”| Safeguard | Purpose | Implementation |
|---|---|---|
| Max iterations | Prevent infinite loops | Hard limit: 3 iterations |
| Quality gates | Ensure fixes are valid | Run tsc && lint before each iteration |
| Protected files | Prevent risky changes | Skip auto-fix for: package.json, migrations, .env |
| Change threshold | Stop when converged | Exit if iteration changes <5 lines |
| Rollback capability | Recover from bad fixes | Git commit before each iteration |
Example Session
Section titled “Example Session”Iteration 1: Initial Review
Claude: Found 8 issues:- 🔴 3 Must Fix (SQL injection, empty catch, missing auth)- 🟡 4 Should Fix (DRY violations, N+1 query)- 🟢 1 Can Skip (naming style)Iteration 2: Fix Must Fix + Re-Review
Claude: Fixed 3 Must Fix issues.Re-review: All 🔴 resolved. No new issues introduced.Remaining: 4 🟡 Should Fix, 1 🟢 Can SkipIteration 3: Fix Should Fix + Re-Review
Claude: Fixed 4 Should Fix issues.Re-review: All 🟡 resolved. No new issues.Remaining: 1 🟢 Can Skip (optional improvement)Convergence
Claude: Converged. Only optional improvements remain.Changes this iteration: 2 lines (below threshold).Review complete. ✅Comparison: One-Pass vs Convergence Loop
Section titled “Comparison: One-Pass vs Convergence Loop”| Aspect | One-Pass Review | Convergence Loop |
|---|---|---|
| Detection | Find all issues once | Find issues → fix → verify → repeat |
| Follow-up awareness | Check git log for “Co-Authored-By: Claude” | Each iteration is aware of previous |
| False positives | Can suggest fixes for already-fixed code | Re-review catches this |
| Confidence | Single validation | Multiple validation passes |
| Time cost | Fastest (1 review) | Slower (3+ reviews) |
| Quality | Good for experienced devs | Better for critical code |
When to use:
- One-pass: Simple PRs, experienced team, time-sensitive
- Convergence loop: Security-critical code, junior team, high-stakes production
Integration with Multi-Agent Review
Section titled “Integration with Multi-Agent Review”Combine convergence loop with multi-agent review for maximum quality:
Each iteration:├─ Agent 1: Consistency Auditor├─ Agent 2: SOLID Principles Analyst└─ Agent 3: Defensive Code Auditor ↓ Fix issues ↓ Re-run 3 agents ↓ Verify fixes + check for new issues ↓ Repeat until convergenceConvergence Criteria
Section titled “Convergence Criteria”Stop iterating when ANY of these is true:
- No issues remaining (ideal outcome)
- Max iterations reached (3 iterations default)
- Change threshold (iteration changed <5 lines)
- Quality gate failure (tsc/lint fails after fix)
- Manual stop (user requests halt)
Anti-Patterns in Review Loops
Section titled “Anti-Patterns in Review Loops”| Anti-Pattern | Problem | Solution |
|---|---|---|
| Infinite loop | No convergence criteria | Set max iterations + change threshold |
| Scope creep | Each iteration adds new requirements | Lock scope before starting loop |
| Breaking fixes | Fix introduces new bugs | Re-review after each fix + quality gates |
| Protected file changes | Modifies package.json, migrations | Explicit skip list for protected files |
| Context loss | Forgets original issues after iteration 3 | Maintain issue tracker across iterations |
Example Session
Section titled “Example Session”Initial Request
Section titled “Initial Request”Create a debounce function in TypeScript.Iteration 1
Section titled “Iteration 1”Looks good. Add:- Generic type support for any function signature- Option to execute on leading edgeIteration 2
Section titled “Iteration 2”Better. Issues:- The return type should preserve the original function's return type- Add cancellation supportIteration 3
Section titled “Iteration 3”Almost there. Final polish:- Add JSDoc comments- Export the types separately- Add unit testsCompletion
Section titled “Completion”Perfect. Commit this as "feat: add debounce utility with full TypeScript support"Community Patterns & Known Limitations
Section titled “Community Patterns & Known Limitations”The community has built several patterns on top of Claude Code’s iterative loop. Some solve real pain points, others expose current limitations worth knowing about.
Ralph Loop (Test-Driven Autonomous Iteration)
Section titled “Ralph Loop (Test-Driven Autonomous Iteration)”Source: nathanonn.com, February 2026.
The Ralph Loop constrains autonomous iteration to one test case per cycle instead of running the full suite every time. This keeps each cycle focused and prevents the agent from chasing multiple failures at once.
How it works:
- Pick one failing test case
- Fix it, verify it passes
- Save progress to a JSON state file
- Move to the next failing test case
- After 3 failed attempts on the same case, mark it as
known_issueand skip it
{ "current_case": "test_auth_token_refresh", "attempts": 2, "known_issues": ["test_legacy_migration_edge_case"], "completed": ["test_login", "test_logout", "test_session_timeout"]}The state file is the key innovation here. It survives context resets, /compact operations, and even full session restarts. The agent reads the file at the start of each cycle to know exactly where it left off, which cases are done, and which ones to skip.
The 3-attempt limit prevents the infinite loop trap that plagues naive autonomous loops. Rather than burning tokens on a stubborn test case, the agent moves forward and flags the issue for human review later.
Auto-Continue Skill
Section titled “Auto-Continue Skill”Source: mcpmarket.com.
A confidence-based continuation system that decides whether the agent should keep going or stop for human input. Instead of a fixed iteration count, it evaluates the situation after each cycle:
Auto-continues when:
- Tests pass
- Build succeeds
- No new error types detected
- Confidence score remains above threshold
Stops for human input when:
- Confidence drops below threshold
- A new category of error appears (not just a new instance of a known error)
- Build or type-check fails in a way the agent hasn’t seen before
This pairs well with Claude Code’s Stop hooks. The skill can trigger post-task verification and decide whether to resume based on the results.
Stop Hooks for Automatic Verification
Section titled “Stop Hooks for Automatic Verification”A pattern that turns Claude Code’s hook system into an automatic quality gate between iterations:
- Claude finishes a task (or an iteration)
- A
PostToolUsehook onTodoWritetriggers a verification script - The script runs type-check, lint, and tests
- Errors get piped back to Claude automatically
- Claude fixes the issues without human intervention
{ "hooks": { "PostToolUse": [ { "matcher": "TodoWrite", "command": "bash -c 'npm run typecheck 2>&1; npm run lint 2>&1; npm test 2>&1'" } ] }}The hook fires every time Claude marks a task as done. If the verification catches something, Claude sees the output and can self-correct before moving to the next task.
Escalation Strategy
Section titled “Escalation Strategy”What to do when 3 iterations fail on the same problem. Instead of looping forever or giving up, follow a structured escalation path:
- Decompose: Break the failing task into 2-3 smaller sub-tasks that can be tackled independently
- Collect context: Dump all error messages, stack traces, and attempted fixes into a structured file
- Model escalation: If using Sonnet, retry the specific failing case with Opus for deeper reasoning
- Human escalation: If the model upgrade doesn’t help, create a GitHub issue with the full error context and mark the task as
known_issue
# Escalation in practiceif [ "$ATTEMPT_COUNT" -ge 3 ]; then # Collect context cat errors.log attempts.log > escalation-context.md
# Try with Opus claude --model claude-opus-4-6 \ "Fix this failing test. Context: $(cat escalation-context.md)"
# If still failing, create issue if [ $? -ne 0 ]; then gh issue create \ --title "Auto-escalation: $TEST_NAME fails after 3 attempts" \ --body "$(cat escalation-context.md)" \ --label "known_issue,needs-human" fifiThe goal is never to silently drop work. Every failure either gets resolved, escalated, or explicitly tracked.
Known Limitations
Section titled “Known Limitations”Being honest about what doesn’t work yet, so you don’t waste time reinventing solutions that don’t exist.
No built-in retry/verify/resume (GitHub issue #28489): Headless automation in Claude Code lacks native support for retry logic, verification gates, and session resumption. Every team implementing autonomous loops builds their own version of this. State files, hook-based verification, and escalation scripts are all community workarounds for a gap in the platform.
Agent iterations can be lost (GitHub issue #28843): In multi-day workflows, agent iterations and their accumulated context can be destroyed. If you’re running a workflow that spans multiple sessions or days, save explicit state files every N iterations. Do not rely on Claude’s conversation memory as your only source of truth.
Multi-day workflow fragility: Long-running automation needs checkpointing discipline. Save state to disk (JSON files, git commits, issue comments) at regular intervals. The pattern is simple but easy to forget: if you can’t reconstruct the agent’s progress from files on disk alone, your workflow will break on session boundaries.
See Also
Section titled “See Also”- exploration-workflow.md — Explore alternatives before iterating
- tdd-with-claude.md — TDD is iterative refinement with tests
- plan-driven.md — Plan before iterating
- ../methodologies.md — Iterative Loops methodology