0
Phase 4

Self Correction

~3 min read

Concept & How It Works

    Why Does It Exist?

    Agents produce errors: wrong calculations, hallucinated facts, malformed outputs, incomplete task execution. Self-correction catches these before the user sees them — the difference between a reliable production agent and a demo that works 60% of the time.

    Real-World Analogy

    Self-correction is spell-check for agent outputs — catching errors automatically before anyone else sees them, and suggesting fixes.
    Loading diagram...

    Visual Workflows

    What is Self Correction?

    Loading diagram...

    Example

    Scenario

    Agent calculates '15% of $2,400 = $360.' Validator: 2400 × 0.15 = 360 ✓. Critic: 'Answer is correct but user asked for monthly, not total — $360 is annual.' Corrector revises: '$30/month ($360/year).'

    Solution

    In Agent Foundations, apply Self Correction to this scenario: Agent calculates '15% of $2,400 = $360. Identify the inputs, run the technique, validate the output, and note one thing you would monitor in production.

    Practice Task

    Do this before moving to the next module — reading alone is not enough.

    Open the Code Walkthrough below and run it locally. Change one parameter related to Self Correction (e.g. model, temperature, top_k, or tool name), observe the difference in output, and write 2–3 sentences explaining what changed.

    Code Walkthrough

    Highlighted lines show where Self Correction happens in the code.

    Self Correction
    1def self_correcting_agent(task: str, max_corrections: int = 2) -> str:  # define a reusable function2    result = agent_run(task)3
    4    for attempt in range(max_corrections + 1):  # key line for Self Correction5        validation = programmatic_check(result, task)6        if not validation["pass"]:7            result = corrector_llm(task, result, validation["issues"])8            continue9
    10        critique = critic_llm(task, result)11        if critique["accept"]:12            return result  # return the result13
    14        result = corrector_llm(task, result, critique["issues"])15
    16    return result  # best effort after max corrections

    Commands to Remember

    Commands to Remember

    • pip install openai # minimal agent = LLM API + Python loop
    • python agent.py # run your agent script
    • pip install python-dotenv # load API keys from .env

    Common Mistakes

    • No validation — errors reach the user
    • Only LLM critic — slow and expensive for simple checks
    • Infinite correction loops — need max attempts
    • Not cross-checking claims against tool results
    • High correction rate ignored — signals systemic prompt issues

    Cheat Sheet

    Quick recap — the most important points from this module.

    Cheat Sheet

    quick ref
    • Validate → correct → re-validate
    • Programmatic first, LLM second
    • max_corrections=2-3
    • Self-consistency for facts
    • Cross-check tool results
    • Track correction rate