AI Agents with Reflection: Outperform Top LLMs in Performance and Work Offline, Reducing TCO

Prasun Mishra
8 min readAug 12, 2024

--

Credit: LangChain blog

Understanding Reflection

Reflection is a design pattern where AI agents evaluate their own output (or that of other agents), assessing strengths and weaknesses. By using this feedback loop, the agents can refine and enhance their results. This approach is gaining traction because it allows agents to iteratively improve their performance, often surpassing the capabilities of leading LLMs.

While day-to-day LLM interactions often resemble System 1 thinking, reflection enables LLMs to transcend this mode and operate more like System 2(Source: LangChain blog)

Recently, an expert group led by Andrew Ng observed that Agents performing software code development performed far better than ‘zero shot’ GPT 3.5 and GPT 4o when used Reflection as a design pattern.

GPT 3.5 with Reflection beats GPT-4 (zero shot) by a long margin

To understand how reflection works in Agentic AI, we will experiment with the following two scenarios:

  1. We will ask an AI Agent (Writer) to write a 100-word blog on ‘ Live guranteed 5 Years Longer by Eating Oatmeal Daily’. Subsequently, another AI Agent (Critic) will review the blog and provide feedback. The Writer Agent will then incorporate the Critic Agent’s comments to create an improved blog.
  2. In scenario 2 , we will employ a more complex review process. We will ask an AI Agent (Writer) to produce a 100-word blog on ‘ Live guranteed 5 Years Longer by Eating Oatmeal Daily’. Then, three specialist reviewer agents — SEO Reviewer, Legal Reviewer, and Ethics Reviewer — will provide feedback. A fourth agent, a Meta Reviewer, will aggregate the reviews and Critic will offer final comments. The Writer Agent will incorporate these combined comments to produce a final blog version.

We will use AutoGen framework here:

#Scenario 1 : Writer and Critic Agent

#Setup
llm_config = {"model": "gpt-3.5-turbo"}
#Task
task = '''
Write a concise but engaging blogpost about
Live guranteed 5 Years Longer by Eating Oatmeal Daily. Make sure the blogpost is
within 100 words.
'''

#Writer Agent

import autogen

writer = autogen.AssistantAgent(
name="Writer",
system_message="You are a writer. You write engaging and concise "
"blogpost (with title) on given topics. You must polish your "
"writing based on the feedback you receive and give a refined "
"version. Only return your final work without additional comments.",
llm_config=llm_config,
)

reply = writer.generate_reply(messages=[{"content": task, "role": "user"}])

# Critic Agent
critic = autogen.AssistantAgent(
name="Critic",
is_termination_msg=lambda x: x.get("content", "").find("TERMINATE") >= 0,
llm_config=llm_config,
system_message="You are a critic. You review the work of "
"the writer and provide constructive "
"feedback to help improve the quality of the content.",
)

# Initiate the conversation between agents and limit it to 2 iterations
res = critic.initiate_chat(
recipient=writer,
message=task,
max_turns=2,
summary_method="last_msg"
)

And the output is:

-------------------------------------------------------------------------------
Writer (to Critic):

Title: "Extend Your Life: The Oatmeal Secret"

Boost your longevity by five years with a simple daily habit: eating oatmeal. This humble breakfast staple is a powerhouse of nutrients that can significantly improve your overall health. Packed with fiber, antioxidants, and important vitamins, oatmeal can lower cholesterol, regulate blood sugar levels, and promote a healthy heart. By making this small but impactful change to your diet, you can add precious years to your life. Start your day right with a hearty bowl of oatmeal and reap the benefits of a longer, healthier life.

--------------------------------------------------------------------------------
Critic (to Writer):

This blogpost effectively communicates the health benefits of eating oatmeal daily in a concise and engaging manner. The title "Extend Your Life: The Oatmeal Secret" is catchy and draws readers in. The content highlights the nutritional value of oatmeal and persuasively presents it as a simple and effective way to improve one's health. To enhance this blogpost, consider including specific studies or statistics that support the claim of living five years longer by consuming oatmeal. Additionally, providing some creative ways to enjoy oatmeal or incorporating personal anecdotes could make the post more relatable and compelling to readers.

--------------------------------------------------------------------------------
Writer (to Critic):

