Assembling Agents into a Crew
A crew in CrewAI is a collaborative group of agents working together to complete one or more tasks following a defined process. As an Agentic Architect, you design the crew so that roles, tasks, and execution flow match a real business workflow.
1. What a Crew Is
- A crew bundles three things: a list of agents, a list of tasks, and a process that defines how those tasks run (e.g., sequential or hierarchical).
- The crew is the unit you kick off to run an entire workflow end‑to‑end, and its final output is the output of the last task.
- Crews can also share memory, tools, and logging, so you manage behavior at the team level, not just at the single‑agent level.
2. Core Crew Attributes
When you instantiate a Crew, you combine agents and tasks plus several optional controls.
Key attributes:
tasks: List of Task objects the crew will execute.agents: List of agents that belong to this crew.process(optional): Execution strategy, usually Process.sequential or Process.hierarchical (default is sequential).verbose(optional): Enables detailed logs during execution.manager_llm / manager_agent(optional): Required for hierarchical crews, used by the manager to delegate and validate tasks.max_rpm(optional): Rate limit for the whole crew.memory(optional): Shared memory (short‑term, long‑term, entity) across tasks.cache(optional): Whether to cache tool results for efficiency (default True).embedder(optional): Shared embedder config, mainly for memory.step_callback / task_callback(optional): Hooks called after each agent step or after each task; useful for logging, analytics, or side‑effects.knowledge_sources(optional): Shared knowledge sources accessible to all agents in the crew.stream(optional): Enable streaming output for real‑time updates during execution.output_log_file(optional): Save logs as .txt or .json for later inspection.planning / planning_llm(optional): Enable an AgentPlanner to create a plan across tasks before each iteration.
These parameters let you move from a simple demo crew to a production‑ready team with observability and controls.
3. Assembling a Crew with CrewBase and Decorators (YAML‑First)
The recommended pattern is to keep agents and tasks in YAML, then assemble them in a Python class that inherits from CrewBase.
3.1 Crew class structure
Then you run:
Key ideas:
@CrewBaseturns the class into a crew container with automatic wiring.@agentmethods define agents and are automatically collected into self.agents.@taskmethods define tasks and are automatically collected into self.tasks.@crewreturns the final Crew object using those agents and tasks.@before_kickoffand@after_kickoffprovide hooks to pre‑process inputs and post‑process outputs without changing task logic.
4. Assembling a Crew Directly in Code (Alternative)
You can also define everything imperatively in plain Python.
Use this approach when
- You want maximum control in code (dynamic agents/tasks).
- You’re rapidly prototyping before extracting things to YAML.
5. Running, Observing, and Replaying a Crew
Once agents and tasks are assembled into a crew, you manage execution and inspection via the CrewOutput, logs, metrics, and replay tools.
5.1 Kicking off a crew
kickoff(): Run once with a single inputs dict.kickoff_for_each(): Run the same crew for a list of inputs (e.g., multiple topics).- Async variants:
akickoff, akickoff_for_each, kickoff_async, kickoff_for_each_asyncfor high‑concurrency workloads.
Example:
5.2 Reading CrewOutput
CrewOutput gives you several views of the result:
raw(str): Final output text (from the last task).json_dict(dict, optional): JSON representation if structured output is enabled.pydantic(BaseModel, optional): Pydantic object when using Pydantic output.tasks_output(list): Individual TaskOutput objects for each task in the crew.token_usage(dict): Token‑usage stats for the whole run.
You can also call json() or to_dict() for serialization.
5.3 Logs, metrics and streaming
- output_log_file=True or a filename saves crew logs to logs.txt or a custom .txt / .json file.
- usage_metrics attribute on the crew shows aggregated LLM usage across tasks.
- stream=True enables streaming; you iterate over chunks and then access streaming.result for the final CrewOutput.
5.4 Replay from a specific task
-
CLI commands:
crewai log-tasks-outputs– list task IDs from the latest run.crewai replay -t <task_id>– replay from that task while preserving previous context
Replay is extremely valuable in a course setting to show debugging and iterative refinement of a single step in a complex workflow.
6. Design Checklist: Assembling a Good Crew
Before you consider a crew “ready,” verify:
- Agents: Each important skill is covered by a distinct agent role/goal/backstory.
- Tasks: Every major step in the workflow is captured as a task with clear
descriptionandexpected_output. - Process:
Process.sequentialvsProcess.hierarchicalmatches how decisions should flow. (see Lesson 6) - Hooks:
before_kickoff/after_kickoffare used if you need input enrichment or output post‑processing. - Observability:
verbose, output_log_file, usage_metrics, and optionally stream are configured for debugging and teaching.
This is the mental model you want your learners to internalize: agents are specialists, tasks are assignments, and the crew is the team plus its playbook for execution.