Why Reflection Is So Important

When humans tackle complex tasks, they rarely get it right in one go. We draft, check for issues, revise, and check again—this loop of 'write → review → revise → review' is the essence of Reflection. AI needs this capability too: the first output is often flawed, but through self-evaluation and iterative refinement, output quality can be significantly improved.

Research shows that incorporating Reflection mechanisms can boost AI's accuracy by 20%-40% on tasks like code generation, mathematical reasoning, and copywriting. More importantly, Reflection makes AI outputs more reliable—it can detect and correct its own mistakes rather than confidently producing wrong answers.

How Reflection Works

At its core, Reflection is a 'generate → evaluate → refine' loop:

  1. Initial Generation: AI produces an initial output based on the task requirements.
  2. Self-Evaluation: AI critically examines its own output, identifying issues (logical gaps, factual errors, formatting problems, etc.).
  3. Refinement: Based on the evaluation, AI makes targeted corrections to the output.
  4. Iteration: Repeat evaluation and refinement until output quality meets a threshold or maximum iterations are reached.

Key insight: The evaluator and generator can be the same model, but with different role settings. When generating, it acts as a 'creator'; when evaluating, it acts as a 'reviewer'—this role-switching allows the model to examine the same content from different perspectives.

Skill Chain Breakdown: Reflective Self-Correction Chain

Let's use the 'Reflective Self-Correction Chain' skill chain as an example to illustrate the node orchestration of Reflection:

Node 1: Initial Generation (sp-187)—AI acts as a 'creator' to produce the first version. This stage prioritizes completeness and coverage over perfection. The key is to quickly produce an evaluable draft.

Node 2: Self-Evaluation (sp-185)—AI switches to a 'reviewer' role and evaluates the initial output against critical criteria. Evaluation dimensions include: accuracy (factual errors), completeness (missing key information), logic (coherence of reasoning), and readability (clarity of expression).

Node 3: Refinement (sp-181)—Based on the evaluation results, AI makes targeted corrections. Note there is a back-edge from Node 3 to Node 2—the refined output re-enters self-evaluation, forming an iterative 'evaluate → refine → evaluate' loop.

Hands-On: Implementing a Reflection Agent

The following code demonstrates how to implement Reflection in an Agent Loop:

import json
from openai import OpenAI

client = OpenAI(
    api_key="your-deepseek-api-key",
    base_url="https://api.deepseek.com"
)

class ReflectionAgent:
    def __init__(self, max_iterations=3, quality_threshold=0.8):
        self.max_iterations = max_iterations
        self.quality_threshold = quality_threshold

    def generate(self, task, feedback=""):
        """Generation phase: produce content"""
        prompt = f"""You are a professional content creator.
Task: {task}
{"Previous feedback: " + feedback if feedback else ""}
Please generate high-quality output content."""

        response = client.chat.completions.create(
            model="deepseek-chat",
            messages=[{"role": "user", "content": prompt}]
        )
        return response.choices[0].message.content

    def evaluate(self, content, task):
        """Evaluation phase: review content quality"""
        prompt = f"""You are a strict reviewer. Please evaluate the quality of the following content.

Original task: {task}
Content to evaluate:
{content}

Please score on the following dimensions (0-1):
1. Accuracy: Is the information accurate?
2. Completeness: Does it cover all key points?
3. Logic: Is the reasoning coherent?
4. Readability: Is the expression clear?

Please output in JSON format:
{{
  "scores": {{"accuracy": 0.0, "completeness": 0.0, "logic": 0.0, "readability": 0.0}},
  "overall_score": 0.0,
  "issues": ["Issue 1", "Issue 2"],
  "suggestions": "Improvement suggestions"
}}"""

        response = client.chat.completions.create(
            model="deepseek-chat",
            messages=[{"role": "user", "content": prompt}]
        )
        return json.loads(response.choices[0].message.content)

    def refine(self, content, evaluation):
        """Refinement phase: improve content based on evaluation"""
        issues = "\n".join(f"- {issue}" for issue in evaluation["issues"])
        suggestions = evaluation["suggestions"]

        prompt = f"""You are an expert content optimizer striving for perfection.

Original content:
{content}

Issues found:
{issues}

Improvement suggestions:
{suggestions}

Please output the revised content based on the above feedback."""

        response = client.chat.completions.create(
            model="deepseek-chat",
            messages=[{"role": "user", "content": prompt}]
        )
        return response.choices[0].message.content

    def run(self, task):
        """Run the Reflection Loop"""
        content = self.generate(task)
        print(f"Initial generation complete, length: {len(content)} characters")

        for i in range(self.max_iterations):
            print(f"=== Reflection round {i+1} ===")

            evaluation = self.evaluate(content, task)
            overall = evaluation["overall_score"]
            print(f"Quality score: {overall:.2f}")
            print(f"Found {len(evaluation['issues'])} issues")

            if overall >= self.quality_threshold:
                print("Quality threshold met, stopping reflection")
                return content

            content = self.refine(content, evaluation)
            print(f"Refinement complete, new length: {len(content)} characters")

        print("Maximum iterations reached")
        return content

# Usage example
agent = ReflectionAgent(max_iterations=3, quality_threshold=0.85)
result = agent.run("Write a 500-word short essay introducing the basic principles of deep learning")
print(f"\nFinal output:\n{result}")

Advanced Techniques for Reflection

Multi-Dimensional Evaluation: Don't just ask AI for a vague score. Break evaluation into multiple dimensions (accuracy, completeness, logic, readability) and score each independently, so AI can give more specific improvement suggestions.

Specific Feedback: Avoid vague feedback like 'content is not good enough'. Ask AI to point out specific problem locations and suggestions. For example: 'The explanation of gradient descent in paragraph 3 lacks mathematical intuition; consider adding an analogy'.

Avoid Over-Correction: Reflection can lead to excessive modification, degrading quality. Set a reasonable quality threshold (e.g., 0.8-0.9) and stop once met, to avoid 'breaking it'.

Keep Historical Versions: Retain the previous version during each refinement. If the score drops after refinement, you can revert to the earlier version.

Limitations of Reflection

Reflection is not a silver bullet. For tasks requiring external knowledge verification (e.g., fact-checking), AI's self-evaluation may be unreliable—it doesn't know if its 'knowledge' is correct. In such cases, combine with tool calls, allowing AI to verify facts via search. Additionally, Reflection increases token consumption and response latency, so you need to balance quality and cost.

Want to orchestrate this skill chain yourself?

Open in Skill Chain →