Building Multi-Agent AI Systems with CrewAI: Assigning and Orchestrating Multiple AI Agents in Python

Artificial Intelligence tutorial - IT technology blog
Artificial Intelligence tutorial - IT technology blog

In modern software development, we frequently encounter projects that demand a myriad of different skills. Imagine building a complete system: from market research, feature ideation, coding, testing, to creating user manuals and marketing strategies. Each stage requires distinct expertise.

Early in my career, I remember having to handle almost everything myself for a small project. I felt like a versatile “hero,” but in reality, I spent a lot of time and was prone to errors due to a lack of deep expertise in each area. That’s when I realized, a complex project requires a team, not a lone individual. This principle holds even truer when applying Artificial Intelligence (AI) to complex tasks.

The Practical Problem: Why a Single AI Agent Isn’t Enough?

When first working with AI, many people tend to think of a single, omnipotent “super AI.” I used to be like that too. We often assign a comprehensive task to large language models (LLMs) and expect them to solve it autonomously. For example, a request like: “Write a blog post on topic X, including code examples and SEO optimization.”

While it sounds simple, the results are often not as expected. The article might lack research depth, the code snippet may not be optimized, or the SEO part might be very basic. Why is that?

Root Cause Analysis: Limitations of Single-Task AI Agents

The problem is that while LLMs are incredibly powerful, they still have inherent limitations when handling multi-faceted tasks that require deep specialization. I often visualize it like asking a friend who is good at everything but not deeply specialized in any particular field. That friend can do many things, but achieving excellence is difficult.

First, lack of specialization: An LLM tasked with multiple duties simultaneously will have its attention divided. It cannot simultaneously delve deep into a topic, act as a programmer writing optimized code, and finally, be an SEO expert to effectively optimize keywords. Consequently, the results are often generic and not truly outstanding.

Second, context window limitations: When a task becomes too long and complex, the information to be processed can exceed the LLM’s “context window” limit. This makes it difficult for the LLM to maintain coherence and connection between different parts of the task.

Third, difficulties in coordination and control: If we want AI to work according to a specific process (e.g., research first, then write, then optimize), a single AI will struggle to comply accurately. Controlling the workflow and intervening to fix errors also becomes more complex.

In practical work, I’ve realized this is a crucial skill to master: the ability to decompose large problems into smaller parts and assign them to specialized “components” for handling. When we apply this mindset to AI, we will see immense potential.

Solutions: From Manual to Automation

1. Manual Approach: Self-Coordinating Each Step

Initially, I adopted this approach: using an LLM for research, then manually copying the results, then using the same LLM (or another) to write, and finally self-adjusting for SEO. This method allowed me to have complete control over each step and easily make corrections when needed. However, it was extremely time-consuming, repetitive, and difficult to scale as the number or size of tasks increased.

2. Building Your Own Coordination System

Taking a step further, I wrote Python scripts to call LLMs multiple times, each with a different prompt, then connect these results. For example, calling an LLM to “research,” saving the output, then calling a second LLM with the results of the first step and requesting it to “write an article.”

This approach offered some automation, but I had to invest a lot of effort in building coordination logic, handling errors, and ensuring steps ran in the correct sequence. This is akin to building a separate water pipeline for each house—both costly and inefficient in the long run.

3. Utilizing Multi-Agent AI Frameworks

This is precisely the optimal solution I want to introduce to you. Instead of doing everything ourselves, we use libraries or frameworks specifically designed to create and coordinate multiple AI Agents. Each Agent will have a specific role and goal, much like a professional team. They can communicate, exchange information, and collectively accomplish a large task.

These frameworks help us overcome the limitations of single AI Agents, maximizing the power of LLMs by allowing them to focus on their specific expertise.

The Best Approach: Building Multi-Agent AI Systems with CrewAI

Among the existing multi-agent frameworks (such as Microsoft’s AutoGen), CrewAI stands out due to its simplicity, accessibility, and clear structure, making it particularly suitable for beginners in this concept in Python.

What is CrewAI?

CrewAI is an open-source framework built on Python that allows us to create, manage, and coordinate AI Agents that operate independently yet collaboratively. Imagine a department within a company, where there are specialists like market researchers, content writers, designers, etc. CrewAI helps you build a “virtual department” consisting of similar AI Agents.

