How to Add a Self-Correcting Loop to Your First LangGraph Agent

November 14, 2026

LangGraph models an agent as a graph: nodes do work, edges decide what happens next. The feature that separates it from a plain chain is the cycle — an edge that points backwards so the agent can retry.

A self-correcting loop is the simplest graph that shows why that matters. One node drafts, one node critiques, a conditional edge sends the draft back until the critique passes. About 40 lines of Python.

What does the minimal graph look like?

Three nodes and one conditional edge.

  • draft — calls the model to write an answer.
  • critique — calls the model to review the draft against a rubric, returning pass or fail plus notes.
  • revise — rewrites the draft using the notes.

The conditional edge after critique goes to revise on fail, and to END on pass. revise loops back to critique.

That is it. No framework beyond langgraph and langchain_openai.

How do you define the state?

State is a TypedDict that every node reads and updates. Keep it flat and explicit.

from typing import TypedDict, Annotated
from operator import add

class State(TypedDict):
    task: str
    draft: str
    notes: str
    attempts: Annotated[int, add]

Annotated[int, add] makes the counter accumulate across node runs instead of being overwritten. This matters more than it looks: without an accumulator, an infinite loop never trips your cap.

How do you stop the loop from running forever?

Two guards, both necessary.

  1. Attempt cap in the conditional edge. Route to END once attempts >= 3, regardless of the critique.
  2. Recursion limit on invoke. graph.invoke(state, {"recursion_limit": 12}). LangGraph raises GraphRecursionError when it hits this, which is your backstop.

The attempt cap handles normal cases. The recursion limit handles the case where you accidentally build a cycle with no exit, which is easy to do when you add nodes later.

What makes the critique node reliable?

The critique is the whole graph. If it always says pass, you have built a slow wrapper around one model call.

Three things help:

  • A written rubric with numbered criteria, not “is this good”. Example: “1. answers the question 2. cites a number 3. under 150 words”.
  • Structured output. Ask for JSON with pass boolean and notes string. Parsing prose for a verdict fails often.
  • A cheaper model for the critic. A small model checks three criteria about as well as a large one, and you call it on every loop.

Print every critique during development. If the pass rate is above 90 percent on the first attempt, the rubric is too loose.

How much does the loop cost?

Each cycle is two model calls minimum (critique plus revise) plus the draft. With three allowed attempts, worst case is seven calls for one answer.

On a mid-tier model at typical prices, that is a few cents per difficult query and well under a cent for easy ones. The cap is what makes the cost predictable — without it, a confused critic can loop 40 times.

What does this pattern not solve?

Factual accuracy. A model critiquing its own output shares its blind spots. If the draft invents a statistic, the critic may approve it.

Long outputs. Each loop regenerates the whole draft, so a 3000-word document costs three times as much per cycle.

Real-world actions. If the node sends an email instead of writing text, you need a human-in-the-loop interrupt before execution. LangGraph supports that with interrupt_before, and you should use it.

Who is this for?

Developers who already have a working single-call pipeline and want measurable quality gains on structured tasks: report writing, code review comments, data extraction with validation.

It is not a fix for bad prompting at the base level. If the first draft is consistently wrong in the same way, add a better prompt or better context, not a loop.

Quick Summary

  • A self-correcting LangGraph agent is three nodes: draft, critique, revise, with a conditional edge after critique.
  • Use an Annotated[int, add] counter and cap attempts at 3, plus recursion_limit as a backstop.
  • Give the critic a numbered rubric and demand JSON with a pass flag; loose rubrics produce rubber-stamp approvals.
  • Worst case is about 7 model calls per answer with a 3-attempt cap, so cost stays predictable.
  • Self-critique does not catch factual errors the model is blind to, and needs interrupt_before before real-world actions.