4 Steps to Build Multi-Agent Nested Chats with AutoGen

Santhosh Reddy Dandavolu Last Updated : 12 Nov, 2024
6 min read

Chatbots have evolved exponentially with developments in artificial intelligence (AI). Now, with the onset of AI agents, they have become capable of handling more complex and layered interactions, far beyond traditional conversational limits. In our previous article on Building Multi-Agent Chatbots with AutoGen, we explored the concept of sequential chat using AutoGen, which allows structured, turn-based communication among multiple agents. Now, building upon that foundation, we will turn to a more complex feature: nested chat. With AutoGen’s robust framework, nested conversations enable bots to maintain fluid exchanges instead of following a fixed sequence. They can delve into other tools, handle interruptions, and resume smoothly, all within a single conversation flow. This article will guide you through implementing nested chat in AutoGen and highlight its relevance in creating responsive, dynamic agentic interactions.

What is Nested Chat?

Let’s begin by understanding what a nested chat is.

Consider a three-agent chat where it is required for two agents to repeatedly talk to each other in a loop. This chat between two agents can be added in a nested chat. Once this separate conversation is done, the agent can bring back the context to the main conversation.

The below figure shows the conversion flow of a nested chat.

What is nested chat

When the incoming message triggers a condition, that message goes to the nested chat. That nested can be a two-agent chat, a sequential chat, or any other. Then the chat results of the nested chat are sent back to the main conversation.

Implementing Nested Chat in AutoGen

In this article, we will build an article-writing system using a nested chat. For this, we will create three agents – one for writing an outline of the article, another for writing the article based on this outline, and another for reviewing the article. We will want the writer and reviewer to talk to each other multiple times so we will these two in a nested chat.

Additionally, we will also provide the outline agent access to a tool to query the web.

Now, let’s implement this with code.

Pre-requisites

Before building AutoGen agents, ensure you have the necessary API keys for the required LLMs. For this exercise, we will also be using Tavily to search the web.

Load the .env file with the API keys needed. Here we will use OpenAI and Tavily API keys ().

from dotenv import load_dotenv

load_dotenv('/home/santhosh/Projects/courses/Pinnacle/.env')

Define the LLM to be used as a config_list

config_list = {

	"config_list": [{"model": "gpt-4o-mini", "temperature": 0.2}]

}

Key Libraries Required

autogen-agentchat – 0.2.37

Tavily-python – 0.5.0

Now, let’s get to the implementation.

Step 1: Define the Outline Agent with the Tool Use

Define the user_proxy agent which will also execute the tool. Then define the Outline Agent using the LLM to generate the article outline.

from autogen import ConversableAgent
user_proxy = ConversableAgent(
	name="User",
	llm_config=False,
	is_termination_msg=lambda msg: msg.get("content") is not None and "TERMINATE" in msg["content"],
	human_input_mode="TERMINATE")

outline = ConversableAgent(
	name="Article_outline",
	system_message="""You are an expert content strategist tasked with creating a detailed outline
                	for an article on a specified topic. Your goal is to organize the article into
                	logical sections that help convey the main ideas clearly and effectively.
                	Use the web_search tool if needed.
                	Return 'TERMINATE' when the task is done.""",
	llm_config=config_list,
	silent=False,
)

Define the web_search function to query the web.

def web_search(query: str) -> str:
	tavily_client = TavilyClient()
	response = tavily_client.search(query, max_results=3, days=10, include_raw_content=True)
	return response['results']

Register the web_search function to the outline agent with the executor as user_proxy.

We are making the executor as user_proxy so that we can review the outline that goes to the writer agent.

register_function(
	web_search,
	caller=outline,  # The assistant agent can suggest calls.
	executor=user_proxy,  # The user proxy agent can execute the calls.
	name="web_search",  # By default, the function name is used as the tool name.
	description="Searches internet to get the results a for given query",  # A description of the tool.
)

Step 2: Define the Writer and Reviewer Agents

Define one agent to generate the article content, and another to review the article and provide suggestions to improve.

writer = ConversableAgent(
	name="Article_Writer",
	system_message="""You are a skilled writer assigned to create a comprehensive, engaging article
                	based on a given outline. Your goal is to follow the structure provided in the outline,
                	expanding on each section with well-researched, clear, and informative content.
                	Keep the article length to around 500 words.
                	Use the web_search tool if needed.
                	Return 'TERMINATE' when the task is done.""",
	llm_config=config_list,
	silent=False,
)
reviewer = ConversableAgent(
	name="Article_Reviewer",
	system_message="""You are a skilled article reviewer who can review technical articles.
                	Review the given article and provide suggestions to make the article more engaging and interesting.
                	""",
	llm_config=config_list,
	silent=False,
)

