Title: Create Multi-Agent Systems with CrewAI

1 Intro CrewAI

CrewAI is an open-source framework designed to orchestrate and coordinate teams of autonomous AI agents, similar to Autogen. Think of it as a way to assemble and manage a group of AI assistants that collaborate to achieve a shared objective, much like a crew on a ship or a project team.

Here are some essential aspects of CrewAI:

  • Emphasis on Collaboration: Unlike many AI frameworks that prioritize individual agents, CrewAI is built for seamless teamwork. Agents share information and tasks, leveraging “collaborative intelligence” to tackle complex challenges that would be daunting for a single agent to handle.
  • Role-Specific Agents: Each agent within a CrewAI team can assume a distinct role—be it a data engineer, marketer, or customer service representative. This role-based approach allows you to customize the team according to the specific demands of your project.
  • User-Friendly and Adaptable: CrewAI is designed with simplicity in mind, making it accessible even for those without a deep understanding of AI. Its flexibility means you can easily tailor it to meet your unique requirements, including the ability for each agent to utilize different large language models (LLMs) suited to their specific roles and tasks.



2 Common Use Cases for CrewAI

1. Building a Smart Assistant Platform: CrewAI can be leveraged to develop a team of agents capable of managing various tasks, such as scheduling appointments, arranging travel, and responding to user inquiries. This creates a comprehensive smart assistant that streamlines everyday activities.

2. Creating an Automated Customer Service System: With CrewAI, you can assemble a team of agents dedicated to handling customer inquiries, resolving issues, and providing support. This automated system enhances customer experience by ensuring timely and efficient responses.

3. Developing a Multi-Agent Research Team: CrewAI can facilitate the formation of a collaborative research team composed of agents that work together on projects. They can analyze data, generate hypotheses, and test ideas, making the research process more efficient and effective.




3 The CrewAI Workflow Process

The CrewAI workflow process typically involves the following steps:

1. Agents: In this initial phase, you define the capabilities of your CrewAI workflow by specifying the agents involved. This includes outlining their roles and the skills they should possess, effectively determining who does what within the team.

2. Tasks: Next, you establish the specific objectives you want your agents to achieve. This step is crucial for guiding the agents toward accomplishing the desired outcomes.

3. Process: Here, you outline how CrewAI will utilize the defined agents and tasks to meet the overarching goals of your project. This involves mapping out the interactions and workflows that will drive the collaboration.

4. Run: Finally, you initiate the execution of your agents and tasks. Once the run is underway, assuming everything goes smoothly, CrewAI will generate results aimed at solving the stated objectives. This step marks the transition from planning to action, bringing your workflow to life.




4 Key Elements of CrewAI Agent

  • Focus

    • When mixing too much information, too many tools, or too much context, the models can lose important information (and also open up opportunities for more hallucination)
    • Another great advantage of the agent is they are able to focus:
      • Tools available
      • Context
      • Goal to achieve
  • Tools

    • Can overload agents with too many tools
    • Smaller models may not know which is the context, which are the tools
    • Provide the agents with the key tools to do the job that they need.
  • Memory

    • Make huge and immense differences for the agents
    • Recollect what they did in the past, learn from it, and apply their knowledge into future execution
    • Some frameworks offer different types of memory and different types of implementations for it.
    • CrewAI Agents have three types of memory:
      • I) Long-term memory
        • Memory that is remain even after the crew finishes
        • The memory is stored in a database locally
        • Everything after an agent completed the tasks, it self critiques itself to learn what it should have done better; or what are the things that should be in there that are not
        • Leads to self-improving agents
      • II) Short-term memory
        • Use only during the crew execution
        • When crew kick-off, it starts from the bank
        • Agents store different things that they learn in this memory
          • Allow agents to share knowledge, activities, and learnings with other agents. Agent 1 can tap into learnings from Agent 3.
          • Allows agents to share intermediate information even before providing “task completion” output.
      • III) Entity memory
        • Only being store during the execution
        • It stores what are the subjects that are being discussed


5 Equip Agents with Tools

A tool in CrewAI is a skill or function that agents can utilize to perform various actions.

Tools are pivotal in extending the capabilities of CrewAI agents, enabling them to undertake a broad spectrum of tasks and collaborate effectively. When building solutions with CrewAI, leverage both custom and existing tools to empower your agents and enhance the AI ecosystem

  • ✦ Tools are essential for LLM Agents as they significantly enhance their capabilities.
  • ✦ They enable agents to perform a wide range of tasks, from web searching and data analysis to content generation and collaboration.
  • ✦ Tools also provide customizability, allowing developers to create or integrate specific functionalities tailored to their needs.
Different Ways to Give Agents Tools
  • Agent Level:
    • The Agent can use the Tool(s) on any Task it performs.
  • Task Level:
    • The Agent will only use the Tool(s) when performing that specific Task.

5.1 Key Differences Between Assigning Tools to an Agent vs. a Task

