getting-started
指导使用 CrewAI CLI 脚手架创建项目,选择合适的抽象层并搭建完整的 Agent 架构
npx skills add crewaiinc/skills --skill getting-startedBefore / After 效果对比
1 组手动创建 CrewAI 项目文件和目录结构,需要理解复杂的配置关系,容易遗漏关键文件或配置错误,平均需要 45 分钟完成基础搭建
使用 CLI 一键生成标准项目结构,自动配置正确的抽象层和依赖关系,30 秒完成项目脚手架,可直接开始业务开发
getting-started
CrewAI Getting Started & Architecture
How to choose the right abstraction, scaffold a project, and wire everything together.
MANDATORY WORKFLOW — Read This First
NEVER manually create crewAI project files. Always scaffold with the CLI:
crewai create flow <project_name>
This is not optional. Even if you only need one crew, even if you know the file structure by heart — run the CLI first, then modify the generated files. Do NOT write main.py, crew.py, agents.yaml, tasks.yaml, or pyproject.toml by hand from scratch.
Why: The CLI sets up correct imports, directory structure, pyproject.toml config, and boilerplate that is easy to get subtly wrong when done manually. The reference material below teaches you how the pieces work so you can modify scaffolded code, not so you can replace the scaffolding step.
Workflow:
-
Run
crewai create flow <name>(use underscores, not hyphens) -
Edit the generated YAML and Python files to match your use case
-
Run
crewai installthencrewai run
1. Choosing the Right Abstraction
crewAI has four levels of abstraction. Pick the simplest one that fits your need:
Level When to Use Overhead Example
LLM.call()
Single prompt, no tools, structured extraction
Lowest
Parse an email into fields
Agent.kickoff()
One agent with tools and reasoning, no multi-agent coordination
Low
Research a topic with web search
Crew.kickoff()
Multiple agents collaborating on related tasks
Medium
Research + write + review pipeline
Flow wrapping crews/agents/LLM calls
Production app with state, routing, conditionals, error handling
Full
Multi-step workflow with branching logic
Decision Flowchart
Do you need tools or multi-step reasoning?
├── No → LLM.call()
└── Yes
└── Do you need multiple agents collaborating?
├── No → Agent.kickoff()
└── Yes
└── Do you need state management, routing, or multiple crews?
├── No → Crew (but still scaffold as a Flow for future-proofing)
└── Yes → Flow + Crew(s)
Rule of thumb: For any production application, always start with a Flow. You can embed LLM.call(), Agent.kickoff(), or Crew.kickoff() inside Flow steps. This gives you state management, error handling, and room to grow.
2. LLM.call() — Direct LLM Invocation
Use for simple, single-turn tasks where you don't need tools or agent reasoning.
from crewai import LLM
from pydantic import BaseModel
class EmailFields(BaseModel):
sender: str
subject: str
urgency: str
llm = LLM(model="openai/gpt-4o")
# Without response_format — returns a string
raw = llm.call(messages=[{"role": "user", "content": "Summarize this text..."}])
print(raw) # str
# With response_format — returns the Pydantic object directly
result = llm.call(
messages=[{"role": "user", "content": f"Extract fields from this email: {email_text}"}],
response_format=EmailFields
)
print(result.sender) # str — access Pydantic fields directly
print(result.urgency) # str
When NOT to use: If you need tools, multi-step reasoning, or retries — use an Agent instead.
3. Agent.kickoff() — Single Agent Execution
Use when you need one agent with tools and reasoning, but don't need multi-agent coordination.
from crewai import Agent
from crewai_tools import SerperDevTool
from pydantic import BaseModel
class ResearchFindings(BaseModel):
main_points: list[str]
key_technologies: list[str]
researcher = Agent(
role="AI Researcher",
goal="Research the latest AI developments",
backstory="Expert AI researcher with deep technical knowledge.",
llm="openai/gpt-4o", # Optional: defaults to OPENAI_MODEL_NAME env var or "gpt-4"
tools=[SerperDevTool()],
)
# Unstructured output
result = researcher.kickoff("What are the latest LLM developments?")
print(result.raw) # str
print(result.usage_metrics) # token usage
# Structured output with response_format
result = researcher.kickoff(
"Summarize latest AI developments",
response_format=ResearchFindings,
)
print(result.pydantic.main_points)
Note: Agent.kickoff() wraps results — access structured output via result.pydantic. This differs from LLM.call(), which returns the Pydantic object directly.
When NOT to use: If you need multiple agents passing context to each other — use a Crew.
4. CLI Scaffold Reference
As stated above: NEVER skip crewai create flow. This section documents what the CLI generates so you know what to modify — not so you can recreate it by hand.
crewai create flow my_project
Warning: Always use underscores in project names, not hyphens. crewai create flow my-project creates a directory that is not a valid Python identifier, causing ModuleNotFoundError on import. Use my_project instead.
This generates:
my_project/
├── src/my_project/
│ ├── crews/
│ │ └── my_crew/
│ │ ├── config/
│ │ │ ├── agents.yaml # Agent definitions (role, goal, backstory)
│ │ │ └── tasks.yaml # Task definitions (description, expected_output)
│ │ └── my_crew.py # Crew class with @CrewBase
│ ├── tools/
│ │ └── custom_tool.py
│ ├── main.py # Flow class with @start/@listen
│ └── ...
├── .env # API keys (OPENAI_API_KEY, etc.)
└── pyproject.toml
Do not use crewai create crew unless you are certain you will never need routing, state, or multiple crews. Prefer crewai create flow as the default.
5. YAML Configuration (agents.yaml & tasks.yaml)
The scaffold uses YAML files for agent and task definitions. This separates configuration from code and supports {variable} interpolation.
agents.yaml
researcher:
role: >
{topic} Senior Data Researcher
goal: >
Uncover cutting-edge developments in {topic}
backstory: >
You're a seasoned researcher with a knack for uncovering
the latest developments in {topic}.
# Optional overrides:
# llm: openai/gpt-4o
# max_iter: 20
# max_rpm: 10
reporting_analyst:
role: >
{topic} Reporting Analyst
goal: >
Create detailed reports based on {topic} research findings
backstory: >
You're a meticulous analyst known for turning complex data
into clear, actionable reports.
tasks.yaml
research_task:
description: >
Conduct thorough research about {topic}.
Identify key trends, breakthrough technologies,
and potential industry impacts.
expected_output: >
A detailed report with analysis of the top 5
developments in {topic}, with sources and implications.
agent: researcher
reporting_task:
description: >
Review the research and create a comprehensive report about {topic}.
expected_output: >
A polished report formatted in markdown with sections
for each key finding.
agent: reporting_analyst
output_file: output/report.md
Key rules:
-
{variable}placeholders are replaced at runtime viacrew.kickoff(inputs={...}) -
expected_outputis always a string (never a Pydantic class name) -
agentvalue must match an agent key inagents.yaml -
In
Process.sequential, each task auto-receives all prior task outputs as context -
For non-sequential deps, use
context=[other_task]to explicitly pass output
6. Wiring It Together — crew.py
The @CrewBase decorator auto-loads YAML config files and collects @agent and @task methods.
from crewai import Agent, Crew, Process, Task
from crewai.project import CrewBase, agent, crew, task
from crewai_tools import SerperDevTool
@CrewBase
class ResearchCrew:
"""Research and reporting crew."""
agents_config = "config/agents.yaml"
tasks_config = "config/tasks.yaml"
@agent
def researcher(self) -> Agent:
return Agent(
config=self.agents_config["researcher"],
tools=[SerperDevTool()],
)
@agent
def reporting_analyst(self) -> Agent:
return Agent(
config=self.agents_config["reporting_analyst"],
)
@task
def research_task(self) -> Task:
return Task(config=self.tasks_config["research_task"])
@task
def reporting_task(self) -> Task:
return Task(
config=self.tasks_config["reporting_task"],
context=[self.research_task()], # Explicit dependency (optional in sequential)
output_file="output/report.md",
)
@crew
def crew(self) -> Crew:
return Crew(
agents=self.agents, # auto-collected by @agent
tasks=self.tasks, # auto-collected by @task
process=Process.sequential,
verbose=True,
)
Important: Method names must match YAML keys. def researcher(self) maps to the researcher: key in agents.yaml.
7. Flows — The Production Foundation
Flows are the recommended way to build production crewAI applications. They provide state management, conditional routing, human-in-the-loop, and persistence — wrapping crews, agents, and LLM calls into a coherent workflow.
Basic Flow — main.py
from crewai.flow.flow import Flow, listen, start
from pydantic import BaseModel
from .crews.research_crew.research_crew import ResearchCrew
class ResearchState(BaseModel):
topic: str = ""
report: str = ""
class ResearchFlow(Flow[ResearchState]):
@start()
def begin(self):
print(f"Starting research on: {self.state.topic}")
@listen(begin)
def run_research(self):
result = ResearchCrew().crew().kickoff(
inputs={"topic": self.state.topic}
)
self.state.report = result.raw
def kickoff():
flow = ResearchFlow()
flow.kickoff(inputs={"topic": "AI Agents"})
if __name__ == "__main__":
kickoff()
Key points:
-
flow.kickoff(inputs={"topic": "AI Agents"})populatesself.state.topic(keys must match Pydantic field names). The YAML{variable}substitution happens later, when you callcrew.kickoff(inputs={"topic": self.state.topic})inside a Flow step. The chain is: flow inputs → state → crew inputs → YAML substitution. -
Each
@listenmethod runs after its dependency completes -
State persists across all Flow steps — use it to pass data between crews
State Management — Structured vs Unstructured
Structured (recommended for production):
from pydantic import BaseModel
class MyState(BaseModel):
topic: str = ""
research: str = ""
draft: str = ""
approved: bool = False
class MyFlow(Flow[MyState]):
...
Unstructured (quick prototyping):
class MyFlow(Flow): # No type parameter — state is a dict
@start()
def begin(self):
self.state["topic"] = "AI" # dict-style access
Use structured state for type safety, IDE autocompletion, and validation. Use unstructured only for throwaway prototypes.
Using Agent.kickoff() Inside Flows (Common Pattern)
Many production Flows skip Crews entirely and orchestrate individual agents via Agent.kickoff(). This gives you fine-grained control — each Flow step calls a specific agent, passes state, and stores the result. The Flow handles orchestration; agents handle reasoning.
from crewai import Agent, LLM
from crewai.flow.flow import Flow, listen, start
from crewai_tools import SerperDevTool, ScrapeWebsiteTool
from pydantic import BaseModel
class ResearchState(BaseModel):
query: str = ""
raw_research: str = ""
analysis: str = ""
report: str = ""
class DeepResearchFlow(Flow[ResearchState]):
@start()
def gather_research(self):
"""Agent with tools does the actual searching."""
researcher = Agent(
role="Senior Research Analyst",
goal="Find comprehensive, factual information about the given topic",
backstory="You're an expert researcher who always cites sources and flags uncertainty.",
tools=[SerperDevTool(), ScrapeWebsiteTool()],
llm="openai/gpt-4o",
)
result = researcher.kickoff(
f"Research this topic thoroughly: {self.state.query}"
)
self.state.raw_research = result.raw
@listen(gather_research)
def analyze_findings(self):
"""A different agent analyzes the raw research — no tools needed."""
analyst = Agent(
role="Data Analyst",
goal="Extract key insights, patterns, and actionable recommendations",
backstory="You turn raw data into clear, structured analysis.",
llm="openai/gpt-4o",
)
result = analyst.kickoff(
f"Analyze these research findings and extract key insights:\n\n{self.state.raw_research}"
)
self.state.analysis = result.raw
@listen(analyze_findings)
def write_report(self):
"""A writer agent produces the final deliverable."""
writer = Agent(
role="Technical Writer",
goal="Produce clear, actionable reports for non-technical readers",
backstory="You specialize in making complex information accessible.",
llm="openai/gpt-4o",
)
result = writer.kickoff(
f"Write a comprehensive report based on this analysis:\n\n{self.state.analysis}"
)
self.state.report = result.raw
Why this pattern works well:
-
Each agent is purpose-built for its step — narrow role, specific tools
-
The Flow manages state and sequencing — no crew overhead
-
Easy to add routing, human review, or retry logic between steps
-
You can mix
Agent.kickoff(),LLM.call(), andCrew.kickoff()freely
When to use Agent.kickoff() vs Crew.kickoff() in a Flow:
Use Agent.kickoff() when
Use Crew.kickoff() when
Each step is a distinct agent with different tools Multiple agents need to collaborate on ONE task
You want the Flow to control sequencing Agents need to pass context to each other within a step
Steps are independent and don't need inter-agent delegation You need hierarchical process with a manager
You want maximum control over what data flows between steps The sub-workflow is self-contained and reusable
Agent.kickoff() with Structured Output in Flows
Combine response_format with state for typed data flow between agents:
class Insights(BaseModel):
key_points: list[str]
recommendations: list[str]
confidence: float
class AnalysisFlow(Flow[AnalysisState]):
@start()
def research(self):
researcher = Agent(role="Researcher", goal="...", backstory="...", tools=[SerperDevTool()])
result = researcher.kickoff(
f"Research {self.state.topic}",
response_format=Insights,
)
# result.pydantic gives you the typed Insights object
self.state.key_points = result.pydantic.key_points
self.state.recommendations = result.pydantic.recommen
...
用户评价 (0)
发表评价
暂无评价
统计数据
用户评分
为此 Skill 评分