[KongchangAI]
· 2 min read· 1,373 words

LangGraph Self-Correction Loop in Action: How AI Recovers from Bad Patches

LangGraph Self-Correction Loop in Action: How AI Recovers from Bad Patches

prbot uses a three-agent LangGraph loop for code self-correction, fixing 3/3 bugs in the demo.

This article analyzes prbot, an open-source LangGraph multi-agent code repair app on Reddit. With a reviewer, a ReAct patcher, and a Docker pytest tester, it forms a self-correction loop of up to 3 attempts via a conditional back-edge. Key designs include failure-history prompts that force strategy shifts, dual-channel structured review, AST hotspot hints, and baseline testing with last-ditch scope reduction. On the demo PR, gpt-4.1-mini fixed all 3 bugs in 24.7 seconds for $0.008; the first patch regressed but the second corrected it. Limitations include reliance on predefined test suites and a limited loop count.

Multi-agent systems often get stuck in a simple "generate-fail-retry" loop on code repair tasks, but few can actually adjust their strategy based on failure feedback. Recently, a developer shared a LangGraph application called prbot on Reddit. It builds a self-correction loop with conditional back-edges using three agents (a reviewer, a ReAct patcher, and a Docker pytest tester), and on a demo PR it successfully recovered from a bad patch. This article takes a deep dive into its design and implementation details.

reddit source: Built a LangGraph self-correction loop that actually recovered from its own bad patch (with logs)

System Architecture: Three Agents and a Conditional Back-Edge

The core of prbot is a LangGraph graph with three main nodes: reviewer analyzes the code and outputs structured findings; patcher is a ReAct agent that generates patches based on the review results; tester runs pytest in a Docker container to validate the patch. The key design is the conditional edge from tester to patcher—when tests fail, the flow returns to patcher with the error information to try again, up to a maximum of 3 loops.

This architecture differs from a simple "retry with error" approach, and the developer highlighted several design choices that make the loop more effective. First, the state maintains an attempted_patches list; each failed diff and the tail of its test output are injected into patcher's system prompt, along with the instruction "pivot to a structurally different solution." Second, the write_patch tool rejects completely duplicate patches to prevent going in circles.

LangGraph is a framework released by the LangChain team for building stateful, multi-participant applications. Its core abstraction is the "graph": nodes represent computation steps (such as calling an LLM or a tool), edges define control flow, and conditional edges allow dynamically choosing the next node based on the current state. Unlike linear chains, LangGraph supports cycles, which makes iterative processes like "generate-test-fail-retry" explicitly modelable. The shared state in the graph is a persistable dictionary that each node reads and updates, and conditional edges route based on fields in the state (such as whether tests passed and how many attempts have been made). prbot uses exactly this mechanism: it takes the failure result of the tester node as a condition and sends control flow back to patcher, forming a self-correction loop with an attempt cap.

ReAct (Reasoning + Acting) is an agent paradigm that has language models alternate between reasoning and tool calls. At each step, the model first outputs a thought (Thought), then selects an action (Action) to call an external tool, then observes the tool's returned result (Observation), and continues reasoning based on it. This loop enables the model to dynamically gather information, validate hypotheses, and adjust plans. In prbot, patcher is implemented as a ReAct agent, meaning it can call tools (such as reading files or searching code) to gather context before generating a patch, and after writing the patch it can decide based on tool feedback whether to keep modifying, rather than outputting the full diff in one shot.

Four Key Designs That Make Self-Correction More Effective

1. Failure History Drives a Strategy Shift

Ordinary retries just throw the error message back to the model, but prbot uses historical failed patches and test output as context and explicitly requires the model to change its approach. This forces the model out of local optima and to explore different implementation paths. In the demo, the first attempt failed the sorting test because of an ordering error (the seen-set returned ['b','a'] instead of ['a','b']), and the second attempt passed.

2. Dual-Channel Structured Review

reviewer does not perform a single review; instead, it performs two focused structured outputs: one for security and one for performance. The developer explained that a single review often stops at the most severe bug, while dual-channel review covers more dimensions and gives patcher more comprehensive repair clues.