Step 3: Register the Nested Chat

We can now register the nested chats for both agents.

writer.register_nested_chats(
	trigger=user_proxy,
	chat_queue=[
    	{
        	"sender": reviewer,
        	"recipient": writer,
        	"summary_method": "last_msg",
        	"max_turns": 2,
    	}
	],
)

In the above code, when user_proxy sends any message to the writer agent, this will trigger the nested chat. Then the writer agent will write the article and the reviewer agent will review the article as many times as max_turns, two in this case. Finally, the result of the nested chat is sent back to the user agent.

Step 4: Initiate the Nested Chat

Now that everything is set, let’s initiate the chat

chat_results = user_proxy.initiate_chats(

              	[{"recipient": outline,

                	"message": "Write an article on Magentic-One agentic system released by Microsoft.",

                	"summary_method": "last_msg",

                	},

               	{"recipient": writer,

                	"message": "This is the article outline",

                	"summary_method": "last_msg",

                	}])

Here, we are going to write an article about the Magentic-One agentic system. First, the user_proxy agent will initiate a chat with the Outline Agent, and then with the Writer Agent.

Now, the output of the above code will be like this:

nested agent chat on AutoGen

As we can see, the user_proxy first sends a message stating the topic of the article to the Outline Agent. This triggers the tool call and user_proxy executes the tool. Based on these results, the Outline Agent will generate the outline and send it to the writer agent. After that, the nested chat between the writer agent and reviewer agent continues as discussed above.

Now, let’s print the final result, which is the article about magentic-one.

print(chat_results[1].chat_history[-2]['content'])
"

Conclusion

Nested chat in AutoGen enhances chatbot capabilities by enabling complex, multitasking interactions within a single conversation flow. Nested chat allows bots to initiate separate, specialized chats and integrate their outputs seamlessly. This feature supports dynamic, targeted responses across various applications, from e-commerce to healthcare. With nested chat, AutoGen paves the way for more responsive, context-aware AI systems. This enables developers to build sophisticated chatbots that meet diverse user needs efficiently.

If you want to learn more about AI Agents, checkout our exclusive Agentic AI Pioneer Program!

Frequently Asked Questions

Q1. What is nested chat in AutoGen, and how does it differ from sequential chat?

A. Nested chat in AutoGen allows a chatbot to manage multiple sub-conversations within a single chat flow, often involving other agents or tools to retrieve specific information. Unlike sequential chat, which follows a structured, turn-based approach, nested chat enables bots to handle interruptions and parallel tasks, integrating their outputs back into the main conversation.

Q2. How does nested chat improve customer support in applications?

A. Nested chat improves customer support by allowing bots to delegate tasks to specialized agents. For instance, in e-commerce, a chatbot can consult a separate agent to check order status or product information, then seamlessly relay the information back to the user, ensuring quicker, more accurate responses.

Q3. What are the key use cases of nested chat in different industries?

A. Nested chat can be applied in diverse industries. In banking, it provides specialized support for account and loan queries; in HR, it assists with onboarding tasks; and in healthcare, it handles appointment scheduling and billing inquiries. This flexibility makes nested chat suitable for any domain requiring multitasking and detailed information handling.

Q4. Do I need any specific setup to implement nested chat in AutoGen?

A. Yes, implementing nested chat in AutoGen requires configuring agents with specific API keys, such as for language models or web search tools like Tavily. Additionally, each agent must be defined with appropriate tasks and tools for the smooth execution of nested conversations.

Q5. Can I track costs associated with each nested chat agent in AutoGen?

A. Yes, AutoGen allows tracking the cost incurred by each agent in a nested chat. By accessing the `cost` attribute in chat results, developers can monitor expenses related to agent interactions, helping optimize the chatbot’s resource usage and efficiency.

I am working as an Associate Data Scientist at Analytics Vidhya, a platform dedicated to building the Data Science ecosystem. My interests lie in the fields of Natural Language Processing (NLP), Deep Learning, and AI Agents.

Responses From Readers

Clear

We use cookies essential for this site to function well. Please click to help us improve its usefulness with additional cookies. Learn about our use of cookies in our Privacy Policy & Cookies Policy.

Show details