Design Patterns for AI Agents: Using Autogen for Effective Multi-Agent Collaboration

Lakshmi narayana .U
11 min readJun 8, 2024
“AI Symphony, Van Gogh Style’- image created by Author and DALL.E-3

Design Patterns for Building AI Agents: Reflection, Tool Use, Planning, and Multi-Agent Collaboration

One of the key approaches to building effective AI systems is the use of design patterns. These structured solutions address common problems in software design, and in the context of AI, they help in creating robust, efficient, and scalable agent-based systems. Andrew Ng has proposed design patterns specifically for building AI agents — Reflection, Tool Use, Planning, and Multi-Agent Collaboration.

Design Patterns for Building AI Agents

Reflection
Reflection involves prompting an AI agent to critique and improve its own output. Instead of producing a final output in one step, the agent iteratively evaluates its work, identifies potential flaws, and makes necessary adjustments. This process significantly enhances the quality of the agent’s responses. You might have experienced prompting ChatGPT, Claude, or Gemini, receiving unsatisfactory output, providing critical feedback, and then getting a better response. Reflection automates this feedback loop, allowing the model to criticize its own output and improve it, either by the same agent or a special critic agent.

For example, an AI agent can generate a piece of code, reflect on its correctness and efficiency, and then revise it based on self-critique, resulting in more reliable and optimized code. Similarly, in content creation, an AI can draft an article, review it for clarity and coherence, and improve it through successive iterations.

According to Madaan et al., “The model was able to autonomously detect inconsistencies in its own outputs and propose more accurate alternatives, demonstrating the potential of self-refinement for enhancing model reliability” (p. 4). Shinn et al. add that “Verbal reinforcement learning allowed the agents to improve their dialogue coherence and context awareness significantly over multiple iterations” (p. 6).

Reflection can be enhanced by equipping the LLM with tools to evaluate its output, such as running code through unit tests to check for correctness or searching the web to verify text accuracy. This allows the model to reflect on errors found and come up with ideas for improvement, potentially using a multi-agent framework for more complex evaluations.

Tool Use
Tool Use equips AI agents with the capability to utilize external tools to perform tasks, ranging from web searches to data retrieval and code execution. This significantly extends the agents’ capabilities beyond simple text generation. For instance, an AI agent can fetch and analyze data using tools like web scrapers, APIs, and data processing libraries, making it particularly useful for tasks requiring extensive data manipulation and analysis.

Developers use functions to search different sources (web, Wikipedia, arXiv), interface with productivity tools (send email, manage calendar entries), generate or interpret images, and more. Systems are being built where the LLM has access to hundreds of tools, using heuristics to select the most relevant subset at any given step.

Patil et al. state that “Integrating APIs like web search, translation, and database access enabled the model to perform a broader range of tasks, showcasing the versatility of tool use” (p. 5). Yang et al. demonstrate that “The inclusion of multimodal tools allowed ChatGPT to process and generate responses involving text, images, and other data types, enhancing its reasoning capabilities” (p. 7).

Planning
Planning enables AI agents to autonomously devise a sequence of actions needed to achieve a larger goal. Instead of executing a task in one step, the agent breaks it down into smaller, manageable subtasks and determines the optimal order to perform them. For example, an AI agent tasked with online research might break down the objective into subtasks like researching specific subtopics, synthesizing findings, and compiling a report.

While many workflows do not require planning, it is crucial for complex tasks where a predefined sequence of steps isn’t feasible. Planning allows the agent to decide dynamically what steps to take. However, this powerful capability can lead to less predictable results and is a token-intensive operation.

Wei et al. highlight that “By breaking down complex tasks into sequential steps, chain-of-thought prompting significantly improved the model’s ability to handle intricate problem-solving tasks” (p. 8). Shen et al. describe how “Coordinating multiple models allowed for more comprehensive task execution, highlighting the strengths of collaborative planning among diverse AI agents” (p. 10).

Multi-Agent Collaboration
Multi-Agent Collaboration involves multiple AI agents working together, each specializing in different aspects of a task. This pattern mimics human collaborative efforts, where team members contribute their unique skills to achieve a common goal. For instance, in software development, different agents handle coding, testing, documentation, and project management, working together to create a comprehensive software solution.

This approach can break down complex tasks like writing software into subtasks executed by different roles, such as a software engineer, product manager, designer, and QA engineer. Different agents might be built by prompting one or multiple LLMs to carry out specific tasks. The multi-agent design pattern provides a framework for decomposing complex tasks into subtasks, similar to how complex projects are divided into smaller tasks in a corporate environment. Each agent implements its workflow, has its memory, and may ask other agents for help, resulting in a cacophony of LLM calls and message passing between agents.

Qian et al. discuss how “Agents were able to collaboratively identify bugs and optimize code, demonstrating the efficacy of multi-agent systems in software engineering” (p. 3). Wu et al. describe how “AutoGen facilitated complex conversations among multiple agents, allowing them to dynamically adjust their strategies and improve task outcomes” (p. 2).

