CrewAI Basics
9 examples to get you started with CrewAI - 6 basic and 3 intermediate.
Search across all documentation pages
9 examples to get you started with CrewAI - 6 basic and 3 intermediate.
You will build a small Researcher → Writer pipeline, then tighten tasks, tools, process, and run controls.
Install with pip install crewai (add crewai[tools] when you need the tools package). Set a provider API key such as OPENAI_API_KEY (or configure another LLM - verify at build).
pip install 'crewai[tools]' for search and file tools used in later examples.Create a Researcher and a Writer with distinct goals.
from crewai import Agent
researcher = Agent(
role="Research Analyst",
goal="Collect accurate, citable notes on the topic",
backstory="You prefer primary sources and short bullet notes over essays.",
verbose=True,
)
writer = Agent(
role="Technical Writer",
goal="Turn research notes into a clear brief for engineers",
backstory="You write tight markdown with headings and no fluff.",
verbose=True,
)verbose=True prints step traces while you learn.Related: Defining Agent Roles, Goals, and Backstories - role recipe
Wire research into writing with context.
from crewai import Task
research_task = Task(
description=(
"Research {topic}. List 5 key facts with why each matters. "
"Note uncertainty explicitly."
),
expected_output="Markdown bullets: fact, why it matters, confidence.",
agent=researcher,
)
write_task = Task(
description=(
"Using the research notes, write a 3-paragraph engineering brief on {topic}. "
"Audience: senior developers. End with 3 open questions."
),
expected_output="Markdown brief with three paragraphs and three questions.",
agent=writer,
context=[research_task],
)expected_output is the acceptance test written in English.context=[research_task] injects the research output into the writer.{topic} is filled at kickoff, not hard-coded.Related: The CrewAI Mental Model: Roles, Tasks, and Crews - task contracts
Run the two-step pipeline.
from crewai import Crew, Process
crew = Crew(
agents=[researcher, writer],
tasks=[research_task, write_task],
process=Process.sequential,
verbose=True,
)
result = crew.kickoff(inputs={"topic": "offline evals for tool-using agents"})
print(result.raw)
print(result.tasks_output) # intermediate + final task outputstasks list.result.raw is the primary completed output string.tasks_output when the final brief looks thin - the research step may be the failure.Related: Sequential vs Hierarchical Crew Processes - process choice
Cap iterations so a stuck tool loop cannot run forever.
researcher = Agent(
role="Research Analyst",
goal="Collect accurate notes quickly",
backstory="You stop when you have five solid facts.",
max_iter=8,
max_retry_limit=2,
verbose=True,
)max_iter forces a best-effort answer after too many internal steps.Related: Debugging a Crew That Won't Converge on an Answer - thrash checklist
Give search only to the researcher.
import os
from crewai import Agent
from crewai_tools import SerperDevTool # requires SERPER_API_KEY
search = SerperDevTool()
researcher = Agent(
role="Research Analyst",
goal="Find current facts with web search",
backstory="You verify claims before writing them down.",
tools=[search],
verbose=True,
)
writer = Agent(
role="Technical Writer",
goal="Write from provided notes only",
backstory="You do not invent sources. You rewrite notes.",
tools=[], # intentional
verbose=True,
)Related: Building a Custom Tool for a CrewAI Agent - custom tools
Use the CrewOutput fields you actually need.
result = crew.kickoff(inputs={"topic": "agent memory design"})
print("--- final ---")
print(result.raw)
for task_out in result.tasks_output:
print(task_out.agent, "=>", (task_out.raw or "")[:200])
print("tokens:", result.token_usage)
# If you enabled structured output on a task, also check result.pydantic / json_dictoutput_file on tasks when humans must review artifacts.Related: CrewAI Best Practices - production habits
Let a manager assign and review when ownership should be dynamic.
from crewai import Crew, Process
crew = Crew(
agents=[researcher, writer],
tasks=[research_task, write_task],
process=Process.hierarchical,
manager_llm="gpt-4o", # or a configured LLM instance; required for hierarchical
verbose=True,
)
result = crew.kickoff(inputs={"topic": "RAG evaluation metrics"})
print(result.raw)manager_llm or a custom manager_agent.Related: Sequential vs Hierarchical Crew Processes - when to switch
Persist facts across tasks (and later runs, depending on storage).
from crewai import Crew, Process, Memory
crew = Crew(
agents=[researcher, writer],
tasks=[research_task, write_task],
process=Process.sequential,
memory=True, # or memory=Memory(...) for tuned scoring / storage
verbose=True,
)
crew.kickoff(inputs={"topic": "CrewAI knowledge sources"})memory=True uses unified memory defaults (embedder + storage - verify at build).context=[...] handoffs on critical artifacts.Related: CrewAI 1.14's Pluggable Memory, Knowledge, and RAG Backends - backends
Persist the deliverable and think in task IDs for debugging.
write_task = Task(
description="Write a 3-paragraph brief on {topic} from the research notes.",
expected_output="Markdown brief saved for review.",
agent=writer,
context=[research_task],
output_file="output/topic_brief.md",
)
crew = Crew(
agents=[researcher, writer],
tasks=[research_task, write_task],
process=Process.sequential,
verbose=True,
output_log_file="crew_run.json", # JSON if filename ends with .json
)
result = crew.kickoff(inputs={"topic": "agent observability"})
print("wrote", "output/topic_brief.md", "final chars", len(result.raw or ""))
# CLI later: crewai log-tasks-outputs / crewai replay -t <task_id> (verify at build)Related: Debugging a Crew That Won't Converge on an Answer - converge checklist
Stack versions: Pins from the category manifest (verify at build): OpenRouter (~315+ models, July 2026 pricing/fees); LangGraph 1.0+; CrewAI 1.14+; Microsoft Agent Framework 1.0; Vercel AI SDK 6; Pydantic AI (latest); LlamaIndex (latest); OpenAI Agents SDK (latest + MCP); MCP (Linux Foundation governance); A2A (HTTP+SSE+JSON-RPC 2.0); Solana
@solana/web3.js+@solana/spl-token.
Reviewed by Chris St. John·Last updated Jul 16, 2026