CrewAI: How to Build an Agent (Python Quickstart)
Updated 2026-11-17
CrewAI models a workflow as a crew of agents running tasks in sequence (or with a manager). This guide uses the direct Python API from CrewAI’s docs: two agents, two tasks, and crew.kickoff(inputs={...}). No YAML or Flow scaffolding required. When tools misbehave, use Fix: CrewAI tool not found. Compare with LangGraph how to build an agent and the raw loop in Build your first AI agent in Python. Last tested 2026-11-17.
Docs used:
Prerequisites
- Python 3.10+
- An LLM API key (CrewAI defaults to the provider configured in your environment; see CrewAI’s LLM setup)
Steps
1. Install CrewAI
pip install crewai
For tools from the crewai_tools package later, install pip install 'crewai[tools]' as in the fix guide.
2. Define agents
Each agent needs a role, goal, and backstory. Keep them short so the model can stay on task.
# research_crew.py
from crewai import Agent
researcher = Agent(
role="Senior Researcher",
goal="Gather accurate facts about {topic}",
backstory="You write concise bullet briefings and avoid speculation.",
verbose=True,
)
writer = Agent(
role="Content Writer",
goal="Turn research into a readable memo about {topic}",
backstory="You edit for clarity and keep memos under 200 words.",
verbose=True,
)
3. Define tasks and wire context
Tasks declare description, expected_output, and agent. Later tasks can use context from earlier ones.
from crewai import Task
research_task = Task(
description="Research {topic}. List three verifiable facts.",
expected_output="Three bullet points about {topic}.",
agent=researcher,
)
memo_task = Task(
description="Write a short memo using the research bullets.",
expected_output="A markdown memo with a title and three short paragraphs.",
agent=writer,
context=[research_task],
)
4. Create the crew and kick off
from crewai import Crew, Process
crew = Crew(
agents=[researcher, writer],
tasks=[research_task, memo_task],
process=Process.sequential,
verbose=True,
)
if __name__ == "__main__":
result = crew.kickoff(inputs={"topic": "Model Context Protocol"})
print(result.raw)
kickoff returns a CrewOutput object; result.raw is the final task’s string output per Crew output.
5. Add tools (optional)
Attach tools on the agent that runs the tool-heavy task. Tools are per agent—see the fix guide if the model calls a name you never registered.
from crewai.tools import tool
@tool("lookup_policy")
def lookup_policy() -> str:
"""Return the company refund policy text."""
return "Refunds within 30 days of delivery."
researcher = Agent(
role="Senior Researcher",
goal="Gather accurate facts about {topic}",
backstory="You cite policies when relevant.",
tools=[lookup_policy],
verbose=True,
)
Official tool patterns: CrewAI Tools.
6. Run from the CLI
export OPENAI_API_KEY="sk-..." # or your provider per CrewAI LLM docs
python research_crew.py
Common errors
ValidationError on Agent fields: Every agent needs role, goal, and backstory strings. Empty backstory often fails validation.
Tool called but not found: The tool list on Agent does not match the name the model used. See Fix: CrewAI tool not found.
Rate limits / slow runs: Set max_rpm on the crew or agent per Crew attributes.
Hang or very long output: Lower verbosity, tighten expected_output, or reduce task count. Compare loop limits with Fix: AI agent stuck in a loop.
FAQ
Should I use JSONC, Flow, or this Python style?
New CrewAI projects often scaffold JSONC or Flows (crewai create flow). This guide uses direct Python because it maps one-to-one to the CrewAI docs’ alternative definition and is easy to paste into an existing repo.
Does CrewAI replace my LLM tool loop?
CrewAI orchestrates multi-step roles and tasks. You can still embed a single-agent tool loop from Build your first AI agent in Python inside a custom tool if you need fine-grained control.
How do I deploy a crew?
CrewAI documents AMP deployment from a project with crewai login and crewai deploy create. Local scripts like this one run anywhere Python and your LLM credentials run—see How to deploy an AI agent with Docker.