Login Sign Up

Building an Agent Crew with CrewAI

Introduction to CrewAI

CrewAI is a multi-agent system designed for enterprise applications, offering a structured and robust framework. Unlike AutoGen, CrewAI operates without a user proxy agent, allowing agents to work collaboratively on assigned tasks.

CrewAI System Components

CrewAI supports two processing models:

  • Sequential Processing: Agents complete tasks in a step-by-step manner.
  • Hierarchical Processing: Tasks are managed with a structured hierarchy.

Creating a Jokester Crew in CrewAI

CrewAI requires detailed setup, enabling greater control. The configuration includes roles, goals, verbosity, memory, backstory, delegation, and additional tools.

Installing CrewAI

pip install crewai

Defining Agents

Example agents in a humor-based task include:

Assigning Tasks

Each agent is assigned a task with a clear goal and expected output:

from crewai import Agent

senior_joke_researcher = Agent(

    role="Senior Joke Researcher",

    goal="Analyze humor trends and identify the funniest elements of a given topic.",

    verbose=True

)

joke_writer = Agent(

    role="Joke Writer",

    goal="Craft jokes based on research findings and deliver them in a humorous style.",

    verbose=True

)
from crewai import Task

research_task = Task(

    name="Humor Research",

    description="Identify the most humorous elements of a given topic.",

    agent=senior_joke_researcher

)

writing_task = Task(

    name="Joke Writing",

    description="Write a joke based on the research findings.",

    agent=joke_writer,

    depends_on=[research_task]  # Sequential processing

)

Building the Crew

A CrewAI system integrates agents and tasks with additional settings like memory, cache, and execution limits. Running the system generates humorous AI-related jokes.

from crewai import Crew

jokester_crew = Crew(

    agents=[senior_joke_researcher, joke_writer],

    tasks=[research_task, writing_task],

    memory=True  # Enables memory for better task handling

)

# Run the crew

jokester_crew.kickoff()

Observing Agent Performance with AgentOps

AgentOps is an observability tool that tracks CrewAI agent interactions, providing insights such as execution duration, token usage, and estimated costs.

Setting Up AgentOps

To set up the AgentOps on your system, you should follow these instructions:

Install the package:

> pip install agentops

or with CrewAI:

> pip install crewai[agentops] 

Obtain an API key from AgentOps and add it to the .env file:

AGENTOPS_API_KEY="your API key" 

Integrate AgentOps into the CrewAI Script

import agentops  

agentops.init()

Monitoring Agent Performance

Running the script allows users to track agent activities in the AgentOps dashboard, analyzing system efficiency and cost.

jokester_crew.kickoff()  # This execution is now monitored by AgentOps