Here are the primary distinctions:

  1. Purpose of Tools for Agents vs. Tasks

    • Agents: Tools given to agents are meant to empower the agent throughout its operational lifespan. They are part of the agent’s capabilities and are accessible during any task execution. These tools reflect the agent’s skill set, enhancing the agent’s overall functionality and efficiency.
    • Tasks: Tools assigned to tasks are utilized specifically within the context of that task. The tools here are employed to achieve a specific goal tied to the task, regardless of the agent performing it. The task defines which tools are necessary to complete its assignment and can override the default tools of an agent.
  2. Scope and Context of Tool Usage

    • Agents: The tools assigned to agents are available across all tasks the agent undertakes. For example, if an agent has a web scraping tool, it can use this tool for any task it is assigned to as long as the task requires or permits it.
    • Tasks: Tools assigned to a task are only usable within the context of that specific task. This setup allows for a high degree of control, ensuring the task uses the most appropriate tools. It also enables different agents to use the same task without necessarily needing to have those tools themselves.
  3. Control Over Execution

    • Agents: When an agent has tools, it controls when and how those tools are used. This setup is suitable when you want an agent to be autonomous and make decisions on when to leverage its abilities. It’s ideal for agents with expertise or unique skills that require specific tools frequently.
    • Tasks: Assigning tools to tasks grants control over tool usage to the task definition itself. This method is beneficial when a task requires specific actions, such as querying a database or making an API call, that need precise tool usage. This setup allows different agents to perform the task while adhering to the tool requirements set by the task.
  4. Tool Management and Overlap

    • Agents: If multiple agents have the same tool, each will manage its instance of that tool, potentially leading to redundant tool usage if not carefully planned. This situation requires mindful management to avoid inefficiencies.
    • Tasks: When tools are task-specific, the tools are only instantiated when the task is executed. This can lead to more streamlined usage and fewer conflicts, as tools are utilized only in the context where they are explicitly needed.

5.2 Deterministic vs. Probabilistic Tool Usage

  • Tools at the Task Level (Deterministic Usage):
    • When a tool is assigned directly to a task, it is guaranteed to be used whenever that task is executed, assuming the task’s logic calls for it. This deterministic nature means that the task’s execution path explicitly involves the tool, ensuring consistent and predictable behavior every time the task runs.
    • For example, if a task is designed to query a database, and it has a database connector tool assigned to it, the task will always invoke this tool as part of its execution flow. The tool’s usage is pre-defined and non-negotiable, ensuring the task’s requirements are consistently met.
  • Tools at the Agent Level (Probabilistic Usage):
    • Tools assigned to agents, on the other hand, operate more probabilistically. This means that even though an agent possesses a tool, it may choose to use it or not based on its internal logic, task requirements, or decision-making process during task execution.
    • An agent with a data analysis tool, for example, might not always use it for every task it handles. The agent’s behavior is influenced by factors like task descriptions, agent backstory, goals, or even prior outcomes, making tool usage more dynamic and less predictable.

5.2.1 Why This Difference Matters

  1. Control and Predictability:
    • Task-Level Tools: Provide control and predictability since you know exactly when and how the tools will be used. This is crucial for tasks where specific actions must always occur, ensuring reliable task completion.
    • Agent-Level Tools: Offer flexibility and adaptive behavior, but at the cost of less control. Agents decide when a tool is necessary, allowing for more creative problem-solving but also introducing variability.
  2. Design Implications:
    • If you need consistency in how a tool is used, place it at the task level. This approach is suitable for scenarios where specific tool operations are non-negotiable parts of the task.
    • If you prefer autonomy and want the agent to decide the best approach, assign tools at the agent level. This setup is ideal for roles requiring adaptive strategies and decision-making.

5.2.2 Practical Scenario

  • Deterministic Example: A data extraction task that always requires accessing a specific API would have the API connector tool assigned to it. Every time the task runs, it will use this tool, ensuring the data is retrieved consistently.
  • Probabilistic Example: An agent responsible for gathering insights from web data may or may not use a web scraper tool depending on the task’s demands, previous information gathered, or the agent’s strategy at that moment.

5.3 Incorporating Third-Party Tools

CrewAI's ability to support not only its native tools but also third-party tools from LangChain and LlamaIndex offers significant advantages.

  • ✦ This flexibility allows users to leverage a broader range of functionalities and integrations, enhancing the overall versatility and capability of the platform.

  • ✦ Developers are not confined to the tools provided by CrewAI alone; they can seamlessly integrate and utilize the best tools available in the market, tailored to their specific needs.

    Screenshot of a collection of LlamaIndex tools (integrations)

    In our walkthrough notebook, we have tried a more advanced example that uses toolkits (a suit of tools) from LangChain to create a tool that can manipulate and analyze tabular data by actually running Python code.

  • ✦ This tool uses the pandas library to manipulate the data and the ChatOpenAI agent to run the code.

  • ✦ While the example is a bit more complex, but we think it's worth to include it because the simplier examples (using a single tool from LangChain) are already well documented in CrewAI's documentation.

  • ✦ The toolkits are usually much more powerful and can be used to achieve more complex tasks, but we have yet to come across a comprehensive documentation on how to incorporate them into CrewAI's agents.

  

from langchain.agents import Tool
from langchain.agents.agent_types import AgentType
from langchain_experimental.agents.agent_toolkits import create_pandas_dataframe_agent

from langchain_openai import ChatOpenAI
import pandas as pd
  

df = pd.read_csv("https://raw.githubusercontent.com/pandas-dev/pandas/main/doc/data/titanic.csv")

  

pandas_tool_agent = create_pandas_dataframe_agent(
	llm=ChatOpenAI(temperature=0, model='gpt-4o-mini'),
	df=df,
	agent_type=AgentType.OPENAI_FUNCTIONS,
	allow_dangerous_code=True # <-- This is an "acknowledgement" that this can run potentially dangerous code
)

  

# Create the tool
pandas_tool = Tool(
	name="Manipulate and Analyze tabular data with Code",
	func=pandas_tool_agent.invoke, # <-- This is the function that will be called when the tool is run. Note that there is no `()` at the end
	description="Useful for search-based queries",
)

For more info on how to use:

for more info about Tools, such as the list of tools or how to create your own tool, see https://docs.crewai.com/core-concepts/Tools/#introduction