Self-correction

Self-correction combines elements of reflection with explicit error detection. The LLM generates an initial solution, then reviews it specifically for errors, logical flaws, and improvements to produce a corrected version.

Self-correction loop

Reflection is open-ended critique ("what could be better?"). Self-correction is targeted error hunting ("find specific mistakes and fix them"). Self-correction is typically a single pass focused on correctness rather than an iterative quality loop.

patterns/17b_self_correction.py
python
def self_correction_reasoning(problem, llm):
    # Step 1: Initial attempt
    initial = llm.generate(f"Solve: {problem}").content

    # Step 2: Self-correction
    correction_prompt = f"""
    Problem: {problem}
    Initial solution: {initial}

    Review and identify errors:
    1. Is the solution correct?
    2. Any logical errors?
    3. Can it be improved?

    Provide a corrected solution.
    """
    corrected = llm.generate(correction_prompt).content
    return {"initial": initial, "corrected": corrected}

Two-pass self-correction: initial attempt then error review.

Quiz: Quiz

Loading practice…

Matching exercise: Match Self-correction concepts

Loading practice…

Flashcards: Flashcards

Loading practice…

Self-correction gives your agents the ability to catch and fix their own mistakes. Next, we will learn problem decomposition to break complex challenges into manageable pieces.