← Back to Tutorials

CrewAI Framework

Multi-Agent Collaboration

CrewAI enables role-based agent teams. Each agent has a role, goal, and backstory — forming a crew that works together on tasks.

pip install crewai crewai-tools

Creating a Crew

from crewai import Agent, Task, Crew

researcher = Agent(
    role="Senior Researcher",
    goal="Find accurate information",
    backstory="Expert researcher with 20 years experience",
    tools=[],
    verbose=True
)

writer = Agent(
    role="Content Writer",
    goal="Write compelling content based on research",
    backstory="Award-winning technology writer",
)

research_task = Task(
    description="Research the latest AI agent frameworks",
    agent=researcher,
    expected_output="A detailed report on AI agent frameworks"
)

write_task = Task(
    description="Write a blog post from the research",
    agent=writer,
    expected_output="A 500-word blog post"
)

crew = Crew(
    agents=[researcher, writer],
    tasks=[research_task, write_task],
    verbose=True
)

result = crew.kickoff()

Financial Research Systems

Build multi-agent financial research teams that gather data, analyze trends, and generate reports.

Custom Tools & Structured Outputs

from crewai_tools import tool
from pydantic import BaseModel

class StockAnalysis(BaseModel):
    ticker: str
    current_price: float
    recommendation: str

@tool("Stock Price Tool")
def get_stock_price(ticker: str) -> float:
    """Get the current stock price for a ticker."""
    import yfinance as yf
    stock = yf.Ticker(ticker)
    return stock.info.get("currentPrice", 0)

Memory & Coding Agents

CrewAI supports short-term, long-term, and entity memory. Use SQL or vector storage for persistent memory across runs.

Advanced Trading Applications

Combine multiple agents (data collector, analyst, risk assessor, trader) into a collaborative trading framework.

✏️ Exercise: Build a financial research crew with three agents: a Data Collector (fetches stock prices and news), an Analyst (evaluates performance and trends), and a Report Writer (generates structured Markdown reports). Use Pydantic for structured outputs. Run the crew for 3 different tickers.