Learnixo

CrewAI Multi-Agents · Lesson 1 of 16

What is CrewAI and When to Use It?

What Is CrewAI?

CrewAI is an open-source Python framework for building multi-agent AI systems where each agent plays a defined role inside a coordinated crew. Instead of writing a single monolithic prompt or wiring together a chain of LLM calls manually, you declare a set of agents — each with a distinct role, goal, and backstory — and a set of tasks to be completed. CrewAI handles orchestration, context passing, and execution order.

The mental model is straightforward: think of a professional crew working on a project. A research analyst gathers data. A medical writer drafts a report. A compliance reviewer checks it. Each person has a job. CrewAI lets you replicate that structure in code.


The Problem CrewAI Solves

Single-agent systems hit limits quickly:

  • One prompt cannot hold all the context for a complex task
  • A single agent cannot specialize in multiple domains simultaneously
  • Long chains of reasoning collapse under hallucination pressure
  • Sequential tasks with handoffs are hard to express as a single prompt

CrewAI solves this by decomposing work into tasks assigned to specialist agents. Each agent works within its domain. Results flow from one agent to the next. The crew delivers a final output.


The Core Metaphor

Every CrewAI system has three core building blocks:

| Concept | What it is | Analogy | |---------|-----------|---------| | Agent | An LLM persona with a role, goal, and backstory | A team member | | Task | A unit of work with a description and expected output | An assignment | | Crew | The collection of agents and tasks plus execution rules | The team itself |

This is not just marketing language — these are actual class instances in the CrewAI library.


Installation

Bash
pip install crewai crewai-tools

CrewAI depends on LiteLLM for model access, which means it works with OpenAI, Anthropic, Azure OpenAI, Groq, Ollama, and dozens of other providers out of the box.

Set your API key before running any crew:

Bash
export OPENAI_API_KEY="sk-..."

Or in Python:

Python
import os
os.environ["OPENAI_API_KEY"] = "sk-..."

Your First Crew: 30 Lines

Python
from crewai import Agent, Task, Crew, Process

# Step 1  Define agents
researcher = Agent(
    role="Research Analyst",
    goal="Find accurate, up-to-date information on any topic",
    backstory=(
        "You are a seasoned research analyst with 10 years of experience "
        "synthesizing complex information into clear summaries."
    ),
    verbose=True,
)

writer = Agent(
    role="Technical Writer",
    goal="Turn research findings into clear, engaging prose",
    backstory=(
        "You specialize in transforming dense research into readable articles "
        "for a general professional audience."
    ),
    verbose=True,
)

# Step 2  Define tasks
research_task = Task(
    description="Research the current state of mRNA vaccine technology in 2026.",
    expected_output="A bullet-point summary of key developments, at least 5 points.",
    agent=researcher,
)

writing_task = Task(
    description=(
        "Using the research summary, write a 300-word article on mRNA vaccines "
        "suitable for a healthcare professional newsletter."
    ),
    expected_output="A polished 300-word article with a title and three paragraphs.",
    agent=writer,
)

# Step 3  Assemble and run the crew
crew = Crew(
    agents=[researcher, writer],
    tasks=[research_task, writing_task],
    process=Process.sequential,
    verbose=True,
)

result = crew.kickoff()
print(result)

Run this and you will see both agents work in sequence: the researcher completes its task, then the writer receives that output as context and writes the article.


Process Modes

CrewAI supports two execution strategies:

Sequential (Default)

Tasks execute in the order they are listed. The output of task N becomes available as context to task N+1.

Python
crew = Crew(
    agents=[researcher, analyst, writer],
    tasks=[task1, task2, task3],
    process=Process.sequential,  # default
)

Use sequential when your workflow is a clear pipeline: gather → analyze → write → review.

Hierarchical

A manager agent (backed by a separate LLM call) reads the task list and decides which agent handles which task. This is useful when tasks are dynamic or the routing logic is non-trivial.

Python
from crewai import LLM

crew = Crew(
    agents=[researcher, analyst, writer],
    tasks=[task1, task2, task3],
    process=Process.hierarchical,
    manager_llm=LLM(model="gpt-4o"),
)

Agents Have Personality

The backstory parameter is not decoration. CrewAI injects the role, goal, and backstory into the system prompt for each agent. This means you can meaningfully shape an agent's reasoning style:

Python
compliance_agent = Agent(
    role="Regulatory Compliance Reviewer",
    goal=(
        "Ensure all content meets FDA guidelines for pharmaceutical communications"
    ),
    backstory=(
        "You spent 15 years as a regulatory affairs specialist at a major pharma company. "
        "You are meticulous, conservative, and always cite the specific regulation "
        "you are referring to. You flag anything ambiguous rather than approving it."
    ),
    verbose=True,
)

This agent will behave very differently from a generic "reviewer" agent. The backstory primes the LLM's tone, caution level, and citation habits.


Tasks Are Contracts

The expected_output field is critical. It tells the LLM exactly what a completed task looks like:

Python
analysis_task = Task(
    description=(
        "Analyze the clinical trial data for Drug X and identify the top 3 safety signals."
    ),
    expected_output=(
        "A numbered list of exactly 3 safety signals. Each signal must include: "
        "the signal name, the incidence rate, and a one-sentence clinical interpretation."
    ),
    agent=analyst,
)

Vague expected outputs lead to vague results. Specific expected outputs guide the agent toward structured, usable responses.


How Agents Access Information

Agents can be equipped with tools — functions that let them search the web, read files, query databases, or call APIs. CrewAI ships with several ready-made tools:

Python
from crewai_tools import SerperDevTool, FileReadTool

search_tool = SerperDevTool()
file_tool = FileReadTool()

researcher = Agent(
    role="Research Analyst",
    goal="Find accurate information using available tools",
    backstory="You are a methodical researcher who always verifies facts.",
    tools=[search_tool, file_tool],
    verbose=True,
)

Tools are covered in depth in the "Giving Agents Tools" lesson.


CrewAI vs Building From Scratch

| Feature | DIY LLM calls | CrewAI | |---------|--------------|--------| | Agent personas | Manual system prompts | Declarative Agent class | | Task handoffs | Manual context injection | Built-in context passing | | Tool use | Manual function calling | tools=[] parameter | | Memory | Custom vector store code | memory=True parameter | | Execution order | Manual orchestration | Process.sequential / hierarchical | | Monitoring | Custom logging | Callbacks + verbose mode |

CrewAI does not eliminate complexity — it relocates it from orchestration boilerplate into agent and task definitions, which are easier to reason about and maintain.


What CrewAI Is Not

  • It is not a replacement for prompt engineering. Badly written goals and backstories produce bad results.
  • It is not magic parallelism. Sequential tasks still run one at a time.
  • It is not a vector database. Memory features use external storage under the hood.
  • It is not framework-agnostic. It is opinionated and works best when you embrace its abstractions.

Summary

CrewAI gives you a clean, role-based mental model for multi-agent AI:

  1. Define agents with roles, goals, and backstories
  2. Define tasks with descriptions and expected outputs
  3. Assign tasks to agents
  4. Choose a process mode (sequential or hierarchical)
  5. Call crew.kickoff() and collect results

The rest of this course goes deep on each of these steps — tools, memory, async execution, structured outputs, and running crews in production.