Iterative refinement

From sammcj/agentic-coding · View interactive version

Iterative refinement is an AI agent skill from the GitHub repository sammcj/agentic-coding. Disciplined, measurable iteration for a substantial refinement or investigation: loop against verifiable pass/fail conditions, fan work out to subagents, and keep the main context lean. Use when improving something measurable over repeated cycles (tuning a metric or detector, refactoring against a r

How to use this skill

Skill file

Iterative refinement with verifiable conditions

Improve any system (script, pipeline, prompt, doc, config, dataset) by looping against measurable pass/fail conditions while keeping the main thread's context lean. Task-agnostic. The aim is to make "is it good yet?" a single repeatable command, catch each regression at the edit that caused it, and keep load-bearing reasoning in cheap, auditable steps.

Use this as a toolkit, not a script. Each method below earns its place by what it prevents, and that reasoning is stated inline so you can judge when it applies. Reach for the methods the situation calls for, scale them to the stakes, and adapt or skip what doesn't fit; they compose well, but no fixed subset is mandatory and this skill can't anticipate every task you'll point it at. When a method clearly fits, lean into it fully rather than half-applying it. The judgement of which to use, and how hard, stays yours.

The loop

  1. Turn the goal into a pass/fail rubric with explicit thresholds. "Useful" isn't checkable; "contamination < 2%, both naive and effective ratios reported, every input row classified, prints Overall: PASS" is.
  2. Bake the rubric into the artifact as a self-check it prints. The artifact computes its own metrics and prints [PASS]/[FAIL] per condition, so the check can't drift out of sync with the code the way an external checklist does. Now any party (you, a subagent, the user) re-verifies with one command.
  3. Change one layer, re-run the check. Catch each regression at the edit that caused it, not three edits later.
  4. Loop on a fast slice, not the full dataset. Size the slice so a run takes seconds, not minutes. Run the full corpus only at checkpoints, when a layer is structurally complete and at sign-off, since that's where rare signals and period splits actually appear and the cost is justified.
  5. Keep a frozen held-out slice the loop never touches, and confirm against it before sign-off. Looping hard on one slice optimises the rubric for that slice (Goodhart on your own metric); the held-out slice is what proves the gain generalised rather than memorised.
  6. When all conditions pass but value remains, write a stricter rubric and loop again. Stop when a cycle yields nothing material.

Writing good conditions

Context-window economics

Every byte a tool returns stays in context and taxes every later turn. This is context engineering: treat the window as the scarcest resource, because reasoning quality degrades with depth (context rot) well before any hard limit. Three mitigations, highest leverage first:

  1. Think-in-code. To _process_ output (filter/count/parse/aggregate), run code over it and print only the answer; raw bytes never enter the conversation. Reading bytes is justified only when you'll _edit_ them. The context-mode ctx_execute tools do this in a sandbox when available.
  2. Return conclusions, not transcripts. A subagent's tool history stays in its context; only its final message returns. A task worth 50 file-reads costs you one paragraph. Forbid the subagent from pasting file bodies or full reports.
  3. Locate before reading. Print the line numbers or symbols you need with one targeted query, then read only that span.

Reasoning degrades in bands: roughly 0-100k tokens is peak, 100-150k still strong, 150-200k noticeably softer, and auto-compaction looms around a third of the window. Budget against the bands, not the ceiling. Bring the main thread's irreversible state (task list, strategy notes, decisions) up to date _before_ you approach compaction; a summary written ahead of time survives, working memory you were relying on may not. If a single task can't fit the smart zone, that's a signal to decompose and delegate, not to push through.

Delegation and subagents

Push work off the main thread whenever it produces more bytes than its conclusion is worth.

Fan-out topology

Match it to the write-pattern:

Accuracy and verifiability

Regression recovery

When a fix won't hold after a few tries, stop pushing on the same line:

Staying on task

A live task list is the backbone of completeness, not bureaucracy. Stand one up before the first loop iteration and keep it current as you work, marking items done and adding new ones the moment they surface. It's the one piece of state that reliably survives compaction, so treat it as the source of truth for what's done and what's left, not an afterthought you reconstruct at the end.

Prior art (why this works)

This playbook assembles established techniques. Reach for the named version when you want to go deeper or justify the approach.

| Practice here | Established name | Origin |

|---|---|---|

| The rubric-and-self-check loop | evaluator-optimizer pattern; eval-driven development | Anthropic, _Building Effective Agents_; Hamel Husain, _Your AI Product Needs Evals_ |

| Critique-then-revise iteration | Reflexion; Self-Refine | Shinn et al. 2023; Madaan et al. 2023 |

| Reconciliation invariants between steps | programmatic gates | Anthropic, _Building Effective Agents_ |

| Fix the measurement before the output | construct validity; Goodhart's law | Cronbach & Meehl 1955 |

| Meaningfulness audit and judge calibration | LLM-as-judge failure modes | Zheng et al. 2023 (MT-Bench) |

| Context economics, conclusions-not-transcripts | context engineering; context rot; compaction | Anthropic, _Effective Context Engineering for AI Agents_ |

| Read-only parallel auditors that vote | self-consistency; orchestrator-workers | Wang et al. 2022; Anthropic, _Multi-Agent Research System_ |

For a worked example of the whole chain (a "frustration spike" that turned out to be a measurement artifact), see references/worked_example.md.

Checklist

A prompt to confirm what applies to the task in front of you, not a gate every task must clear. Skip the lines that don't fit.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.