Hands-On with CrewAI: Building Multi-Agent Systems for Real-World ML Applications
Target audience: Developers & Architects
Purpose: A practical walkthrough for creating an end-user–facing multi-agent system using the CrewAI framework.
1. Why CrewAI for Multi-Agent Systems?
In the AI development landscape, multi-agent systems are emerging as the next step after single-LLM pipelines. Instead of one monolithic AI model handling every task, we have multiple specialised agents collaborating, each with its own skills, tools, and goals.
CrewAI is a Python framework that simplifies:
Defining agents (roles, goals, tools, personalities)
Creating task workflows (parallel or sequential execution)
Managing interactions (message passing, state management)
Integrating LLMs & external APIs
Think of it as a film crew for AI development — each agent is a "specialist" actor with a role, and CrewAI is the director ensuring they work together.
2. Architecture Overview
High-level flow for an end-user application:
[User Input]
↓
[Orchestrator Agent] — parses request, assigns tasks
↓
[Specialized Agents]
├── Data Retrieval Agent
├── Data Analysis Agent
├── Content Generation Agent
├── Validation Agent
↓
[Response Aggregator]
↓
[End-User Output]
You can visualize this as:
Orchestrator = PM assigning work
Specialized Agents = team members with expertise
Aggregator = editor polishing and merging outputs
3. Setting Up CrewAI
Install CrewAI and required dependencies:
pip install crewai openai langchain
(Replace openai with your chosen LLM provider if needed.)
Set your LLM API key in the environment:
export OPENAI_API_KEY="your_api_key_here"4. Defining Your First Agent
CrewAI agents are Python classes (or objects) with:
Role — defines the expertise & responsibility
Goal — defines what the agent is trying to achieve
Backstory — optional narrative to shape LLM responses
Tools — functions the agent can call
Example: Data Retrieval Agent
from crewai import Agent
from crewai_tools import tool
from langchain_community.tools import DuckDuckGoSearchRun
search_tool = DuckDuckGoSearchRun()
data_retrieval_agent = Agent(
role="Data Retrieval Specialist",
goal="Fetch the most relevant, up-to-date information for the user query",
backstory="An expert at searching online sources, news articles, and reports",
tools=[search_tool]
)
5. Defining a Task
A Task is an action given to an agent, with clear instructions and an expected output format.
from crewai import Task
task_fetch_info = Task(
description="Find the latest market trends in AI for 2025",
agent=data_retrieval_agent,
expected_output="A bullet-point list of 5 latest AI trends for 2025"
)
6. Creating a Multi-Agent Crew
Multiple agents can be grouped into a Crew for collaborative execution.
from crewai import Crew
crew = Crew(
agents=[data_retrieval_agent],
tasks=[task_fetch_info],
verbose=True
)
7. Running the Workflow
result = crew.run()
print(result)
This executes:
Task → Agent → Tool → Result
CrewAI handles the message passing and context between agents if you have more than one.
8. Example: End-to-End Multi-Agent Solution
Imagine building a "Market Insights Generator" that:
Retrieves the latest AI news
Summarizes findings
Generates a readable end-user report
Code:
# Step 1: Agents
summary_agent = Agent(
role="Summarization Specialist",
goal="Condense retrieved information into a concise report",
backstory="Expert in summarizing complex information for business leaders"
)
report_agent = Agent(
role="Report Writer",
goal="Create a well-structured, end-user–friendly report",
backstory="Writes clear and compelling business reports"
)
# Step 2: Tasks
task_summarize = Task(
description="Summarize the information from Data Retrieval Agent",
agent=summary_agent,
expected_output="A concise 3-paragraph summary"
)
task_write_report = Task(
description="Turn the summary into a professional report",
agent=report_agent,
expected_output="A Markdown-formatted business report"
)
# Step 3: Crew
crew = Crew(
agents=[data_retrieval_agent, summary_agent, report_agent],
tasks=[task_fetch_info, task_summarize, task_write_report],
verbose=True
)
# Step 4: Run
final_report = crew.run()
print(final_report)
9. Scaling for Production
For real-world deployment:
LLM Selection: Use cost-efficient models for simple tasks, high-quality models for final output
Caching: Avoid redundant calls with results caching (e.g., LangChain’s
InMemoryCache)Parallelisation: Run independent tasks in parallel for faster responses
State Management: Store agent states in Redis or a vector database
Observability: Integrate logging & monitoring (e.g., LangSmith, OpenTelemetry)
10. Key Takeaways
CrewAI abstracts the complexity of agent orchestration
Great for developer productivity—minimal boilerplate
Works well with LangChain tools & custom Python functions
Scales from prototyping to production-grade ML systems
11. Next Steps
Build a customer support AI with specialised agents (FAQ retrieval, sentiment analysis, escalation)
Integrate with vector search for long-term memory
Deploy via FastAPI + Docker for production serving


