Skip to content
English
  • There are no suggestions because the search field is empty.

CrewAI

CrewAI is a powerful framework for creating and orchestrating AI agents that can collaborate to perform complex tasks.

CrewAI Agents

In the CrewAI framework, an Agent is an autonomous unit that can:

  • Perform specific tasks

  • Make decisions based on its role and goal

  • Use tools to accomplish objectives

  • Communicate and collaborate with other agents

  • Maintain memory of interactions

  • Delegate tasks when allowed

Note: CrewAI Framework is supported only with GPT-4.1, GPT-4o, GPT-4o mini, o3-mini, and o1 models.

Set Up the Environment

Install CrewAI and its dependencies:

1 pip install crewai crewai-tools
2 pip install 'crewai[tools]'

Agents in CrewAI can be defined using YAML configuration or directly in code. The recommended approach is to use YAML for better maintainability.

Define Agent Using YAML Configuration

To define aget using YAML configuration:

  1. Navigate to config/agents.yaml file.

  2. Define your agent in the agents.yaml file:

    1  researcher:
    
    2 role: "{topic} Senior Data Researcher"
    3 goal: "Uncover cutting-edge developments in {topic}"
    4 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."
    5

    6 reporting_analyst:
    7 role: "{topic} Reporting Analyst"
    8 goal: "Create detailed reports based on {topic} data analysis and research findings"
    9 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. Define task specifications for agents in the tasks.yaml file:

    1  research_task:
    
    2 description: >
    3 Conduct a thorough research about {topic}
    4 Make sure you find any interesting and relevant information.
    5 expected_output: >
    6 A list with 10 bullet points of the most relevant information about {topic}
    7 agent: researcher
    8

    9 reporting_task:
    10 description: >
    11 Review the context you got and expand each topic into a full section for a report.
    12 Make sure the report is detailed and contains any and all relevant information.
    13 expected_output: >
    14 A fully fledged report with the main topics, each with a full section of information.
    15 Formatted as markdown without '```
    16 agent: reporting_analyst
    17 output_file: report.md

     

Implement Agents

Integrate any of the Compass APIs. In the below example, GPT-4o has been used.

Define Compass LLM (GPT-4o) using the CrewAI framework:


1 from crewai import LLM

2 import os
3

4 MODEL_NAME = "gpt-4o"
5 MODEL_URL = "https://api.core42.ai/v1"
6 API_KEY = <api_key>

7

8 llm = LLM(
9 model = MODEL_NAME,
10 base_url = MODEL_URL,
11 api_key = API_KEY
12 )

You can test if the model is working using the code below:

1 sample_prompt = "Explain the concept of artificial intelligence in simple terms."

2 response = llm.call(sample_prompt)
3 print("Custom LLM Response:", response)

Two agents are created for reference: researcher and reporting_analyst.

Working sample code of building agents, defining tasks, and agentic functionality using CrewAI framework is given below:

1  import yaml

2 from crewai import Agent, Task, Crew
3

4 def load_config(file_path):
5 """Load configuration from a YAML file."""
6 with open(file_path, 'r') as file:
7 return yaml.safe_load(file)
8

9 def create_agent(config, name, topic, llm):
10 """Create an agent using the custom LLM."""
11 agent_config = config[name]
12 return Agent(
13 role=agent_config['role'].format(topic=topic),
14 goal=agent_config['goal'].format(topic=topic),
15 backstory=agent_config['backstory'].format(topic=topic),
16 verbose=True,
17 llm=llm,
18 )
19

20 def create_task(config, name, agent, topic):
21 """Create a task and assign it to an agent."""
22 task_config = config[name]
23 return Task(
24 description=task_config['description'].format(topic=topic),
25 expected_output=task_config['expected_output'].format(topic=topic),
26 agent=agent
27 )
28

29 def main(topic):
30 """Main function to set up and run the crew."""
31 # Load configurations
32 agents_config = load_config('config/agents.yaml')
33 tasks_config = load_config('config/tasks.yaml')
34

35 # Create agents using the custom LLM
36 researcher = create_agent(agents_config, 'researcher', topic, llm)
37 reporting_analyst = create_agent(agents_config, 'reporting_analyst', topic, llm)
38

39 # Create tasks for the agents

40 research_task = create_task(tasks_config, 'research_task', researcher, topic)
41 reporting_task = create_task(tasks_config, 'reporting_task', reporting_analyst, topic)
42

43 # Create and run the crew
44 crew = Crew(
45 agents=[researcher, reporting_analyst],
46 tasks=[research_task, reporting_task],
47 verbose=True # Set verbosity to True for detailed logs
48 )
49

50 result = crew.kickoff()
51 print(result)
52

53 if __name__ == "__main__":
54 topic = "Quantum Computing" # You can change this to any topic you want to research
55 main(topic)

Response

Markdown file


Configurable Agent Parameters

Key parameters for agent configuration include:

  • Critical Parameters: role, goal, and backstory

  • Memory and Context: memory, respect_context_window, knowledge_sources

  • Execution Control: max_iter, max_execution_time, max_rpm, max_retry_limit

  • Code Execution: allow_code_execution, code_execution_mode

  • Templates: system_template, prompt_template, response_template

Best Practices

  1. Use YAML configuration for cleaner, more maintainable agent definitions.

  2. Assign specialized roles to different agents for complex tasks.

  3. Utilize performance metrics analysis to fine-tune agents for specific contexts.