The goal of CrewAI is to solve complex problems by dividing them into smaller tasks, then assigning them to specialized Agents for processing. These Agents can communicate with each other, use external tools (e.g., web search, document reading), and make decisions based on defined processes.

Key Components of CrewAI

To build a “crew” with CrewAI, you need to understand 4 key concepts:

  • Agent: Is a “member” of the AI team. Each Agent is defined by:
    • role: E.g., “Market Research Specialist”.
    • goal: E.g., “Find the latest technology trend information”.
    • backstory: A brief description of the Agent’s experience and personality, e.g., “Meticulous, cautious, enjoys data analysis”.
    • llm (language model): The AI model the Agent will use (like GPT-4, Claude 3).
    • tools: Tools the Agent can use (e.g., Google Search, API access, PDF reading).
  • Task: Is a specific “mission” that an Agent needs to complete. Each Task has:
    • description: Detailed content of the task.
    • expected_output: The format or content of the expected result, e.g., “a 500-word report in Markdown format”.
    • agent: The Agent assigned to this task.
  • Tool: Are extended functionalities that help Agents interact with the external world. CrewAI provides some built-in tools (e.g., SerperDevTool for web search), or you can define your own tools.
  • Crew: Is the “team” that brings Agents and Tasks together, defining the workflow.
    • agents: List of participating Agents.
    • tasks: List of Tasks to be completed.
    • process: Workflow. The two main types are Process.sequential (Agents execute sequentially: one Agent finishes before the next begins) or Process.hierarchical (one Agent manages and coordinates other Agents).

Installation and Basic Example with CrewAI

First, you need to install the CrewAI and CrewAI Tools libraries. CrewAI Tools contains useful tools that Agents can use.


pip install crewai crewai-tools

Next, you need to set up the API key for the language model you want to use (e.g., OpenAI, Anthropic Claude). It is recommended to store these keys in environment variables to ensure security and ease of management.


import os
os.environ["OPENAI_API_KEY"] = "YOUR_OPENAI_API_KEY"
os.environ["OPENAI_MODEL_NAME"] = "gpt-4-turbo" # Or gpt-3.5-turbo

Now, let’s build a simple example: a “Blog Writing Crew” with a “Researcher” Agent and an “Editor” Agent.


from crewai import Agent, Task, Crew, Process
from langchain_openai import ChatOpenAI

# 1. Initialize LLM
llm = ChatOpenAI(model="gpt-4-turbo", temperature=0.7)

# 2. Define Agents
researcher = Agent(
    role='Market Research Specialist',
    goal='Find the latest information and trends about Artificial Intelligence',
    backstory='An experienced analyst, always up-to-date with AI technology breakthroughs.',
    verbose=True,
    allow_delegation=False,
    llm=llm
)

writer = Agent(
    role='Content Editor',
    goal='Write an engaging and easy-to-understand blog post based on research results',
    backstory='A seasoned writer, capable of transforming complex information into attractive content.',
    verbose=True,
    allow_delegation=True, # Can delegate if needed
    llm=llm
)

# 3. Define Tasks
research_task = Task(
    description='Find 3 most prominent AI trends in 2024 and analyze their potential.',
    expected_output='A detailed summary of the top 3 AI trends, with explanations.',
    agent=researcher
)

write_task = Task(
    description=(
        'Write a blog post of approximately 800 words based on the researched AI trend summary. '
        'The article needs a clear structure, using easy-to-understand and engaging language for a general audience.'
    ),
    expected_output='The entire blog post content in Markdown format.',
    agent=writer
)

# 4. Build Crew (Team)
blog_crew = Crew(
    agents=[researcher, writer],
    tasks=[research_task, write_task],
    process=Process.sequential, # Sequential process: Research then write
    verbose=True
)

# 5. Launch Crew and view results
print("### Starting blog creation process with CrewAI ###")
result = blog_crew.kickoff()
print("\n### Final blog result: ###")
print(result)

Advanced Example: Practical Assignment and Coordination with Tools

For Agents to be truly useful, they need the ability to interact with the outside world. This is where Tools come into play. Suppose you want an Agent to be able to search for information on the internet.

We will use SerperDevTool, a web search tool provided by CrewAI Tools. To use it, you need a Serper API key (you can sign up for free with a certain number of queries).