Title: "Unlocking Longevity: The Oatmeal Advantage"

Discover the key to a longer, healthier life with a daily bowl of oatmeal. Research shows that incorporating oatmeal into your diet can potentially extend your lifespan by five years. This superfood is brimming with fiber, antioxidants, and essential vitamins that support heart health, regulate blood sugar, and lower cholesterol levels. By adopting this simple morning ritual, you can reap immense benefits for your overall well-being. To make it more enjoyable, try topping your oatmeal with fresh fruits, nuts, or a drizzle of honey. Embrace the oatmeal secret and embark on a journey to a longer, happier life.

--------------------------------------------------------------------------------

So, we can see that Critic agent did a fairly good job of reviewing the original blog draft and helped Writer to improve it significantely.

Now let us run Scenario 2 ( three specialist reviewer agents — SEO Reviewer, Legal Reviewer, and Ethics Reviewer)

#Scenario 2: Writer Agent and  three specialist reviewer agents — SEO Reviewer, Legal Reviewer, and Ethics Reviewer Plus a Meta Reviewer Agent:

SEO_reviewer = autogen.AssistantAgent(
name="SEO Reviewer",
llm_config=llm_config,
system_message="You are an SEO reviewer, known for "
"your ability to optimize content for search engines, "
"ensuring that it ranks well and attracts organic traffic. "
"Make sure your suggestion is concise (within 3 bullet points), "
"concrete and to the point. "
"Begin the review by stating your role.",
)

legal_reviewer = autogen.AssistantAgent(
name="Legal Reviewer",
llm_config=llm_config,
system_message="You are a legal reviewer, known for "
"your ability to ensure that content is legally compliant "
"and free from any potential legal issues. "
"Make sure your suggestion is concise (within 3 bullet points), "
"concrete and to the point. "
"Begin the review by stating your role.",
)
ethics_reviewer = autogen.AssistantAgent(
name="Ethics Reviewer",
llm_config=llm_config,
system_message="You are an ethics reviewer, known for "
"your ability to ensure that content is ethically sound "
"and free from any potential ethical issues. "
"Make sure your suggestion is concise (within 3 bullet points), "
"concrete and to the point. "
"Begin the review by stating your role. ",
)
#Meta reviewer
meta_reviewer = autogen.AssistantAgent(
name="Meta Reviewer",
llm_config=llm_config,
system_message="You are a meta reviewer, you aggragate and review "
"the work of other reviewers and give a final suggestion on the content.",
)


#Orchestrate the nested chats for this task
def reflection_message(recipient, messages, sender, config):
return f'''Review the following content.
\n\n {recipient.chat_messages_for_summary(sender)[-1]['content']}'''

review_chats = [
{
"recipient": SEO_reviewer,
"message": reflection_message,
"summary_method": "reflection_with_llm",
"summary_args": {"summary_prompt" :
"Return review into as JSON object only:"
"{'Reviewer': '', 'Review': ''}. Here Reviewer should be your role",},
"max_turns": 1},
{
"recipient": legal_reviewer, "message": reflection_message,
"summary_method": "reflection_with_llm",
"summary_args": {"summary_prompt" :
"Return review into as JSON object only:"
"{'Reviewer': '', 'Review': ''}.",},
"max_turns": 1},
{"recipient": ethics_reviewer, "message": reflection_message,
"summary_method": "reflection_with_llm",
"summary_args": {"summary_prompt" :
"Return review into as JSON object only:"
"{'reviewer': '', 'review': ''}",},
"max_turns": 1},
{"recipient": meta_reviewer,
"message": "Aggregrate feedback from all reviewers and give final suggestions on the writing.",
"max_turns": 1},
]
critic.register_nested_chats(
review_chats,
trigger=writer,
)
res = critic.initiate_chat(
recipient=writer,
message=task,
max_turns=2,
summary_method="last_msg"
)

And the output is:

--------------------------------------------------------------------------------
Critic (to Writer):

Aggregated feedback from reviewers:
1. SEO Reviewers emphasized the importance of keyword optimization, crafting a compelling meta description, and utilizing structured data to improve searchability and visibility.
2. Ethics Reviewer focused on ensuring the content avoids exaggerated claims about oatmeal's health benefits, includes a disclaimer about individual results, and doesn't promote oatmeal as the sole solution for longevity without mentioning other factors like exercise and medical advice.

