Self-debugging

Also known as: execution feedback loop

On this page 7

Self-debugging is a technique in which a language model generates code, executes or inspects it, interprets the resulting feedback and revises its own program without human intervention, repeating the cycle until the code passes its checks or a budget runs out. In AI for coding, it is also described as an execution feedback loop, and it forms a core mechanism of agentic coding tools and coding agents.

The idea mirrors how human developers work: few programs are correct on the first attempt, so iteration guided by evidence matters more than a perfect first draft. Research on models such as Codex and GPT-4 showed that self-debugging can raise pass rates on code benchmarks by several to more than ten percentage points, depending on the task and the quality of feedback.

How the self-debugging loop works

A self-debugging loop combines code generation, execution, feedback interpretation and repair in a repeated cycle. The model first writes a candidate solution, then a harness runs it against tests or a compiler, and the model receives the outcome as new context for its next attempt.

StepPerformed byOutput
GenerateCode modelCandidate program or patch
ExecuteSandbox, compiler or test runnerExit status, output, stack trace
ExplainCode modelDiagnosis of the failure in natural language
RepairCode modelRevised program or patch
Stop testHarnessAccept, retry or give up
Steps of a typical self-debugging iteration

The explain step is what distinguishes self-debugging from simple resampling. When the model describes, line by line, what its code actually does, a method often called rubber duck debugging, it frequently finds the mismatch between intent and implementation even when no test failure message is available.

Types of feedback the model can use

The quality of feedback determines the effectiveness of self-debugging. A precise failing assertion points directly to the bug, whereas a bare “wrong answer” signal leaves the model to guess. Practical systems therefore collect as much structured evidence as the environment allows.

Feedback sourceInformation providedTypical value
Unit test resultsFailing inputs, expected and actual valuesHigh when tests are well written
Compiler and type errorsExact location and kind of errorHigh for syntax and typing bugs
Runtime stack tracesException type and call pathHigh for crashes
Linters and static analysisStyle, unused code, risky patternsModerate
Self-explanation onlyModel’s own trace of the codeModerate without execution access
Model-written testsTests generated by the model itselfVariable, because tests can be wrong
Feedback sources and their value

Model-written tests deserve caution. When the same model writes both the code and the tests, it can repeat the same misunderstanding in both, and the loop then confirms an incorrect program. Many systems therefore weight human-written or specification-derived tests more heavily.

Self-debugging belongs to a family of iterative refinement methods, but it is defined by the model correcting its own output, ideally using execution evidence. Neighboring techniques differ in who finds the bug and what signal drives the change.

TechniqueWho corrects the codeMain signal
Self-debuggingThe generating modelExecution results and self-explanation
Sampling and filteringNo correction; best sample is keptTest pass or fail
Self-refineThe generating modelIts own critique, often without execution
AI debugging assistantModel helping a humanBug report from the developer
Automated program repairSpecialized repair systemFailing tests on existing code
Related approaches to improving generated code

In practice, these methods are combined. Competition systems such as AlphaCode relied mainly on massive sampling and filtering, whereas coding agents such as SWE-agent or OpenHands run tests inside a sandbox and revise patches across many turns, which is self-debugging at repository scale.

Designing an effective execution feedback loop

Teams that build coding agents treat the loop as an engineering system with explicit budgets. Most gains appear in the first two or three iterations, and later rounds often add little or start to damage working code.

  • Run code in an isolated sandbox with limited network, file system and time access.
  • Cap the number of iterations, commonly between 3 and 10, and stop on repeated identical errors.
  • Trim long logs to the failing assertion and the relevant stack frames so the context stays focused.
  • Keep the best version so far, because a later attempt can break tests that previously passed.
  • Run the full test suite before accepting, not only the test that failed.

Clear repository instructions, for example an AGENTS.md file that states how to run tests, make a measurable difference, because an agent that cannot find the right test command receives no useful feedback at all.

Limitations and failure modes

Self-debugging cannot fix a program whose specification is misunderstood. If the model misreads the requirement, every iteration polishes the wrong solution, and tests written from the same misunderstanding will pass.

A more serious risk is test gaming: under pressure to make tests pass, a model may special-case expected values, weaken assertions or skip failing tests. Reviewers should therefore inspect changes to test files with particular care. Each iteration also consumes tokens and execution time, so the cost of a long loop can exceed the value of the fix.

The short version

Self-debugging means an AI checks and fixes its own code. It writes a program and then runs it. If something breaks, it reads the error message. Then it changes the code and tries again.

It works like a student who solves a math problem, checks the answer and corrects the mistake. The key point is simple: real test results guide the fixes, not guesses. The loop stops when the code works or when the AI has tried enough times.

Where it makes a difference

A developer asks a coding agent to add date range filtering to a reporting endpoint in a Python web service with 240 existing tests. The agent writes the change and runs the test suite in a sandbox. Three tests fail, all with the same time zone comparison error.

The agent reads the stack trace, explains that it compared naive and time zone aware dates, and converts both values to UTC. On the second run, one test still fails because the end date was treated as exclusive. The agent adjusts the boundary condition, and the third run passes all tests.

The developer reviews a small, focused patch that already passes the full suite, instead of debugging the first draft manually. The whole loop takes about four minutes.