Crews and Processes

A Crew is a set of agents, a set of tasks, and a Process (execution strategy). Start it with crew.kickoff(inputs=...); use kickoff_async() for async.

In production, a Crew usually lives inside one Flow @listen step, not as the whole program. See Introduction and Flows.


Create and run

from crewai import Agent, Task, Crew, Process

crew = Crew(
    agents=[researcher, writer],
    tasks=[research_task, writing_task],
    process=Process.sequential,
    verbose=True,
    memory=True,       # unified memory — see the Memory chapter
    planning=False,    # True: plan tasks before running
)

output = crew.kickoff(inputs={"topic": "CrewAI Flows"})
print(output.raw)
print(output.tasks_output)
print(output.token_usage)

CrewOutput: raw, pydantic, json_dict, tasks_output, token_usage. For JSON, set output_pydantic / output_json on the last task.

Other frequent options: cache (tool-result cache, default True), max_rpm, embedder (for memory; OpenAI by default), manager_llm / manager_agent, output_log_file, stream, checkpoint.


Process.sequential

Process is an enum: sequential and hierarchical.

Sequential runs the tasks list in order; earlier output becomes later context. This is the default and the right starting point.

crew = Crew(
    agents=my_agents,
    tasks=my_tasks,
    process=Process.sequential,
)

Use context=[other_task] on a Task when you need an explicit dependency, rather than assuming list order is the only data flow.


Process.hierarchical

Hierarchical mimics a manager assigning work. Tasks need not be pre-bound to agents. You must set manager_llm or manager_agent. The manager plans, delegates, and reviews.

crew = Crew(
    agents=my_agents,
    tasks=my_tasks,
    process=Process.hierarchical,
    manager_llm="openai/gpt-4o",
    # or manager_agent=my_manager_agent
)

JSONC: "process": "hierarchical" plus manager_llm or manager_agent (the manager file may sit outside the top-level agents array).

Hierarchical crews cost more tokens and are harder to debug. Stabilize sequential first; enable hierarchy when assignment must be dynamic.


JSONC vs classic scaffold

Default crewai create crew is JSON-first: crew.jsonc + agents/*.jsonc, loaded by crewai run.

{
  "name": "Market Research Crew",
  "agents": ["researcher", "analyst"],
  "process": "sequential",
  "verbose": true,
  "memory": true,
  "tasks": [],
  "inputs": { "topic": "AI Agents" }
}

crewai create crew <name> --classic uses crew.py and YAML plus @CrewBase, @agent, @task, @crew (classic path only — not Flow decorators). JSON "custom:" tools and {"python": "module.attribute"} execute local Python; only run projects you trust.

Load a JSON crew:

from pathlib import Path
from crewai.project import load_crew

crew, default_inputs = load_crew(Path("crew.jsonc"))
result = crew.kickoff(inputs={**default_inputs, "topic": "AI Agents"})

Checkpoints and logs

checkpoint=True (or CheckpointConfig) saves state after key events so interrupted runs can resume. output_log_file=True writes logs.txt; a .json path writes JSON logs.


Next steps

评论