# Install the Serper library if you haven't already
pip install google-search-results

import os
from crewai import Agent, Task, Crew, Process
from crewai_tools import SerperDevTool
from langchain_openai import ChatOpenAI

# Set up API keys
os.environ["OPENAI_API_KEY"] = "YOUR_OPENAI_API_KEY"
os.environ["SERPER_API_KEY"] = "YOUR_SERPER_API_KEY" # Register at serper.dev

llm = ChatOpenAI(model="gpt-4-turbo", temperature=0.7)

# Initialize search tool
search_tool = SerperDevTool()

# Define Market Data Analyst Agent with search tool
researcher_with_tool = Agent(
    role='Market Data Analyst',
    goal='Search and analyze the latest technology trends on the Internet',
    backstory='A veteran researcher, always using search tools to gather accurate and timely information.',
    verbose=True,
    allow_delegation=False,
    tools=[search_tool], # Assign search tool to this Agent
    llm=llm
)

# Define Content Strategy Specialist Agent
content_strategist = Agent(
    role='Content Strategy Specialist',
    goal='Create a detailed content plan based on researched market trends',
    backstory='Capable of transforming data into engaging content strategies that attract the target audience.',
    verbose=True,
    allow_delegation=True,
    llm=llm
)

# Define Research Task using the tool
research_task_with_tool = Task(
    description='Use the search tool to find 5 most prominent technology trends that could impact the IT industry in the next 2 years. Then summarize each trend.',
    expected_output='A list of 5 top technology trends, each with a brief description and potential impact.',
    agent=researcher_with_tool,
)

# Define Content Planning Task
strategy_task = Task(
    description=(
        'Based on the 5 researched technology trends, create a detailed content plan for a series of 3 blog posts. '
        'Each blog post needs a title, main outline (H2/H3), and target audience. '
        'Ensure the blog posts are linked and cover different aspects of the trends.'
    ),
    expected_output='A detailed content plan for 3 blog posts, including title, outline, and target audience for each, in JSON format.',
    agent=content_strategist,
)

# Build Crew
marketing_crew = Crew(
    agents=[researcher_with_tool, content_strategist],
    tasks=[research_task_with_tool, strategy_task],
    process=Process.sequential,
    verbose=True
)

# Run Crew
print("### Starting content planning process with CrewAI and search tool ###")
result_advanced = marketing_crew.kickoff()
print("\n### Complete content plan: ###")
print(result_advanced)

Notes on Using CrewAI

To achieve the best results when building multi-agent AI systems with CrewAI, I have some experience to share:

  1. Effective prompting techniques for Agents and Tasks: This is key. The Agent’s role, goal, backstory and the Task’s description, expected_output need to be very clear and detailed. The more specific, the better the Agent will understand the task and produce accurate results.
  2. Choose the right LLM: Different LLMs have different capabilities and costs. For critical tasks requiring creativity, I prioritize powerful models like GPT-4 (currently priced around $0.03/1K tokens for input and $0.06/1K tokens for output). For simpler tasks, GPT-3.5 Turbo (10-20 times cheaper) or other models can be more economical choices.
  3. Cost management: Every time an Agent calls an LLM, it consumes tokens and incurs costs. Monitor CrewAI’s output (when verbose=True) to understand the workflow and optimize unnecessary LLM calls. One tip is to set token limits for each call or use API cost monitoring tools.
  4. Experiment and iterate: Building a perfect CrewAI doesn’t happen instantly. You need to experiment, review results, adjust prompts, Agent roles, or workflows until you achieve the desired performance. Don’t be afraid to continuously change and improve!
  5. Effective Use of Tools: Leverage available tools or build your own so that Agents can perform real-world actions, helping solve problems beyond the pure reasoning capabilities of LLMs.

Conclusion

Multi-agent AI systems are not just an interesting concept but also a powerful approach to solving complex challenges in software development and many other fields. With CrewAI, you will immediately have a powerful assistant to turn the idea of “AI teams” into reality, automate workflows, and significantly boost productivity.

Learning how to break down problems, define clear roles for each Agent, and enable them to coordinate smoothly is an incredibly valuable skill. I believe that once you master how to use CrewAI, you will open up a new era in building smarter and more efficient AI applications. Don’t hesitate to start experimenting today!

Share: