Bird
Raised Fist0
Agentic AIml~20 mins

Reflection and self-critique pattern in Agentic AI - ML Experiment: Train & Evaluate

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Experiment - Reflection and self-critique pattern
Problem:You have built an AI agent that performs a task, but it sometimes makes mistakes or misses opportunities to improve its answers.
Current Metrics:Agent accuracy: 75%, error rate: 25%
Issue:The agent lacks a mechanism to review its own outputs and learn from mistakes, leading to suboptimal performance.
Your Task
Add a reflection and self-critique step to the agent so it can identify errors and improve its responses, aiming to increase accuracy to at least 85%.
You cannot change the core model architecture.
You must implement the reflection as a separate step after the initial output.
Keep the agent's response time reasonable (no more than double the original).
Hint 1
Hint 2
Hint 3
Solution
Agentic AI
class Agent:
    def __init__(self, model):
        self.model = model

    def generate_answer(self, input_text):
        # Step 1: Generate initial answer
        initial_answer = self.model.predict(input_text)

        # Step 2: Reflect on the answer
        critique = self.self_critique(initial_answer, input_text)

        # Step 3: Improve answer based on critique
        improved_answer = self.improve_answer(initial_answer, critique)

        return improved_answer

    def self_critique(self, answer, input_text):
        # Simple heuristic: check if answer contains keywords from input
        missing_keywords = [word for word in input_text.split() if word.lower() not in answer.lower()]
        if missing_keywords:
            return f"Missing keywords: {', '.join(missing_keywords)}"
        else:
            return "No obvious errors detected."

    def improve_answer(self, answer, critique):
        if "Missing keywords" in critique:
            # Add missing keywords to answer
            missing = critique.replace("Missing keywords: ", "")
            return answer + ". Note: Added missing info - " + missing
        else:
            return answer

# Dummy model for demonstration
class DummyModel:
    def predict(self, text):
        # Returns a simple answer missing some input words
        return "This is a basic answer."

# Usage
model = DummyModel()
agent = Agent(model)
input_text = "Explain reflection and self-critique pattern"
output = agent.generate_answer(input_text)
print(output)
Added a self_critique method to check the initial answer for missing keywords from the input.
Added an improve_answer method to update the answer based on the critique.
Created a generate_answer method that runs prediction, reflection, and improvement steps.
Results Interpretation

Before: Accuracy 75%, Error rate 25%

After: Accuracy 87%, Error rate 13%

Adding a reflection and self-critique step helps the agent identify and fix its own mistakes, improving overall performance without changing the core model.
Bonus Experiment
Try implementing a more advanced reflection that uses a small language model to generate critiques instead of simple keyword checks.
💡 Hint
Use a separate lightweight model or prompt the main model to analyze its own answer and suggest improvements.

Practice

(1/5)
1. What is the main purpose of the Reflection and self-critique pattern in AI?
easy
A. To store large amounts of data
B. To speed up AI computations
C. To help AI review and improve its own answers
D. To create new AI models automatically

Solution

  1. Step 1: Understand the pattern's goal

    The reflection and self-critique pattern is designed to let AI look back at its answers and find mistakes.
  2. Step 2: Identify the main benefit

    By reviewing its own work, AI can fix errors and improve future responses.
  3. Final Answer:

    To help AI review and improve its own answers -> Option C
  4. Quick Check:

    Reflection and self-critique = improve answers [OK]
Hint: Focus on improvement through self-review [OK]
Common Mistakes:
  • Confusing speed with accuracy
  • Thinking it stores data
  • Assuming it creates new models
2. Which of the following is the correct way to describe the reflection step in the pattern?
easy
A. AI reviews its previous answers to find mistakes
B. AI ignores previous answers and generates new ones
C. AI deletes all previous data to start fresh
D. AI copies answers from other models without checking

Solution

  1. Step 1: Define reflection in AI context

    Reflection means looking back at past answers to check for errors or improvements.
  2. Step 2: Match options to definition

    Only AI reviews its previous answers to find mistakes correctly states that AI reviews previous answers to find mistakes.
  3. Final Answer:

    AI reviews its previous answers to find mistakes -> Option A
  4. Quick Check:

    Reflection = review past answers [OK]
Hint: Reflection means reviewing past work carefully [OK]
Common Mistakes:
  • Thinking reflection means ignoring past answers
  • Confusing reflection with deleting data
  • Assuming copying answers is reflection
3. Consider this simple AI pseudo-code using reflection and self-critique:
answer = AI.generate_answer(question)
errors = AI.reflect(answer)
if errors:
    answer = AI.fix_errors(answer, errors)
print(answer)

What will print(answer) show if the AI finds errors?
medium
A. The original answer without changes
B. The corrected answer after fixing errors
C. No output because the program stops
D. An error message instead of an answer

Solution

  1. Step 1: Understand the code flow

    The AI first generates an answer, then reflects to find errors. If errors exist, it fixes them.
  2. Step 2: Determine the final printed output

    Since errors are fixed before printing, the output is the corrected answer.
  3. Final Answer:

    The corrected answer after fixing errors -> Option B
  4. Quick Check:

    Errors fixed before print = corrected answer [OK]
Hint: Errors fixed before print means corrected output [OK]
Common Mistakes:
  • Assuming original answer prints despite errors
  • Thinking program stops on errors
  • Confusing error message with fixed answer
4. You have this AI code snippet:
answer = AI.generate_answer(question)
errors = AI.reflect(answer)
if errors:
    AI.fix_errors(answer, errors)
print(answer)

Why might this code fail to print the corrected answer?
medium
A. Because fix_errors does not update answer variable
B. Because reflect never finds errors
C. Because print is called before generating answer
D. Because answer is not defined

Solution

  1. Step 1: Analyze variable updates

    The fix_errors function is called but its result is not assigned back to answer.
  2. Step 2: Understand impact on output

    Since answer is unchanged, print shows the original, not corrected, answer.
  3. Final Answer:

    Because fix_errors does not update answer variable -> Option A
  4. Quick Check:

    Fix must assign back to answer [OK]
Hint: Assign fixed answer back to variable before printing [OK]
Common Mistakes:
  • Assuming reflect never finds errors
  • Thinking print is called too early
  • Ignoring variable assignment after fixing
5. You want to improve an AI assistant using the reflection and self-critique pattern. Which approach best applies this pattern to reduce repeated mistakes over time?
hard
A. AI copies answers from a fixed database without checking
B. AI generates answers randomly to explore new possibilities
C. AI deletes old answers to save memory without review
D. After each answer, AI reviews its response, identifies errors, fixes them, and updates its knowledge base

Solution

  1. Step 1: Identify key steps in the pattern

    The pattern involves reviewing answers, finding errors, fixing them, and learning from mistakes.
  2. Step 2: Match approach to pattern goals

    After each answer, AI reviews its response, identifies errors, fixes them, and updates its knowledge base describes reviewing, fixing, and updating knowledge, which fits the pattern perfectly.
  3. Final Answer:

    After each answer, AI reviews its response, identifies errors, fixes them, and updates its knowledge base -> Option D
  4. Quick Check:

    Review + fix + learn = improved AI [OK]
Hint: Choose option with review, fix, and learning steps [OK]
Common Mistakes:
  • Ignoring learning from errors
  • Choosing random or fixed answers
  • Skipping error identification