Implementing Design Patterns with AutoGen

Source: AutoGen: Enabling Next-Gen LLM Applications via Multi-Agent Conversation-Paper

Microsoft’s AutoGen framework is a powerful tool for implementing these design patterns, particularly Multi-Agent Collaboration. AutoGen allows developers to create and manage multiple AI agents that can communicate and collaborate to solve tasks. The framework provides several key features that support the implementation of these design patterns:

  1. Conversable Agents: AutoGen enables agents to engage in dynamic dialogues, adapting their roles and responsibilities based on the conversation. This feature is integral to both Reflection and Multi-Agent Collaboration.
  2. Tool-Enabled Agents: Agents can utilize external tools for various tasks, extending their capabilities beyond text generation, thus incorporating the Tool Use design pattern.
  3. Nested Chat: AutoGen supports nested structures where agents can review and critique each other’s outputs, facilitating iterative improvements. This is essential for the Reflection pattern.
Source: AutoGen Website

4. Manager Agents: These agents coordinate the tasks of other agents, ensuring efficient planning and execution. Manager Agents are crucial for implementing the Planning and Multi-Agent Collaboration patterns.

5. Group Chat: Multiple agents can communicate and collaborate in structured conversations using Group Chat Manager to address different components of a task. This feature is fundamental to Multi-Agent Collaboration.

Source: AutoGen Website

Essentially, AutoGen Studio is a platform designed for the rapid prototyping of multi-agent solutions using AutoGen framework. It’s a practical tool that allows you to build and experiment with your own agents. One of the key features of AutoGen Studio is its ability to let you define and modify agent workflows through a straightforward interface. You can create chat sessions with the specified agents and view results, such as chat history, generated files, and time taken. It’s a hands-on way to see your agents in action.

What’s more, AutoGen Studio is open source. If you are looking to go on a fast-track of creating and using agents, please check out the above article.

In the following section, let’s look at implementing the ‘LN Consulting’ use case (modified a bit) which we addressed using CrewAI…

…now with AutoGen Framework implementing Reflection, tool use (via GPT Assistant), Planning and Multi-Agent Collaboration.

(Complete code available at Github link at the end of this article)

Implementation exaamples usingAutogen

Here are the core steps involved in setting up Multi-Agent Collaboration with Autogen.

  1. Define the task
task = "Take-it-easy.Inc is experiencing declining sales in their main product line,"\
"which has been on the market for been. They suspect Increased competition, outdated marketing strategies."\
"are to blame. Their desired outcome is to Revitalize sales and regain market share. within the next the." \
"They are Open to exploring new marketing channels."\
"Generate a 2-page sample report in markdown format," \
" with teaser of possible solutions"\
"and how LN Consulting can deliver, using LN Consulting's relevant case studies. Save it as sample.report.txt"

2. Set up the agents

#Agent-1 
user_proxy = autogen.ConversableAgent(
name="Admin",
system_message="Give the task to planner and review the ouput of lead_consultant. Once the report is completed, display the final report and save it as sample_report.txt.",
code_execution_config=False,
llm_config=llm_config2,
human_input_mode="ALWAYS",
)
# Agent-2
planner = autogen.ConversableAgent(
name="Planner", #Manager
system_message="Given a task, please determine "
"what information is needed to complete the task. "
"After each step is done by others, check the progress and "
"instruct the remaining steps. If a step fails, try to "
"workaround",
description="Planner. Given a task, determine what "
"information is needed to complete the task. "
"After each step is done by others, check the progress and "
"instruct the remaining steps",
llm_config=llm_config2,
)
# Agent-3
research_specialist = autogen.AssistantAgent(
name="research_specialist",
llm_config=llm_config1,
system_message="Research Specialist."
"Research relavent information as per the plan from planner."
"This research provides the foundation for strategic recommendations and report writing.Get any relavent details from gpt_assistant",
description="Research Specialist."
"Write a draft document based on the research"
)
# Agent-4
gpt_assistant = GPTAssistantAgent(
name="LN_Consulting_Docs_Analyser", instructions="Please extract all the relavent details of LN Consulting from the uploaded documents related to LN Consulting and running_text and give to lead_consultant.", llm_config=llm_config
)
# Agent- 5
lead_consultant = autogen.AssistantAgent(
name="lead_consultant",
llm_config=llm_config1,
system_message="Writer."
"Review and modify reports in markdown format (with relevant titles) tailored to LN consulting."
"You take feedback from the admin and refine your report.",
description="Lead Consultant and Report Writer."
"Wearing a strategic hat, review and write final reports based on the research and take "
"feedback from the admin to refine the report. Present the final report to admin in markdown format and also save as sample_report.md. Add a movie quote"
)

gpt_assistant calls Open AI assistant using assistant_id. In this case it works like a RAG tool delivering relevant case studies for the report generation, from a set of documents.

