Chuyển đến nội dung chính

Lesson 11: Advanced Patterns — Planning, Reflection & Self-Correction

Advanced Pattern: Plan-and-Execute, Tree-of-Thought planning, Self-Reflection loops, Critic-and-Revise. Implement agent evaluates and corrects its own output.

🧠 AI & ML — Lesson 10 Lesson 11: Advanced Patterns — Planning, Reflection & Self-Correction

Build AI Agents: From Zero to Production

Part 4: Agentic Frameworks

xdev.asia

Introduction

Agents basically work in a "think 1 step, do 1 step" style. Advanced agents need to know how to plan, self-evaluate, and self-correct. This article covers the most important design patterns.


1. Plan-and-Execute Pattern

User Request → Planner (tạo plan) → Executor (thực hiện từng step) → Re-planner (adjust)

2. Self-Reflection / Critic Loop

def reflect_and_improve(agent_output, original_task):
    critic_prompt = f"""
    Task: {original_task}
    Agent Output: {agent_output}
    
    Evaluate:
    1. Does it fully answer the task? (completeness)
    2. Is the information accurate? (accuracy)
    3. Is it well-structured? (quality)
    
    If issues found, provide specific improvements.
    """
    feedback = call_llm(critic_prompt)
    
    if "APPROVED" in feedback:
        return agent_output
    else:
        improved = call_llm(f"Improve based on feedback: {feedback}")
        return improved

3. Tree-of-Thoughts

Instead of 1 chain of thought, explore multiple branches of parallel thoughts, then choose the best branch.


Summary

  • Plan-and-Execute: plan first, execute later
  • Self-Reflection: criticism loop improves output quality
  • Tree-of-Thought: explore many reasoning paths
  • Combine patterns for the strongest agent

Exercises

  1. Implement Plan-and-Execute agent
  2. Add self-reflection loop (max 3 iterations)
  3. Compare output quality: with vs without reflection
  4. Implement Tree-of-Thought for math problem solving