Skip to content
OverKnightsOverKnights home

Recursive code improvement with AI: what works, and who should run the loop

Self-refine, reflection, tool-checked critique and test-anchored iteration: what the research says about AI code improvement loops, and where they fail.

OverKnightsPublished 3 min read
Short answer

Letting a model critique and refine its own output improved results by about 20% absolute on average across 7 tasks. Loops that learn from real feedback work well on code: Reflexion reached 91% pass@1 on HumanEval, against 80% for GPT-4 alone. Running and fixing code against tests in a loop raised GPT-4 from 19% to 44% pass@5 on CodeContests.

  • Letting a model critique and refine its own output improved results by about 20% absolute on average across 7 tasks.
  • Loops that learn from real feedback work well on code: Reflexion reached 91% pass@1 on HumanEval, against 80% for GPT-4 alone.
  • Running and fixing code against tests in a loop raised GPT-4 from 19% to 44% pass@5 on CodeContests.
  • Without external feedback, self-correction can make reasoning worse.

Four loops that improve AI code

  • Self-refine: the same model drafts, critiques its own draft and rewrites it, with no extra training.1
  • Reflection with memory: the agent writes down what went wrong after each attempt and reads those notes on the next try.2
  • Tool-checked critique: the model verifies its output with external tools such as a code interpreter, then revises. The authors stress that the external feedback is what makes it work.5
  • Test-anchored iteration: generate tests, run the code against them, fix, repeat. Writing useful extra tests turned out to be easier than writing correct code.3

Where the loops break

  • A model checking itself with nothing external to check against often swaps right answers for wrong ones, and reasoning can get worse.4
  • Functional gains do not carry over to security: in Veracode's tests the security pass rate stayed near 55% across model generations.6
  • Tests written by the same agent that wrote the code tend to share its blind spots. We see assertion-free and self-mocking tests often enough to give them their own check, AID-05.
  • Loops cost tokens, time and attention. Without a budget and a stop rule they run long and drift.

44% of AI code generation tasks introduced a risky security vulnerability in Veracode's tests. The pass rate has barely moved since the first report.6

What a good improvement loop needs

  • An external signal: tests, type checks, scanners and a run of the code, not the model's own opinion.
  • An independent reviewer: a different model or person from the one that wrote the change.
  • Memory: past mistakes turned into rules your coding agent reads on every session.
  • A budget: a cap on iterations and findings, so the loop ends.
  • A human gate on anything that merges, deploys or deletes.

How OverKnights runs the loop so you do not have to

Loop elementWhat OverKnights does
External signalScanners, type checks and local models run against every new commit, every night
Independent reviewerA second model verifies every High and Critical before it reaches your tracker
MemoryThe weekly report ends with one rule to add to your coding agent's instructions
Test anchorEvery issue carries a fix prompt and a test that fails until the fix lands
BudgetAt most 5 new issues per repo per night; the rest roll into the weekly report
Human gateWe never push or merge. Your team decides what ships

Your team keeps building the product during the day. The improvement loop runs at night, on someone else's attention, and hands back a short list that is ready to act on.

Note: Our loop draws on these methods. It is not a reproduction of the papers' benchmarks, and their numbers come from their own test sets.

Sources

  1. NeurIPS 2023, Self-Refine: Iterative Refinement with Self-Feedback (Madaan et al.), 2023. Peer-reviewed. 7 tasks, GPT-3.5 and GPT-4.
  2. NeurIPS 2023, Reflexion: Language Agents with Verbal Reinforcement Learning (Shinn et al.), 2023. Peer-reviewed.
  3. arXiv, Code Generation with AlphaCodium: From Prompt Engineering to Flow Engineering (Ridnik et al.), Jan 2024. Preprint by CodiumAI, now Qodo, a code review vendor.
  4. ICLR 2024, Large Language Models Cannot Self-Correct Reasoning Yet (Huang et al.), 2024. Google DeepMind and University of Illinois.
  5. ICLR 2024, CRITIC: Large Language Models Can Self-Correct with Tool-Interactive Critiquing (Gou et al.), 2024.
  6. Veracode, 2026 GenAI Code Security Report: AI is writing more of your code but security hasn't caught up, Jul 2026. Vendor research. Generated code tested with Veracode's own SAST tool.
OverKnights

Want to know what your repo would show?

Send one public repo. We reply with three verified findings and the prompts to fix them. No call needed.