Building Your First Collaborative AI Crew

Learn the basics of AI agents, what they are, and how they're changing the way we interact with technology.

Hashan Ranasinghe5 min read
Share

If this is the first article you are seeing, I highly recommend that you go through the first article to get a clear idea about CrewAI, because now we are going to dive deep into CrewAI.

So, let's look at the definitions of the components of the CrewAI framework.

Crew

A crew in CrewAI represents a collaborative group of agents working together to achieve a set of tasks. Each crew defines the strategy for task execution, agent collaboration, and the overall workflow.

Agent

An agent is an autonomous unit that can perform specific tasks, make decisions based on its role and goal, use tools to achieve objectives, and work with other agents.

Tasks

A task provides all the necessary details for execution, such as a description, the responsible agent, required tools, and more, facilitating a wide range of action complexities.


Great! Let's get started with the development. First, we need to install a few things on your system.

If you haven't installed Python yet, install Python first. Then, you need to install uv on your system.

Install uv

On macOS / Linux

Use curl to download the script and execute it with sh:

curl -LsSf https://astral.sh/uv/install.sh | sh

If your system doesn't have curl, you can use wget:

wget -qO- https://astral.sh/uv/install.sh | sh

On Windows

Use irm to download the script and iex to execute it:

powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"

If you run into any issues, refer to UV's installation guide for more information.

Install CrewAI 🚀

Run the following command to install the CrewAI CLI:

uv tool install crewai

To verify that CrewAI is installed, run:

uv tool list

You should see something like:

crewai v0.102.0
- crewai

Awesome! You're all set to start your first CrewAI project.

Now, go to your project folder, open the terminal there, and type the following command to create your first CrewAI project:

crewai create crew <your_project_name>

Next, you'll need to choose the provider for the LLM model you want to use. You can go with OpenAI for simplicity, or if you prefer a free development API key, you can choose Gemini. You can also use a local model deployed on your machine, but honestly, using OpenAI or Gemini is usually the easiest option.

Then, you have selected the model that was provided by the user. I selected gemini/gemini-2.5-flash-preview-04-17.

However, this model is not working, and you need to change the model in the .env file to:

gemini/gemini-2.5-flash

Then, simply enter the API key for the model you selected by copying and pasting it.

Great! Your project has been successfully created. Now, open it in VS Code. You'll see a project structure like this.

Next, create a .venv in your project, activate it, and then run your project:

uv pip install .

This will install all the relevant packages from pyproject.toml into your .venv.

Okay, let's start by looking at the main.py file. First, we'll clean it up a bit. Don't worry—the things we're removing aren't useless; we're just starting simple. Go ahead and remove train(), replay(), and test().

After that, the file will only have the main function, which is enough for now.

In short, this file is the entry point of your CrewAI project. This means you can use it to run your crew locally and provide the initial inputs that the crew needs to execute.


Next, let's check out the crew.py file. You'll notice a lot of comments in there—feel free to remove them, because I'm going to explain everything step by step.

You can see some decorators: @CrewBase, @agent, @task, and @crew.

@CrewBase Decorator

This decorator turns your Python class into a CrewAI project container. In simple terms, it loads the agent_config and task_config, and collects all agents and tasks defined with decorators. This makes it easy to generate a crew object from the class.

@agent

This decorator marks a method inside the class as an agent factory. In other words, each method decorated this way should return an instance of an agent.

@task

This decorator marks a method as a task factory. That means each method decorated with @task should return a task object.

@crew

This decorator marks a method that builds and returns a crew object.


Now, let's take a look at the problem this crew is designed to solve. Simply put, it takes the inputs, does some research, analyzes the information, and then creates a report.

You'll notice there are two agents and two tasks, each related to one another.

Next, let's go through the files in the config folder. You'll find two YAML files: agents.yml and tasks.yml.

agents.yml

First, let's open agents.yml. I recommend reading the prompts in this file carefully—they're important for understanding how the agents work.

You can see that the agent names mentioned in crew.py are defined here. Each agent has three parts:

  • Role: The role defines what the agent does and their area of expertise. You must clearly provide the role of the agent.
  • Goal: The goal directs the agent's efforts and shapes their decision-making process. You should clearly define what the agent is trying to achieve.
  • Backstory: The backstory gives depth to the agent, influencing how they approach problems and interact with others.

You'll also notice the {topic} variable here. Remember the inputs we mentioned in main.py? Go back and take a look at that code. The topic variable is how we provide those inputs to the agents.


Now, go to the tasks.yml file. There are two tasks, each related to one of the agents defined in agents.yml.

Each task has two parts:

  • Description: The description should focus on what to do and how to do it.
  • Expected output: The expected output should define what the final result should look like.

At the bottom of each task, the agent related to the task is mentioned.


Alright! By now, I hope you have a good idea about this CrewAI project. Now, let's go back to the crew.py file, because we need to make some updates there.

Think about it like this: when we research something, we usually search the internet. So how does research_agent do that? Right now, the agent only has a brain—meaning it only knows what it already knows.

To help the agent search online, we need to give it a tool. The CrewAI team has already developed an inbuilt tool using Tavily. All we need to do is import it—super simple!

First, go to https://www.tavily.com/, sign up, and get your API key. You'll get 1,000 free credits for development.

Then, add that key to your .env file. After that, you can use the tool like this:

from crewai_tools import TavilySearchTool

Define that tool under the agent that requires it like this:

@agent
def researcher(self) -> Agent:
    return Agent(
        config=self.agents_config['researcher'],  # type: ignore[index]
        verbose=True,
        tools=[TavilySearchTool()]
    )

Now, you can run your crew and see what happens!

Now, you'll get to see your crew in action! This will give you a clear idea of how the agents work together and use the tools. Just scroll through the terminal and check out the logs—I think you'll find it really interesting.

Your first crew is now up and running! You can try changing the inputs, see the outputs, and even tweak the prompts for tasks and agents.

Just keep an eye on your usage: the free credits for the Tavily Search API and your LLM model API key can run out if you go over.


Next, let's check out another article on developing a comprehensive CrewAI project.