3. AST Hotspot Hints as Guidance

The system analyzes the code's abstract syntax tree (AST), identifies potential hotspots (such as queries inside loops or nested loops over the same iterable), and places these hints in the context as clues for the model to confirm. This is equivalent to giving the model "suspects," increasing the probability of finding deeper issues.

Abstract Syntax Tree (AST) is a tree representation of the syntactic structure of source code, where each node corresponds to a syntactic construct (such as a function definition, loop, conditional, or function call). Unlike raw text, an AST strips away formatting and comments, making the program's structural relationships explicit, so it is well suited for static analysis. prbot traverses the AST to identify patterns such as "a database query inside a loop body" or "nested loops over the same iterable," which are often breeding grounds for performance problems. Providing these patterns to the model as "suspect" hints is equivalent to using lightweight static analysis to narrow the model's search space so it does not have to guess from scratch where the problem is. However, such rule libraries can only cover known patterns and may fail on novel or domain-specific anti-patterns.

4. Baseline Tests and a Last-Ditch Strategy

Before starting repairs, a baseline node first runs the test suite; if it already fails, the process aborts immediately, avoiding wasted effort on a broken foundation. On the final attempt, the system narrows the scope to the most severe findings, ensuring at least a partially validated fix rather than nothing at all.

Measured Performance: 3/3 Bugs Fixed at Low Cost

On the demo PR using the gpt-4.1-mini model, prbot successfully found all 3 bugs. The whole process took 24.7 seconds, consumed about 17k tokens, and cost only $0.008. Although the first patch introduced a regression (a sorting error), the loop corrected it on the second attempt. This result demonstrates the potential of self-correction loops in real scenarios—not only must errors be found, but the system must also learn from them and adjust its strategy.

The developer open-sourced the full repository, including a graph diagram and telemetry data (https://github.com/ayanag5076-commits/prbot), and welcomes community questions about graph design and sandbox isolation.

gpt-4.1-mini is a smaller-scale model released by OpenAI, positioned for cost- and latency-sensitive scenarios. Compared with flagship models, it makes trade-offs in complex reasoning but remains usable for tasks such as structured output, tool calling, and code generation. prbot's choice of this model for the demo shows that the self-correction loop design can achieve acceptable results without relying on the most expensive models—the loop itself (failure history, structured review, AST hints) carries part of the "reasoning burden," thereby reducing dependence on single-shot model capability. This also explains why the total cost can be kept at $0.008: the small model has a low unit price, the number of loops is limited, and token consumption is effectively constrained.

Takeaways and Limitations

prbot's design provides reusable patterns for multi-agent code repair: history-aware retries, structured review, static-analysis guidance, and progressively narrowing scope. However, the current implementation still relies on predefined test suites, and the loop count is limited (3 times); more complex tasks may require more flexible termination conditions. In addition, the accuracy of AST hints depends on the coverage of the rule library. Overall, this case demonstrates that a carefully designed self-correction loop can significantly improve the reliability of AI code repair, and proper cost control makes it practically valuable.

One-line Summary

prbot uses a three-agent LangGraph loop to achieve code self-correction, fixing 3/3 bugs in the demo.

Paragraph Summary

This article analyzes prbot, an open-source multi-agent code repair application on Reddit based on LangGraph. It consists of three nodes—a reviewer, a ReAct patcher, and a Docker pytest tester—and forms a self-correction loop of up to 3 attempts through a conditional back-edge from the tester to the patcher. The key designs that make the loop effective include: injecting failed patch history into the prompt and requiring a strategy shift, dual-channel structured review, AST hotspot hints, and baseline tests plus a last-ditch scope reduction. On the demo PR, gpt-4.1-mini fixed all 3 bugs in 24.7 seconds at a cost of $0.008; the first patch introduced a regression but the second corrected it. The article also points out limitations such as reliance on predefined test suites and a limited number of loops.

#LangGraph#self-correction loop#multi-agent systems#code repair#ReAct agent#AST analysis#gpt-4.1-mini
Share:

Related articles