Source: Sample test for AI assistant

3. Group Chat

groupchat = autogen.GroupChat(
agents=[user_proxy, research_specialist, gpt_assistant, lead_consultant, planner],
messages=[],
max_round=6,
)
manager = autogen.GroupChatManager(
groupchat=groupchat, llm_config=llm_config1
)
groupchat_result = user_proxy.initiate_chat(
manager,
message=task,
)

AutoGent also provides a way to constrain the selection of the next speaker by using theallowed_or_disallowed_speaker_transitions argument of the GroupChat class.

# Sample, not part of this usecase depiction
groupchat = autogen.GroupChat(
agents=[user_proxy, engineer, writer, executor, planner],
messages=[],
max_round=10,
allowed_or_disallowed_speaker_transitions={
user_proxy: [engineer, writer, executor, planner],
engineer: [user_proxy, executor],
writer: [user_proxy, planner],
executor: [user_proxy, engineer, planner],
planner: [user_proxy, engineer, writer],
},
speaker_transitions_type="allowed",
)
```

Final output

('The final report for Take-it-easy.Inc has been carefully prepared and '
'reviewed by the consultants, incorporating a thorough analysis, case '
'precedents from LN Consulting, and outlined strategies tailored to address '
'the specific challenges faced by the company. Below is the finalized '
'content, ready for saving as `sample_report.txt`.\n'
'\n'
'```markdown\n'
'# Revitalizing Take-it-easy.Inc\n'
'\n'
'## Introduction\n'
'\n'
'Amidst heightened competition and fading effectiveness of conventional '
'marketing approaches, Take-it-easy.Inc stands on critical grounds needing a '
'transformative leap. LN Consulting, with a track record of enabling '
'companies to thrive through innovation and strategic revamps, is positioned '
'as the ideal partner to drive this transformation.\n'
'\n'
'## Current Situation Analysis\n'
'\n'
"Take-it-easy.Inc's current predicament stems from:\n"
'- Outdated marketing tactics failing to connect with evolving customer '
'expectations.\n'
'- A product line that has not kept pace with market trends and consumer '
'preferences.\n'
'\n'
'## Case Studies Highlight from LN Consulting\n'
'\n'
'### 1. Digital Transformation for ABC Media Solutions:\n'
' - **Outcome**: Enhanced engagement by 45%.\n'
' - **Strategy**: Overhaul of digital channels and introduction of '
'data-driven marketing tactics.\n'
'\n'
'### 2. Content Optimization for XYZ Broadcasting:\n'
' - **Outcome**: 40% uplift in viewer engagement.\n'
' - **Strategy**: Revamping content delivery systems to provide tailored '
'viewer experiences.\n'
'\n'
'### 3. Operational Revamp for GDI Tech Solutions:\n'
' - **Outcome**: 30% boost in efficiency and 25% cost reduction.\n'
' - **Strategy**: Streamlined operations through process automation and '
'resource optimization.\n'
'\n'
'## Proposed Action Plan for Take-it-easy.Inc\n'
'\n'
'- **Enhanced Digital Strategy**: Deploy cutting-edge digital marketing '
'solutions to widen reach and improve customer interaction.\n'
'- **Content Renewal and Personalization**: Re-engineer content strategy to '
'resonate deeply with targets, enhancing retention and acquisition.\n'
'- **Efficiency in Operations**: Implement process improvements that slash '
'costs while enhancing service delivery.\n'
'\n'
'## Conclusion and Call to Action\n'
'\n'
'LN Consulting invites Take-it-easy.Inc to a detailed discussion on how these '
'proposed strategies can be customized and implemented to turnaround the '
'prevailing market challenges into growth opportunities. We are excited to '
'potentially partner and chart a success story together.\n'
'\n'
'Contact us at:\n'
'- Email: [contact@lnconsulting.com](contact@lnconsulting.com)\n'
'- Phone: 123-456-7890\n'
'```\n'
'\n'
'### Saving the Report\n'
'The final report is now ready to be saved as `sample_report.txt`. This '
'document encapsulates a strategic blueprint designed to facilitate '
"Take-it-easy.Inc's resurgence in the market.")

Github Link

I also tried out another use case covered in my below article that explores nested chat. (Also available on my github)

In conclusion, leveraging design patterns such as reflection, tool use, planning, and multi-agent collaboration significantly enhances the capabilities and efficiency of AI agents. By implementing these patterns through frameworks like AutoGen, which is programmer-friendly, developers can create adaptable AI systems. Meanwhile, tools like CrewAI offer a more accessible approach for non-programmers to benefit from AI-driven workflows. The use cases presented in this article, which are related to small businesses, aim to spark interest in AI agents. They also serve to demonstrate the potential of AI design patterns, showing the way for the development of more advanced and collaborative AI solutions.

In an upcoming article, I plan to cover a detailed comparison of existing AI agent frameworks.

--

--