Build your first CrewAI Agent

Let’s create a simple crew that will help us research and report on the latest AI developments for a given topic or subject.

Before we proceed, make sure you have crewai and crewai-tools installed. If you haven’t installed them yet, you can do so by following the installation guide.

Follow the steps below to get crewing! 🚣‍♂️

1

Create your crew

Create a new crew project by running the following command in your terminal. This will create a new directory called latest-ai-development with the basic structure for your crew.

2

Modify your `agents.yaml` file

You can also modify the agents as needed to fit your use case or copy and paste as is to your project.

agents.yaml
# src/latest_ai_development/config/agents.yaml
researcher:
  role: >
    {topic} Senior Data Researcher
  goal: >
    Uncover cutting-edge developments in {topic}
  backstory: >
    You're a seasoned researcher with a knack for uncovering the latest
    developments in {topic}. Known for your ability to find the most relevant
    information and present it in a clear and concise manner.
      
reporting_analyst:
  role: >
    {topic} Reporting Analyst
  goal: >
    Create detailed reports based on {topic} data analysis and research findings
  backstory: >
    You're a meticulous analyst with a keen eye for detail. You're known for
    your ability to turn complex data into clear and concise reports, making
    it easy for others to understand and act on the information you provide.
3

Modify your `tasks.yaml` file

tasks.yaml
# src/latest_ai_development/config/tasks.yaml
research_task:
  description: >
    Conduct a thorough research about {topic}
    Make sure you find any interesting and relevant information given
    the current year is 2024.
  expected_output: >
    A list with 10 bullet points of the most relevant information about {topic}
  agent: researcher

reporting_task:
  description: >
    Review the context you got and expand each topic into a full section for a report.
    Make sure the report is detailed and contains any and all relevant information.
  expected_output: >
    A fully fledge reports with the mains topics, each with a full section of information.
    Formatted as markdown without '```'
  agent: reporting_analyst
  output_file: report.md
4

Modify your `crew.py` file

crew.py
# src/latest_ai_development/crew.py
from crewai import Agent, Crew, Process, Task
from crewai.project import CrewBase, agent, crew, task
from crewai_tools import SerperDevTool

@CrewBase
class LatestAiDevelopmentCrew():
  """LatestAiDevelopment crew"""

  @agent
  def researcher(self) -> Agent:
    return Agent(
      config=self.agents_config['researcher'],
      verbose=True,
      tools=[SerperDevTool()]
    )

  @agent
  def reporting_analyst(self) -> Agent:
    return Agent(
      config=self.agents_config['reporting_analyst'],
      verbose=True
    )

  @task
  def research_task(self) -> Task:
    return Task(
      config=self.tasks_config['research_task'],
    )

  @task
  def reporting_task(self) -> Task:
    return Task(
      config=self.tasks_config['reporting_task'],
      output_file='output/report.md' # This is the file that will be contain the final report.
    )

  @crew
  def crew(self) -> Crew:
    """Creates the LatestAiDevelopment crew"""
    return Crew(
      agents=self.agents, # Automatically created by the @agent decorator
      tasks=self.tasks, # Automatically created by the @task decorator
      process=Process.sequential,
      verbose=True,
    ) 
5

Feel free to pass custom inputs to your crew

For example, you can pass the topic input to your crew to customize the research and reporting to medical llms or any other topic.

main.py
#!/usr/bin/env python
# src/latest_ai_development/main.py
import sys
from latest_ai_development.crew import LatestAiDevelopmentCrew

def run():
  """
  Run the crew.
  """
  inputs = {
    'topic': 'AI Agents'
  }
  LatestAiDevelopmentCrew().crew().kickoff(inputs=inputs)
6

Set your environment variables

Before running your crew, make sure you have the following keys set as environment variables in your .env file:

7

Lock and install the dependencies

Lock the dependencies and install them by using the CLI command but first, navigate to your project directory:

8

Run your crew

To run your crew, execute the following command in the root of your project:

9

View your final report

You should see the output in the console and the report.md file should be created in the root of your project with the final report.

Here’s an example of what the report should look like:

In addition to the sequential process, you can use the hierarchical process, which automatically assigns a manager to the defined crew to properly coordinate the planning and execution of tasks through delegation and validation of results. You can learn more about the core concepts here.

Replay Tasks from Latest Crew Kickoff

CrewAI now includes a replay feature that allows you to list the tasks from the last run and replay from a specific one. To use this feature, run:

crewai replay <task_id>

Replace <task_id> with the ID of the task you want to replay.

Reset Crew Memory

If you need to reset the memory of your crew before running it again, you can do so by calling the reset memory feature:

crewai reset-memory

This will clear the crew’s memory, allowing for a fresh start.

Deploying Your Project

The easiest way to deploy your crew is through CrewAI Enterprise, where you can deploy your crew in a few clicks.