Exploring AI Agents: An Introduction to CrewAI

Sudheer RanjanSudheer Ranjan
6 min read

Evolution of AI Agents from LLMs

You might have experimented with Large Language Models from OpenAI and Hugging Face. They excel at predicting the next word using the vast data they are trained on. They are great for simple tasks, but what about complex tasks? If you think the answer needs changes, you would likely tweak the prompts (with Prompt Engineering skills) and add context to the LLM to get the desired output. However, this means we have to engage in the interactions ourselves. How do we automate this or make the LLM autonomous? Given that LLMs can put together words that make sense, agents are a way to help LLMs improve on their own.

Agentic Automation

Automation in traditional software development involves using if-else conditions to achieve an output. In AI, however, we simply provide a map. Unlike traditional software development, AI handles fuzzy inputs, performs fuzzy transformations, and produces fuzzy outputs. Additionally, the results can vary each time.

CrewAI is one such Open-source framework and platform for orchestrating role-playing, autonomous AI agents. CrewAI's building blocks include Agents, Tasks, and Crew, which we will discuss in detail.

CrewAI Mindmap

Image Credits: CrewAI

Agents

While Andrew Ng highlights Reflection, Tool Use, Planning, and Multi-agent collaboration in his blog Batch, CrewAI adds a few more patterns. Think of these patterns as a way to interact with an LLM using a wrapper. These patterns help the agent complete complex tasks. Let's go through each of these.

  1. Role Playing: Role-playing of Agent helps the LLM set context, leading to better results.

  2. Focus: Though we can provide a long context to the LLM with increasing context windows, too much information can cause the LLM to hallucinate. Narrowly focused tasks help the agent perform better.

  3. Tools: You might have seen LLMs failing miserably in mathematical calculations. LLMs have overcome this by converting the problem to Python and executing it. What about other scenarios where an LLM can't handle the task? Here, the agent should be able to access tools like specific websites, the internet, or files. However, too many tools can make it difficult for the agent (especially with a small model) to choose the right one. Always provide the key tools. With CrewAI, you can decide whether to give the tools at task level or agent level.

  4. Cooperation: Agents role-play and have conversations in their roles, which helps in getting better results. Taking feedback and delegating tasks also improve their output. Delegation doesn't need to happen every time; agents should decide to delegate based on the problem's complexity. Collaboration among multiple agents can occur sequentially, hierarchically (like a manager delegating tasks), or asynchronously.

  5. Guardrails: Unlike traditional software, AI systems have to handle unclear inputs, perform unclear transformations, and produce unclear outputs. Some tasks might end up in a loop. Guardrails help prevent your agent from getting off track and guide them to stay focused.

  6. Memory: Agents should remember the past, learn from it, and apply that knowledge to future tasks. Agents with memory can learn from their mistakes. CrewAI specifically offers three types of memory out of the box: short memory, long memory, and entity memory. With memory, you can achieve more reliable results and reduce randomness.

    1. Short Memory: This memory/context is shared across all agents during crew's execution. Share intermediate information even before providing task completion output with other agents.

    2. Long Memory: This stays even after crew's execution, stored in Database, agents learn from its previous execution for future tasks. This leads to self improving agents.

    3. Entity: This memory stays during crew's execution, it memorizes subjects being discussed.

Multi Agent Systems

  1. Multi Agent Systems are a group of agents in which each agent is customized to do one single thing and do it well.

  2. Each agent can run from different LLMs.

  3. They have ability to delegate or give feedback.

Basic components/classes of CrewAI

  1. Agent: This class requires the role and goal of the agent along with some backstory of the task.

  2. Task: This class requires the description of the task, expected output and the agent to work on.

  3. Crew: Crew is something that puts together both agents and tasks. By default crew operates sequentially, but we assign weights to make it work asynchronously.

What makes a great tool for an AI Agent?

While an LLM on its own doesn't know everything we ask, we can give it tools to complete tasks. When creating a chatbot for specific domain data, we either fine-tune the LLM to that domain or use Retrieval Augmented Generation (RAG) to query the domain data. Think of this as a tool. More tools like this need to be created for various use cases. Below are some qualities you should look for when creating or using tools.

  1. Versatile: A tool connects unclear input to unclear output. It should handle any input and any output that LLMs generate.

  2. Fault-tolerant: Exceptions should not stop the execution. Instead, they should prompt the agent to find alternative ways to achieve the goal.

  3. Caching Strategy: Tools are used to call internet services, internal/external services, and API services. The same tool with the same set of arguments is cached. This prevents unnecessary requests, saving requests per second and execution time. CrewAI offers cross-agent caching, allowing different agents to share the cache layer instead of making the same API call again.

With CrewAI we can build our own custom tool.

Examples of tools

  1. Search the internet

  2. Scrape a website: RAG over a website

  3. Connect to a Database

  4. Call an API

  5. Send Notifications

How to decide the Tasks

This offers a mental framework of how we should think of solving a complex problem. Deciding how we frame the Tasks and Agents is key in solving the problem.

  1. What is the goal? What is the process?

  2. What kind of individuals do I need to hire to complete this task? These individuals become the agents.

  3. What processes and tasks do I expect the individuals on my team to perform?

    These become tasks for those agents.

  4. A few other parameters, like clear and concise expectations and setting the context, make up a detailed task.

CrewAI represents a significant advancement in AI, moving beyond simple language models to sophisticated AI agents capable of handling complex tasks autonomously. Key features include agentic automation, role-playing for context, focused tasks to reduce hallucination, and access to external tools. Cooperation among agents and the use of guardrails ensure efficiency and accuracy. CrewAI's memory system—short-term, long-term, and entity memory—enables agents to learn from past experiences. Multi-agent systems specialize in specific tasks and collaborate seamlessly. Essential components include agents, tasks, and crews. Effective tools should be versatile, fault-tolerant, and utilize caching strategies. CrewAI's approach helps solve complex problems by defining clear goals, identifying necessary agents, and outlining tasks and expectations.

References:

Andrew Ng's Article on Agentic Design Patterns

Multi AI Agent Systems with CrewAI course on Deeplearning.ai

0
Subscribe to my newsletter

Read articles from Sudheer Ranjan directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Sudheer Ranjan
Sudheer Ranjan

I am a Senior Data Scientist at Optum (UnitedHealth Group)