M11-A — CrewAI How It Works

How to read this note

CrewAI is a Python tool that lets you build a team of AI agents without writing the orchestration loop yourself. Think: you hire a crew; CrewAI is the office manager.

Website: crewai.com


🏢 CrewAI vocabulary (4 words only)

Word Meaning Real-world match
Agent One AI worker with a job title + backstory "Research Analyst"
Task One specific assignment with expected output "Find 5 facts about RAG"
Crew The whole team + how they work together Your project group
Process Order agents run: sequential or hierarchical Assembly line vs boss → interns
CREW
 ├── Agent 1 (Researcher)
 ├── Agent 2 (Writer)
 ├── Agent 3 (Editor)
 ├── Task A → assigned to Agent 1
 ├── Task B → assigned to Agent 2  (uses Task A output)
 └── Task C → assigned to Agent 3

🔄 What happens when you run a Crew (backend tour)

You run one command (or click Run in a notebook). Behind the screen:

STEP 1  CrewAI reads your Python config
        (agents, tasks, goals, LLM API key)

STEP 2  For each TASK in order:
          a. CrewAI builds a big prompt:
             - agent role + backstory
             - task description
             - output from previous tasks (memory)
          b. Sends prompt to LLM API (OpenAI / Gemini / etc.)
          c. LLM returns text (or asks to use a tool)
          d. If tool needed → CrewAI runs tool → feeds result back
          e. Task marked "done" → output stored

STEP 3  Final task output = CREW RESULT shown to you
┌─────────────┐     HTTPS      ┌──────────────┐
│  Your PC    │ ──────────────▶│  LLM API     │
│  CrewAI     │ ◀──────────────│  (cloud)     │
│  script     │   text/tools   └──────────────┘
└──────┬──────┘
       │ optional
       ▼
   Local tools (search, files, calculator)
The real insight

You write job descriptions. CrewAI handles the boring loop: who speaks next, what context they get, where to save output.


📝 Minimal example (concept — not full install)

# Simplified idea — real code has more imports

researcher = Agent(
    role="Research Analyst",
    goal="Find key facts about RAG from the brief",
    backstory="You explain AI simply for school students."
)

writer = Agent(
    role="Content Writer",
    goal="Turn research into a 1-page study note",
    backstory="You write clear Obsidian-style markdown."
)

task1 = Task(
    description="List 5 RAG facts a beginner must know",
    agent=researcher
)

task2 = Task(
    description="Write study note using research output",
    agent=writer
)

crew = Crew(
    agents=[researcher, writer],
    tasks=[task1, task2],
    process=Process.sequential  # task1 before task2
)

result = crew.kickoff()

When kickoff() runs → backend loop from diagram above starts.


🔀 Sequential vs Hierarchical process

Sequential Hierarchical
Task 1 → Task 2 → Task 3 Manager agent assigns/reviews subtasks
Like passing paper left in class Like team lead delegating
Easier for beginners Better for big projects

Start sequential for your first class project.


🧰 Tools in CrewAI

Agents can use tools — same idea as Module 9:

Tool example Agent uses it to
Web search Get fresh facts
File read Read your PDF
Custom function Call your own Python code
LLM: "I need to search the web for X"
      ↓
CrewAI intercepts → runs search tool → returns results
      ↓
LLM continues with real data

🎓 Class project ideas (from faculty checklist)

Project Crew design
Daily quote + tip 1 agent, 1 task, scheduled run
Email summariser Researcher reads → Summariser writes
Quiz from notes Reader agent → Question writer → Fact-checker

Faculty practical #8: Small CrewAI automation — daily quote or email summary.


⚙️ What you need installed (overview)

1. Python on your PC
2. pip install crewai (and related packages)
3. API key from OpenAI or Gemini in .env file
4. Python file defining agents + tasks
5. Run: crew.kickoff()

Errors usually mean: wrong API key, no internet, or task description too vague.

API keys cost money

Set usage limits in your Google/OpenAI dashboard. Test with short tasks first.


🆚 CrewAI vs doing it manually

Manual (Module 4–5) CrewAI
You paste between ChatGPT tabs Code connects agents
Good for learning Good for repeat runs
Free tier chat only API billing per run
No programming Basic Python required

✅ Before you code — paper design

  1. Draw 2–3 agents with names and one-sentence jobs
  2. List tasks in order
  3. Write expected output per task
  4. Mark human review step
  5. Only then open VS Code / Cursor

Next: M11-B — AutoGen Introduction
Foundation: M10-B — Task Distribution and Orchestration