0PricingLogin
AI Agents · Lesson

Reflection and Self-Critique Loops

Agents that evaluate their own outputs and generate improvement suggestions.

What Is Agent Self-Reflection?

Self-reflection is the practice of asking the agent to evaluate its own just-completed output before returning it to the user, or immediately after. The agent acts as its own critic.

This mirrors how expert humans review their work: draft → critique → revise. Adding this loop to agents often improves output quality with no changes to the underlying model.

The Reflection Prompt Pattern

After the agent produces a response, feed the response back into the model with a structured reflection prompt. The model then identifies weaknesses and suggests improvements.

import anthropic

client = anthropic.Anthropic(api_key='YOUR_API_KEY')

def reflect_on_response(task: str, response: str) -> str:
    reflection_prompt = (
        'You just completed the following task:\n\n'
        f'TASK: {task}\n\n'
        f'YOUR RESPONSE:\n{response}\n\n'
        'Please reflect on your performance by answering:\n'
        '1. What did you do well?\n'
        '2. What could be improved?\n'
        '3. What would you do differently if you had to redo this?\n'
        'Be specific and honest.'
    )
    result = client.messages.create(
        model='claude-opus-4-5',
        max_tokens=512,
        messages=[{'role': 'user', 'content': reflection_prompt}]
    )
    return result.content[0].text

All lessons in this course

  1. Feedback Collection and Storage
  2. Reflection and Self-Critique Loops
  3. Trajectory-Based Self-Improvement
  4. When Self-Improvement Goes Wrong
← Back to AI Agents