Module 12 Lesson 4: Self-Correction Loops
The double-check. Implementing internal loops where one agent reviews and corrects the errors of another.
Self-Correction Loops: The Internal Audit
If you want 99% accuracy, you cannot trust a single "pass" of an LLM. Even the smartest models make mistakes. The Self-Correction Loop is an architecture where the agent is asked to review its own work (or another agent reviews it) before it is finalized.
1. The "Reflection" Pattern
Reflection is the simplest form of self-correction.
- Agent: Generates an answer.
- System Prompt: "Review your answer above. Are there any factual errors? Is the tone correct? If there are issues, provide a corrected version. If it's perfect, repeat the answer."
This small addition often catches math errors and tone inconsistencies that would have otherwise gone to the user.
2. Multi-Agent Correction
A better version is Adversarial Correction.
- Agent A (The Writer): Creates the response.
- Agent B (The Fact-Checker): Searches the web to verify every claim made by Agent A.
- Loop: If Agent B finds a lie, it tells Agent A to rewrite.
3. Visualizing the Audit
graph TD
Start[User Query] --> Gen[Generator: Draft Response]
Gen --> Audit[Auditor: Check for Flaws]
Audit --> Path{Found Errors?}
Path -- Yes --> Logic[Logic: Feed errors back]
Logic --> Gen
Path -- No --> Final[Success: Return Result]
4. Cost vs. Quality Trade-off
Self-correction loops are expensive. You are essentially doubling or tripling your token usage for every single query.
- High-Stakes (Billing/Medical): Use a loop.
- Low-Stakes (Casual Chat): Skip the loop.
5. Engineering Tip: Specific Checklists
Don't just ask the agent to "Review." Give it a Criteria Checklist. *"Verify the final JSON contains:
- All three requested fields.
- Correct capitalization of Names.
- No information outside of the provided source text."*
Specificity in the auditor's prompt is the secret to high-quality correction.
Key Takeaways
- Self-Correction is required for production-level reliability.
- Reflection allows an agent to catch its own probabilistic errors.
- Checklists guide the auditor to focus on specific high-risk areas.
- Always budget for increased costs and latency when adding correction loops.