Building Your First Multi-Agent System: A Beginner’s Guide
Image by Editor | Midjourney
The surge of AI in general — and large language models (LLMs) in particular — is thanks to numerous research groups and companies racing to develop their most advanced models and demonstrate their potential use cases across broad domains. More recently, these developments often focus on one model aspect: reasoning. Each new iteration of a given model is often introduced as a one which possesses greater thinking capability and better reasoning.
With more potent reasoning models emerging, the topic of AI agents is a hot one. Agents and their discussion are gaining momentum thanks to their ability to take action toward the fulfilment of specific goals, supported by the strong reasoning abilities of LLMs.
It’s becoming essential for practitioners to able to build AI agents, especially multi-agent systems. How can we go about doing so? Let’s explore together.
Multi-Agent System
AI agents are autonomous systems that can observe the environment, reason about their goals, and take action using external tools. This means the agent is a system that can reason and execute actions to achieve its goal.
In general, AI agents’ core relies on cognitive architecture, which comprised of three components, including:
- Language model as the central decision-making engine
- Tools for the agent to interact with the external systems
- Orchestration that governs how the agents act
Individually, the agent is already robust enough to perform basic tasks. However, a single agent might not be enough to handle more complex tasks. With an increase in complexity, the chance for the agent to fail to achieve the goal is higher. This is why a multi-agent system emerges: to allow several agents to work collaboratively towards shared goals. Multiple specialized individual agents work in a collaborative environment to finish individual tasks and achieve the shared, overarching goal.
Structure-wise, multi-agent systems can be constructed in any way that preserves collaboration. No specific architecture will be ideal for all use cases. Instead, we select the architecture that best accommodates our current problem.
Two of the most common architectures are either the network or supervisor.
![Single agent architecture versus multi-agent network and supervisor architectures.](https://www.kdnuggets.com/wp-content/uploads/Building-Your-First-Multi-Agent-System-A-Beginners-Guide_2.png)
Single agent architecture versus multi-agent network and supervisor architectures.
In a network architecture, agents operate within a multi-agent system, communicating with one another to decide which agent to call next. In contrast, a supervisor architecture relies on a single supervisor agent to determine which agent should act next. Beyond these, there are numerous other architectures to choose from — or you can design one tailored to your needs, including hybrid or ad hoc architectures.
For example, let’s build a multi-agent system to generate a report on a specific topic we want. We will use a supervisor network architecture, as shown in the image below.
![Our multi-agent system supervisor network architecture.](https://www.kdnuggets.com/wp-content/uploads/Building-Your-First-Multi-Agent-System-A-Beginners-Guide_3.png)
Our multi-agent system supervisor network architecture.
The structure detail is simple; we will have four specialized agents:
- Web researcher agent
- Trend analyst agent
- Report writer agent
- Proofreader agent
The manager agent supervises all of these agents which only perform their tasks if directed. Additionally, the web researcher agent has access to the web search tool to access the internet to research the intended topic.
Let’s start building the multi-agent system. We will use the CrewAI Agent to make things easier, which works well to initiate a multi-agent system.
First, we will install all the libraries we need to develop the system.
pip install crewai langchain–community pydantic |
Then, we will import all the necessary libraries used for this tutorial.
from crewai import Crew, Task, Agent, Process, LLM from pydantic import BaseModel from crewai.tools.structured_tool import CrewStructuredTool from langchain_community.tools import BraveSearch |
Agent systems depend on the LLM as their core decision-making. The stronger their reasoning power, the better. We will use the GPT-4o model as our LLM for this tutorial, but you can always switch this to another model if you prefer. CrewAI uses LiteLLM to access the LLM, so anything available to LiteLLM is useable within the CrewAI framework.
llm = LLM( model=‘openai/gpt-4o’, api_key=“YOUR-API-KEY” ) |
Then, we will initiate the searching tool using LangChain and wrap them in the CrewAI tools framework using the code below. For this example, we will use the Brave search engine. It will require the API key, so don’t forget to acquire one.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# Define the BraveSearch input schema class BraveSearchInput(BaseModel): query: str
def brave_search_wrapper(*args, **kwargs): if isinstance(kwargs, dict) and “query” in kwargs: query = kwargs[“query”] elif len(args) > 0 and isinstance(args[0], BraveSearchInput): query = args[0].query else: raise ValueError(“Invalid input provided to BraveSearchTool.”)
brave_search = BraveSearch.from_api_key( api_key=“BRAVE-API-KEY”, search_kwargs={“count”: 3} )
result = brave_search.run(query) return result
def create_brave_search_tool(): return CrewStructuredTool.from_function( name=“brave_search_tool”, description=( “Searches the web using BraveSearch and returns relevant information for a given query. “ “Useful for finding up-to-date and accurate information on a wide range of topics.” ), args_schema=BraveSearchInput, # Use the BraveSearch input schema func=brave_search_wrapper )
# Create the BraveSearch tool SearchTool = create_brave_search_tool() |
Next we will define our agent. CrewAI makes it easier to develop an agent by requiring only a description of its role, goal, and backstory. The more specific your description is, the better the agent will perform its action.
For example, the below code shows how we can define web researcher agent.
# Define agents web_researcher_agent = Agent( role=“Web Research Specialist”, goal=( “To find the most recent, impactful, and relevant about {topic}. This includes identifying “ “key use cases, challenges, and statistics to provide a foundation for deeper analysis.” ), backstory=( “You are a former investigative journalist known for your ability to uncover technology breakthroughs “ “and market insights. With years of experience, you excel at identifying actionable data and trends.” ), tools=[SearchTool], llm=llm, verbose=True ) |
Unique to the web researcher agent is the ability to access the SearchTool we have initiated previously.
I have provided the placeholder {topic}
in the web researcher agent above. This is where users can pass their input and affect the multi-agent systems without changing our code.
Let’s define the other agent we will use for our multi-agent system. You can always change the role, goal, and backstory for each agent as I only provide them as simple as possible:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
trend_analyst_agent = Agent( role=“Insight Synthesizer”, goal=( “To analyze research findings, extract significant trends, and rank them by industry impact, growth potential, “ “and uniqueness. Provide actionable insights for decision-makers.” ), backstory=( “You are a seasoned strategy consultant who transitioned into {topic} analysis. With an eye for patterns, “ “you specialize in translating raw data into clear, actionable insights.” ), tools=[], llm=llm, verbose=True )
report_writer_agent = Agent( role=“Narrative Architect”, goal=( “To craft a detailed, professional report that communicates research findings and analysis effectively. “ “Focus on clarity, logical flow, and engagement.” ), backstory=( “Once a technical writer for a renowned journal, you are now dedicated to creating industry-leading reports. “ “You blend storytelling with data to ensure your work is both informative and captivating.” ), tools=[], llm=llm, verbose=True )
proofreader_agent = Agent( role=“Polisher of Excellence”, goal=( “To refine the report for grammatical accuracy, readability, and formatting, ensuring it meets professional “ “publication standards.” ), backstory=( “An award-winning editor turned proofreader, you specialize in perfecting written content. Your sharp eye for “ “detail ensures every document is flawless.” ), tools=[], llm=llm, verbose=True )
manager_agent = Agent( role=“Workflow Maestro”, goal=( “To coordinate agents, manage task dependencies, and ensure all outputs meet quality standards. Your focus “ “is on delivering a cohesive final product through efficient task management.” ), backstory=( “A former project manager with a passion for efficient teamwork, you ensure every process runs smoothly, “ “overseeing tasks and verifying results.” ), tools=[], llm=llm, verbose=True ) |
Once we have set up the agents, we will set up the tasks that they will perform. If the agent is the individual, the task in the CrewAI is the action that certain will do and try to achieve.
The CrewAI task code structure example is shown in the code below.
# Define tasks web_research_task = Task( description=( “Conduct web-based research to identify 5-7 of the {topic}. Focus on key use cases. “ ), expected_output=( “A structured list of 5-7 {topic}.” ) ) |
You can generally pass two parameters, including the task description and the expected output. You can also set many other parameters, but those are the basics.
Let’s set up the other tasks for all the agents we have.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
trend_analysis_task = Task( description=( “Analyze the research findings to rank {topic}. “ ), expected_output=( “A table ranking trends by impact, with concise descriptions of each trend.” ) )
report_writing_task = Task( description=( “Draft report summarizing the findings and analysis of {topic}. Include sections for “ “Introduction, Trends Overview, Analysis, and Recommendations.” ), expected_output=( “A structured, professional draft with a clear flow of information. Ensure logical organization and consistent tone.” ) )
proofreading_task = Task( description=( “Refine the draft for grammatical accuracy, coherence, and formatting. Ensure the final document is polished “ “and ready for publication.” ), expected_output=( “A professional, polished report free of grammatical errors and inconsistencies. Format the document for “ “easy readability.” ) ) |
You can set which task should be performed by which agent, but the manager agent will do that for you.
Lastly, we will set up the crew to orchestrate the multi-agent system. We will pass the manager agent to the crew and have the supervisor decide which task to perform to achieve the goal.
crew = Crew( agents=[web_researcher_agent, trend_analyst_agent, report_writer_agent, proofreader_agent], tasks=[web_research_task, trend_analysis_task, report_writing_task, proofreading_task], process=Process.hierarchical, manager_agent=manager_agent, verbose=True ) |
With everything in place, let’s initiate the multi-agent system. With the following code, we can pass the topic we want to search for and write the report about.
crew_output = crew.kickoff(inputs={“topic”: “AI Trends”}) |
The output will be a final report on the multiple agents’ work. It will be too long to show the result, but the final report will look like something in the image below.
![Sample multi-agent report system final report output.](https://www.kdnuggets.com/wp-content/uploads/Building-Your-First-Multi-Agent-System-A-Beginners-Guide_4.jpg)
Sample multi-agent report system final report output.
Congratulations! You have developed your first multi-agent system. I suggest you follow up by trying out another agent combination, architecture, and tools to understand better how to develop the system.
Conclusion
An AI agent is a system that can reason and execute actions to achieve a given goal. Individually, agents are already robust enough to perform basic tasks. Still, a multi-agent system where multiple specialized individual agents work in a collaborative environment can achieve the goals more efficiently.
This article has demonstrated how to develop a multi-agent system to generate reports on desired topics using CrewAI. With this library, we saw how we are able to set up agents and tasks, and to structure the system and agent coordination with the implementation of the supervisor architecture using a manager agent.