Use CrewAI to build a useful AI Agent with just a few lines of code.

At the beginning of this year, I heard someone saying that 2025 was going to be the year of agentic AI when we would start using AI to perform tasks for us instead of only responding to prompts.

But first of all, let us define an AI agent.

AI agent is a Large Language Model (LLM) with the ability to make decisions and take actions to achieve specific goals. The idea is to perform tasks based on prompts and predefined rules.

AI agents are everywhere — from chatbots handling customer service to recommendation systems suggesting your next favorite show. Each day, these models become more advanced, tackling increasingly complex tasks and giving us back something precious: time.

I have been seeing agents being created everywhere and new tools oriented in the sense of making the creation of agents a common task. Whether it’s no-code platforms where you can build an agent by simply dragging and dropping boxes or powerful programming tools like Python, creating an AI agent is becoming a common and accessible task.

One of those tools that has caught my attention lately due to its simplicity of use: CrewAI.

In this article, we’ll explore how to build our first AI agent using Crew AI, a tool that makes the process quick and approachable — even if you’re new to the field. All it takes is some Python knowledge.

This agent will be able to research a given product on the internet and return a comparative table of the 3 top choices, suggesting the best one for purchase.

Let’s dive in and get you building in minutes!

Code

Creating an AI agent with this template is really simple. We will go through 4 steps that make it happen.

  1. Choose our Large Language Model to operate our agent.
  2. Define our agent’s role and goal.
  3. Define the agent’s task.
  4. Connect the pieces and run the agent

We just need to import a couple of modules first.

# Importing modules
from crewai import Agent, Task, Crew, Process, LLM
from crewai_tools import SerperDevTool
import os

Moving on to the code.

Choosing the LLM

The AI agent must have an AI behind it to perform the task. We can choose the model to be used. We will use the Qwen-QWQ model, which is a medium-sized reasoning model created by Alibaba Cloud, and trained on their data. 

According to Hugging Face, when compared to similar instruction-tuned models, QWQ can achieve great performance against state-of-the-art reasoning models, e.g., DeepSeek-R1, o1-mini.

Well, selecting the LLM can be done with the next code snippet.

# Defining the LLM
llm = LLM(
    model="groq/qwen-qwq-32b",
    api_key= os.environ.get("GROQ_API_KEY"),
    temperature=0.7
)

The next step is where we define the agent’s role.

Agent’s Role and Goal

LLM, AI agents… all of those are still machines. Thus, to make them work well, we have to give them good instructions. Think of it as if you were explaining the job to a new hire. 

Every time someone starts a new job, they expect to receive (at least) initial instructions on what to do, how to behave, where to look for information, and where to get the data needed for their work. 

The agent is the same thing. So here is how we can set it up. Using the Agent() class, we can define the instructions:

  • role: What is the role to perform, e.g. researcher, analyst, concierge, assistant.
  • goal: What is the goal of this agent? In our example, we’re creating an agent to research, compare, and suggest the best purchase of a given product.
  • backstory: Tell the LLM how to play the role, giving detailed instructions like comparison criteria and how many options to return.
  • tools: Now this is very cool in CrewAI. There are so many tools to use. In our example, we’re using the SerperDevTool, an API that enables Google searches.
  • llm: here we point to the object with the chosen model created in the previous step.
# Agent Definition
researcher = Agent(
    role = "{topic} Products Researcher",
    goal= '''Compare the performance of different brands of {topic} based on review articles and news. Search for Best {topic} Brands''',
    backstory='''You are a product researcher the compares the performance of different brands of {topic}. 
    Present the best 3 on a table and recommend the best brand.''',
    tools=[SerperDevTool()],
    llm=llm
)

Cool. Let’s move on.

Agent’s Task

Now it is time to give more context about the task to be performed. 

We already explained the job role to the “new hire”. Now we have to give more instructions about this specific task to be performed. Once again, I invite you to think about when your manager asks you for a new task. It is expected that you receive some information to get it started.

The code is simple, using the class Task() .

  • description: describe the task. For example, compare using these criteria, where to search etc.
  • expected_output: tell the agent how to return the data, such as a report, a table, or a markdown notation text. Here I also used this field to restrict the searches to two times, because during my tests I learned that the agent can keep searching for a long time if it doesn’t decide that it has the answer.
  • agent: Point to the agent object created in the previous step.
# Define the research task for the Product Researcher
product_research_task = Task(
    description = '''Research the best {topic} brands and compare their price average, ratings and performance.''',
    expected_output='''Based on maximum 2 searches on Amazon and Google US, present a table comparing the price average,
    performance and overall rating of different brands of {topic} and 
    a recommendation for the best brand.''',
    agent = researcher
)

Let’s see how to connect the dots now and make our agent run.

Running the Agent

We have the pieces constructed. So, to connect them and make the agent run and perform the task, we can use the Crew() class. The arguments are:

  • agents: it takes the agents created. We created just a single one.
  • tasks: It also takes in a list. We have only the product_research_task to perform.
  • process: This takes the Process.sequential function to run everything in a sequence.
  • We will leave the verbose=True to show the agent working in our terminal (that’s really cool!)

Then we kick off by providing the agent with the topic and print the results.

# Launch the Product Researcher
def main(topic):
    crew = Crew(
        agents = [researcher],
        tasks = [product_research_task],
        process= Process.sequential,
        verbose = True
    )

    result = crew.kickoff(inputs={'topic': topic})

    print(result)

To run the agent:

if __name__ == "__main__":

    main(topic="Laptop PC")
Part of the verbose printed on screen by the agent. Image by the author.

Once it finishes running, you can see the response:

Agent’s result on the terminal. Image by the author.

And it’s markdown notation, so in GitHub you will see it properly formatted.

Wow, strong result.

Before You Go

This post brings us a nice addition to our skills. Building AI agents might become a common task in a near future. Time will tell.

I have a feeling that many boring tasks will start to be delegated to AI. Things like report formatting, Excel file cleaning and consolidation, meeting minutes, email follow up, all those tasks will probably be performed by agents sometime in the future.

Therefore, knowing how to create one can be useful. Certainly the ways we create agents might change, but knowing that right now, when it is beginning can give you a head start and put you in a better position as a data professional.

CrewAI is a great tool and I suggest you explore it more and deepen your knowledge. Explore their quickstart page, where there’s an awesome tool that automates the creation of an entire project template for your crew. It is very interesting!

There’s also a free course offered by Andrew Ng’s company (DeepLearning.AI) linked on their page.

GitHub Repository

https://github.com/gurezende/First-AI-Agent

Contact and Portfolio

Gustavo R Santos
I am a Data Scientist specializing in data analysis, machine learning, and visualization using Python, R, SQL, and…gustavorsantos.me

References

Open source
Connect with like-minded developers in our forum, contribute to the open-source project on GitHub, and collaborate on…www.crewai.com

Multi Agent Systems and how to build them
Master CrewAI for building multi-agent systems with our comprehensive course. Gain hands-on experience in AI…learn.crewai.com

Qwen/QwQ-32B · Hugging Face
We’re on a journey to advance and democratize artificial intelligence through open source and open science.huggingface.co

GroqCloud – Build Fast
Build Fast with GroqCloudconsole.groq.com

Quickstart – CrewAI
Build your first AI agent with CrewAI in under 5 minutes.docs.crewai.com