Skip to content

🤝 CrewAI Framework

CrewAI is a powerful, production-grade framework for orchestrating role-playing autonomous AI agents. Through structured collaboration, agents work together to execute complex, multi-step workflows, simulating a highly cohesive team of human specialists.


📊 Tool Datasheet

MetricDetails
Tool NameCrewAI
CategoryMulti-Agent Orchestration Framework
PurposeBuilding role-based, collaborative agent groups that delegate tasks, share memory, and execute sequential or hierarchical workflows.
LicenseMIT License
Core LanguagesPython

🛠️ Capabilities

  1. Role-Based Agent Design: Define agents with unique personas, backstories, specific goals, and fine-tuned sets of tools.
  2. Autonomous Task Delegation: Agents can dynamically delegate sub-tasks to other specialist agents in the crew depending on the runtime conditions.
  3. Flexible Workflows: Execute agent pipelines sequentially (one agent passes output to the next) or hierarchically (a manager agent orchestrates and validates workers).
  4. Short & Long-Term Memory: Built-in context caching, short-term session memory, and PostgreSQL/pgvector-backed long-term memory for recall.
  5. Native Tool Bindings: Easy integration with LangChain tools, custom Python functions, and web search/database interfaces.

🧭 Step-by-Step Tutorial & Setup

This tutorial guides you through building a collaborative Research & Writing crew utilizing Google Gemini to generate business intelligence briefs.

1. Environment Activation

Seed your Conda virtual environment and install dependencies:

bash
conda activate ai_dev
uv pip install crewai

2. Configure Environment Variables

Create or update your .env file at the root of your workspace:

text
GEMINI_API_KEY="your_gemini_api_key_here"

3. Create the Agent Crew Script

Create a new file named market_crew.py to define your agents and tasks: Path: ~/AI_BOOTCAMP/labs/crew_agent/market_crew.py

python
import os
from dotenv import load_dotenv
from crewai import Agent, Task, Crew, Process, LLM

### Load environment configurations
load_dotenv()

### Initialize Gemini LLM configuration
gemini_llm = LLM(
    model="gemini/gemini-1.5-flash",
    api_key=os.environ.get("GEMINI_API_KEY")
)

### Define Agent 1: Market Analyst Specialist
analyst = Agent(
    role="Senior Market Analyst",
    goal="Extract and analyze emerging trends in AI agent technology.",
    backstory="You are a data-driven market analyst who excels at parsing complex tech developments into actionable insights.",
    verbose=True,
    memory=True,
    llm=gemini_llm
)

### Define Agent 2: Technical Writer
writer = Agent(
    role="Technical Content Writer",
    goal="Translate raw tech analytics into clear, engaging executive summaries.",
    backstory="You are an expert technical editor who turns complex technical reports into high-level strategic business guides.",
    verbose=True,
    llm=gemini_llm
)

### Define Task 1: Research Trend Analysis
research_task = Task(
    description="Analyze the top 3 trends in open-source AI multi-agent orchestration frameworks for Q2 2026.",
    expected_output="A bulleted market research summary detailing capabilities, developer adoption, and key use cases.",
    agent=analyst
)

### Define Task 2: Executive Draft Compilation
draft_task = Task(
    description="Based on the market research summary, draft an executive brief highlighting strategic business opportunities.",
    expected_output="A clean, markdown-formatted executive brief suitable for C-suite presentation.",
    agent=writer
)

### Form the Crew and execute the process
tech_crew = Crew(
    agents=[analyst, writer],
    tasks=[research_task, draft_task],
    process=Process.sequential,
    verbose=True
)

if __name__ == "__main__":
    print("--- Starting CrewAI Orchestration Loop ---")
    result = tech_crew.kickoff()
    print("\n--- Final Compiled Brief Output ---\n")
    print(result)

📈 Advanced Orchestration: Hierarchical Process & Managers

For workflows requiring rigorous quality control or dynamic execution routing, CrewAI supports hierarchical processes. In this setup, you define worker agents and assign a manager agent (such as Gemini 1.5 Pro for complex reasoning) to coordinate task assignments and review final outputs before completion.

Python Code Example (Hierarchical Crew):

Path: ~/AI_BOOTCAMP/labs/crew_agent/hierarchical_crew.py

python
import os
from dotenv import load_dotenv
from crewai import Agent, Task, Crew, Process, LLM

load_dotenv()

### Initialize the manager LLM (Gemini 1.5 Pro) and worker LLM (Gemini 1.5 Flash)
manager_llm = LLM(
    model="gemini/gemini-1.5-pro",
    api_key=os.environ.get("GEMINI_API_KEY")
)

worker_llm = LLM(
    model="gemini/gemini-1.5-flash",
    api_key=os.environ.get("GEMINI_API_KEY")
)

### Workers are defined with their specific LLM instances
analyst = Agent(
    role="Data Analyst",
    goal="Gather raw financial metrics.",
    backstory="Specialist in tracking corporate balance sheets.",
    verbose=True,
    llm=worker_llm
)

writer = Agent(
    role="Report Writer",
    goal="Write executive reports.",
    backstory="Expert in distilling metrics into summaries.",
    verbose=True,
    llm=worker_llm
)

### Define tasks to be orchestrated by the manager
task1 = Task(
    description="Fetch financial metrics for Q1 2026.",
    expected_output="A table of revenue, margins, and operating expenses."
)

task2 = Task(
    description="Compile metrics into a 1-page financial summary.",
    expected_output="An executive markdown summary outlining margins."
)

### Compile hierarchical crew
financial_crew = Crew(
    agents=[analyst, writer],
    tasks=[task1, task2],
    process=Process.hierarchical,
    manager_llm=manager_llm,
    verbose=True
)

if __name__ == "__main__":
    financial_crew.kickoff()

📐 Spec-Driven Agent Design (Integrating Specify with CrewAI)

Combining the Specify framework with CrewAI creates a standardized pathway from abstract product requirements to fully working multi-agent systems:

Translation Protocol:

  1. User Stories to Tasks: Each prioritized user story from spec.md (e.g., User Story 1 - Fetch Balance Sheets) maps directly to a CrewAI Task definition, specifying the required details and expected markdown outputs.
  2. Actors to Agents: The primary actors or systems identified in the spec.md requirements translate to CrewAI Agent personas. If the spec requires database extraction and report compilation, you instantiate a database agent and a writer agent.
  3. Success Criteria to Quality Gates: The technology-agnostic outcomes defined in Success Criteria become the verification checks run by your CrewAI Manager agent or programmatic assertions inside test scripts.

💡 Sample Ideation to Test

To test the capabilities of this framework, try customizing your crew with these ideas:

  1. Introduce a Quality Editor: Add a third agent called "Senior Editor" with the task to audit the writer's report, check for bias, and request changes. Use hierarchical orchestration (Process.hierarchical) with a manager agent overseeing all tasks.
  2. Integrate Custom Database Tools: Create a custom tool that wraps PostgreSQL queries on agent_memories using psycopg2 or SQLAlchemy, allowing your research agent to fetch context from your relational store.
  3. SDD Pipeline Prototype: Write a script that reads a Specify spec.md file, parses the functional requirements using Pydantic, and dynamically spawns CrewAI Agents and Tasks matching the parsed requirements structure.