AI Tools

How to Build a Multi-Agent AI System with CrewAI

Learn how to orchestrate autonomous AI agents using CrewAI. Complete guide to defining roles, tools, and sequential workflows for local LLMs.

July 28, 20264 min read3,741 views
How to Build a Multi-Agent AI System with CrewAI
Advertisement

The Shift to Multi-Agent Systems

Single-agent AI systems (like raw chatbots or basic auto-looping scripts) often struggle with complex, multi-step workflows. If you ask a single agent to research a topic, write a detailed article, check the facts, and output clean markdown, the model is prone to lose context, skip instructions, or hallucinate references.

To solve this, developers are building **multi-agent AI systems**. The core philosophy is **role specialization**: instead of having one model do everything, you create a team of narrow, specialized agents (e.g. a Researcher, a Writer, and a Code Reviewer) that pass data to each other. The framework that has popularized this pattern is CrewAI. This guide walks you through the core CrewAI classes, explains task delegation, and provides a complete Python setup tutorial.

Key Takeaway: CrewAI structures AI collaboration using agents, tasks, and crews. This modularity models real-world team structures, drastically improving task success rates.

Understanding the Core CrewAI Classes

CrewAI structures multi-agent collaboration using three foundational building blocks:

  • Agent: Represents an individual AI persona. You define its **Role** (e.g., 'Senior Research Analyst'), its **Goal** (e.g., 'Find emerging trends in quantum computing'), and its **Backstory** (defining its personality, constraints, and expertise).
  • Task: The specific assignment given to an agent. You define the **Description of what needs to be done, the **Expected Output** format, and assign it to a specific Agent.
  • Crew: The orchestration layer. It groups your Agents and Tasks together and defines the **Process** execution workflow (e.g. Sequential, where tasks run one after another, or Hierarchical, where a manager agent directs the workload).

Agent Orchestration Topologies

CrewAI supports multiple workflow models depending on the complexity of your task:

Process TypeData Flow ModelBest Use Case
SequentialTask A output feeds into Task B input directlyLinear pipelines (e.g. Write -> Translate -> Edit)
HierarchicalManager agent delegates tasks to sub-agentsCreative projects requiring feedback loops
Consensus (Custom)Agents evaluate each other's outputs and voteFact-checking & code auditing pipelines

Memory Systems in CrewAI

To enable agents to collaborate effectively over long periods, CrewAI implements a tiered memory subsystem:

  • Short-Term Memory: Allows agents to share transient state context during a single execution run.
  • Long-Term Memory: Persists task execution results in a local database, allowing agents to learn from past executions and improve their search paths over time.
  • Entity Memory: Extracts key definitions, names, and concepts to build a persistent glossary for the crew.

Custom Tool Engineering in CrewAI

While CrewAI provides pre-built search and file reading tools, real-world applications require building custom tools to connect agents to proprietary APIs or databases. You can build custom tools by extending CrewAI's `BaseTool` class:

# Custom CrewAI Tool implementation
from crewai_tools import BaseTool

class LocalDbFetchTool(BaseTool):
    name: str = "Local DB Fetcher"
    description: str = "Fetches user metrics from local SQLite database."

    def _run(self, user_id: str) -> str:
        # Local custom Python query logic goes here
        return f"User {user_id} active metrics: 89% engagement score."

tool_instance = LocalDbFetchTool()

Managing Crew Task Callbacks & Event Hooks

For complex logic loops, you can define **Callbacks** that trigger when a task finishes. This is ideal for logging telemetry, alerting teams on Slack, or routing outputs to different systems based on dynamic validation results:

def log_task_completion(output):
    print(f"Task completed! Raw output length: {len(output.raw)}")
    # Send webhook alert or write to local files

task = Task(
    description='Write a code audit report.',
    agent=writer,
    callback=log_task_completion
)

Production Container Deployment

To run your agent crews continuously or on trigger events, containerize the application using Docker and run it on headless servers:

# Dockerfile for running headless CrewAI agents
FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["python", "agent_crew.py"]

Step-by-Step Python Implementation

Let's write a complete Python script that orchestrates a two-agent crew: a **Senior Research Analyst** who searches the web, and a **Tech Content Writer** who writes a report. We will connect them to a local Ollama model.

Step 1: Install CrewAI

Install the core package alongside the tools library using pip:

pip install crewai crewai-tools langchain-community

Step 2: Create the Agent Script

Save the following script as agent_crew.py, ensuring you have Ollama running locally with the Llama-3 model:

from crewai import Agent, Task, Crew, Process
from langchain_community.llms import Ollama
from crewai_tools import SerperDevTool

# Initialize local model
local_llm = Ollama(model="llama3")

# Initialize search tool
search_tool = SerperDevTool()

# 1. Define the Researcher Agent
researcher = Agent(
    role='Senior Research Analyst',
    goal='Uncover cutting-edge developments in AI agents',
    backstory="""You are an expert researcher. You analyze web articles, code repos, 
    and research papers to isolate trends. You output highly structured summaries.""",
    verbose=True,
    allow_delegation=False,
    tools=[search_tool],
    llm=local_llm
)

# 2. Define the Writer Agent
writer = Agent(
    role='Tech Content Writer',
    goal='Draft clear, engaging technical blog posts',
    backstory="""You are a technical blogger. You translate complex research briefs 
    into developer guides. Your writing is concise and structured.""",
    verbose=True,
    allow_delegation=True,
    llm=local_llm
)

# 3. Define Tasks
task1 = Task(
    description='Research the top 3 open-source AI agent frameworks in 2026.',
    expected_output='A bulleted list containing framework names, GitHub stars, and key features.',
    agent=researcher
)

task2 = Task(
    description='Draft a developer blog post explaining these frameworks.',
    expected_output='A markdown-formatted article of at least 800 words.',
    agent=writer
)

# 4. Boot the Crew
crew = Crew(
    agents=[researcher, writer],
    tasks=[task1, task2],
    process=Process.sequential,
    verbose=True
)

result = crew.kickoff()
print("######################")
print(result)

When you run python agent_crew.py, the Researcher will trigger the search tool, fetch results, compile them, and pass the output directly to the Writer, who will synthesize the final markdown article entirely offline.

Conclusion and Next Steps

Multi-agent orchestration represents the future of advanced software engineering. By breaking down complex goals into specialized agents, tasks, and crews, CrewAI bypasses the limitations of single prompt systems. Start by building a simple sequential crew, test it with local Ollama weights, and then implement custom tool adapters to connect your agents to local databases and system scripts.

Frequently Asked Questions

What is CrewAI?+
CrewAI is a Python framework designed to orchestrate role-playing autonomous AI agents. By dividing a complex task into specialized roles (like researcher, writer, or QA inspector), it enables agents to collaborate, delegate tasks, and solve problems dynamically.
How does CrewAI compare to AutoGPT?+
AutoGPT is a single, general-purpose agent that tries to solve tasks by looping recursively. CrewAI is a multi-agent framework where specialized, narrow agents collaborate sequentially, yielding much higher task success rates and fewer looping errors.
Can I run CrewAI with local Ollama models?+
Yes. CrewAI integrates with LangChain's Ollama classes, allowing you to run your entire agent crew offline using local models like Llama-3 or Mistral on your own hardware.

Share this article

Enjoyed this article?

Get more insights on AI tools, remote work, and passive income delivered to your inbox every week.

Related Articles