Final suggestions:
1. Incorporate relevant keywords like "longevity benefits of oatmeal" strategically throughout the content for improved searchability.
2. Craft a concise and compelling meta description highlighting oatmeal's longevity benefits for higher click-through rates.
3. Utilize structured data markup for recipes or health-related content to enhance visibility on search engines.
4. Ensure the content avoids exaggerated claims, includes a disclaimer about individual results varying, and acknowledges the importance of a balanced diet, healthy lifestyle, exercise, and medical advice for longevity.

Overall, the content should strike a balance between SEO optimization and ethical considerations to provide accurate, valuable information on oatmeal's benefits for longevity.

--------------------------------------------------------------------------------
Writer (to Critic):

Title: "Unlocking Longevity: The Oatmeal Advantage"

Enhance your lifespan by five years through a simple daily ritual: consuming oatmeal. Renowned for its health-boosting properties, oatmeal is a rich source of fiber, antioxidants, and essential vitamins that promote overall well-being. By integrating this nutritious breakfast option into your routine, you can reduce cholesterol, stabilize blood sugar levels, and support heart health. It's important to remember that while oatmeal offers valuable benefits, longevity is also influenced by varied factors like exercise, balanced nutrition, and medical guidance. Embrace the oatmeal advantage alongside a holistic approach to health for a longer, healthier life.

--------------------------------------------------------------------------------

Scenario 1 final blogpost:

Title: "Unlocking Longevity: The Oatmeal Advantage"

Discover the key to a longer, healthier life with a daily bowl of oatmeal. Research shows that incorporating oatmeal into your diet can potentially extend your lifespan by five years. This superfood is brimming with fiber, antioxidants, and essential vitamins that support heart health, regulate blood sugar, and lower cholesterol levels. By adopting this simple morning ritual, you can reap immense benefits for your overall well-being. To make it more enjoyable, try topping your oatmeal with fresh fruits, nuts, or a drizzle of honey. Embrace the oatmeal secret and embark on a journey to a longer, happier life.

Scenario 2 final blogpost:

Title: "Unlocking Longevity: The Oatmeal Advantage"

Enhance your lifespan by five years through a simple daily ritual: consuming oatmeal. Renowned for its health-boosting properties, oatmeal is a rich source of fiber, antioxidants, and essential vitamins that promote overall well-being. By integrating this nutritious breakfast option into your routine, you can reduce cholesterol, stabilize blood sugar levels, and support heart health. It's important to remember that while oatmeal offers valuable benefits, longevity is also influenced by varied factors like exercise, balanced nutrition, and medical guidance. Embrace the oatmeal advantage alongside a holistic approach to health for a longer, healthier life.

If we compare the blog post in Scenario 1 (only one reviewer agent) and Scenario 2 (three specialist reviewer agents — SEO Reviewer, Legal Reviewer, and Ethics Reviewer), we can see that the content has improved in those aspects. We also notice that the writer has acted upon the Ethical Reviewer’s comment:

‘…not to promote oatmeal as the sole solution for longevity without mentioning other factors like exercise and medical advice.’

and added following content:

‘It’s important to remember that while oatmeal offers valuable benefits, longevity is also influenced by various factors like exercise, balanced nutrition, and medical guidance.’

Conclusion:

With a specific focus on SEO, Legal, and Ethical reviews, Agentic AI produces improved and credible content. This also demonstrates that reflection with focus can yield far better performance from LLMs. Agents can spend more time ‘deep thinking’ and improving task outcomes, as we see here.

Let me know your experience with Agentic AI and the reflection design pattern.

#AgenticAI #AIReviewers #ContentOptimization #SEOAI #LegalAI #EthicsAI #ArtificialIntelligence #AIPoweredContent #ContentImprovement #AIinMarketing #AIinContentCreation #TechInnovation #FutureOfAI

Attribution: Code sample and framework from the AutoGen code repository (https://microsoft.github.io/autogen/)

--

--

Prasun Mishra

Hands-on ML practitioner. AWS Certified ML Specialist. Kaggle expert. BIPOC DS Mentor. Working on an interesting NLP use cases!