You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/en/concepts/crews.mdx
+32-6Lines changed: 32 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -307,12 +307,27 @@ print(result)
307
307
308
308
### Different Ways to Kick Off a Crew
309
309
310
-
Once your crew is assembled, initiate the workflow with the appropriate kickoff method. CrewAI provides several methods for better control over the kickoff process: `kickoff()`, `kickoff_for_each()`, `kickoff_async()`, and `kickoff_for_each_async()`.
310
+
Once your crew is assembled, initiate the workflow with the appropriate kickoff method. CrewAI provides several methods for better control over the kickoff process.
311
+
312
+
#### Synchronous Methods
311
313
312
314
-`kickoff()`: Starts the execution process according to the defined process flow.
313
315
-`kickoff_for_each()`: Executes tasks sequentially for each provided input event or item in the collection.
314
-
-`kickoff_async()`: Initiates the workflow asynchronously.
315
-
-`kickoff_for_each_async()`: Executes tasks concurrently for each provided input event or item, leveraging asynchronous processing.
316
+
317
+
#### Asynchronous Methods
318
+
319
+
CrewAI offers two approaches for async execution:
320
+
321
+
| Method | Type | Description |
322
+
|--------|------|-------------|
323
+
|`akickoff()`| Native async | True async/await throughout the entire execution chain |
324
+
|`akickoff_for_each()`| Native async | Native async execution for each input in a list |
325
+
|`kickoff_async()`| Thread-based | Wraps synchronous execution in `asyncio.to_thread`|
326
+
|`kickoff_for_each_async()`| Thread-based | Thread-based async for each input in a list |
327
+
328
+
<Note>
329
+
For high-concurrency workloads, `akickoff()` and `akickoff_for_each()` are recommended as they use native async for task execution, memory operations, and knowledge retrieval.
These methods provide flexibility in how you manage and execute tasks within your crew, allowing for both synchronous and asynchronous workflows tailored to your needs.
366
+
These methods provide flexibility in how you manage and execute tasks within your crew, allowing for both synchronous and asynchronous workflows tailored to your needs. For detailed async examples, see the [Kickoff Crew Asynchronously](/en/learn/kickoff-async) guide.
CrewAI provides the ability to kickoff a crew asynchronously, allowing you to start the crew execution in a non-blocking manner.
10
+
CrewAI provides the ability to kickoff a crew asynchronously, allowing you to start the crew execution in a non-blocking manner.
11
11
This feature is particularly useful when you want to run multiple crews concurrently or when you need to perform other tasks while the crew is executing.
12
12
13
-
## Asynchronous Crew Execution
13
+
CrewAI offers two approaches for async execution:
14
14
15
-
To kickoff a crew asynchronously, use the `kickoff_async()` method. This method initiates the crew execution in a separate thread, allowing the main thread to continue executing other tasks.
15
+
| Method | Type | Description |
16
+
|--------|------|-------------|
17
+
|`akickoff()`| Native async | True async/await throughout the entire execution chain |
18
+
|`kickoff_async()`| Thread-based | Wraps synchronous execution in `asyncio.to_thread`|
19
+
20
+
<Note>
21
+
For high-concurrency workloads, `akickoff()` is recommended as it uses native async for task execution, memory operations, and knowledge retrieval.
22
+
</Note>
23
+
24
+
## Native Async Execution with `akickoff()`
25
+
26
+
The `akickoff()` method provides true native async execution, using async/await throughout the entire execution chain including task execution, memory operations, and knowledge queries.
-`CrewOutput`: An object representing the result of the crew execution.
30
41
31
-
## Potential Use Cases
42
+
### Example: Native Async Crew Execution
43
+
44
+
```python Code
45
+
import asyncio
46
+
from crewai import Crew, Agent, Task
47
+
48
+
# Create an agent
49
+
coding_agent = Agent(
50
+
role="Python Data Analyst",
51
+
goal="Analyze data and provide insights using Python",
52
+
backstory="You are an experienced data analyst with strong Python skills.",
53
+
allow_code_execution=True
54
+
)
55
+
56
+
# Create a task
57
+
data_analysis_task = Task(
58
+
description="Analyze the given dataset and calculate the average age of participants. Ages: {ages}",
59
+
agent=coding_agent,
60
+
expected_output="The average age of the participants."
61
+
)
62
+
63
+
# Create a crew
64
+
analysis_crew = Crew(
65
+
agents=[coding_agent],
66
+
tasks=[data_analysis_task]
67
+
)
68
+
69
+
# Native async execution
70
+
asyncdefmain():
71
+
result =await analysis_crew.akickoff(inputs={"ages": [25, 30, 35, 40, 45]})
72
+
print("Crew Result:", result)
73
+
74
+
asyncio.run(main())
75
+
```
76
+
77
+
### Example: Multiple Native Async Crews
78
+
79
+
Run multiple crews concurrently using `asyncio.gather()` with native async:
32
80
33
-
-**Parallel Content Generation**: Kickoff multiple independent crews asynchronously, each responsible for generating content on different topics. For example, one crew might research and draft an article on AI trends, while another crew generates social media posts about a new product launch. Each crew operates independently, allowing content production to scale efficiently.
81
+
```python Code
82
+
import asyncio
83
+
from crewai import Crew, Agent, Task
84
+
85
+
coding_agent = Agent(
86
+
role="Python Data Analyst",
87
+
goal="Analyze data and provide insights using Python",
88
+
backstory="You are an experienced data analyst with strong Python skills.",
89
+
allow_code_execution=True
90
+
)
91
+
92
+
task_1 = Task(
93
+
description="Analyze the first dataset and calculate the average age. Ages: {ages}",
94
+
agent=coding_agent,
95
+
expected_output="The average age of the participants."
96
+
)
97
+
98
+
task_2 = Task(
99
+
description="Analyze the second dataset and calculate the average age. Ages: {ages}",
100
+
agent=coding_agent,
101
+
expected_output="The average age of the participants."
-**Concurrent Market Research Tasks**: Launch multiple crews asynchronously to conduct market research in parallel. One crew might analyze industry trends, while another examines competitor strategies, and yet another evaluates consumer sentiment. Each crew independently completes its task, enabling faster and more comprehensive insights.
113
+
for i, result inenumerate(results, 1):
114
+
print(f"Crew {i} Result:", result)
36
115
37
-
-**Independent Travel Planning Modules**: Execute separate crews to independently plan different aspects of a trip. One crew might handle flight options, another handles accommodation, and a third plans activities. Each crew works asynchronously, allowing various components of the trip to be planned simultaneously and independently for faster results.
116
+
asyncio.run(main())
117
+
```
38
118
39
-
## Example: Single Asynchronous Crew Execution
119
+
###Example: Native Async for Multiple Inputs
40
120
41
-
Here's an example of how to kickoff a crew asynchronously using asyncio and awaiting the result:
121
+
Use `akickoff_for_each()` to execute your crew against multiple inputs concurrently with native async:
122
+
123
+
```python Code
124
+
import asyncio
125
+
from crewai import Crew, Agent, Task
126
+
127
+
coding_agent = Agent(
128
+
role="Python Data Analyst",
129
+
goal="Analyze data and provide insights using Python",
130
+
backstory="You are an experienced data analyst with strong Python skills.",
131
+
allow_code_execution=True
132
+
)
133
+
134
+
data_analysis_task = Task(
135
+
description="Analyze the dataset and calculate the average age. Ages: {ages}",
136
+
agent=coding_agent,
137
+
expected_output="The average age of the participants."
The `kickoff_async()` method provides async execution by wrapping the synchronous `kickoff()` in a thread. This is useful for simpler async integration or backward compatibility.
Both async methods support streaming when `stream=True` is set on the crew:
252
+
253
+
```python Code
254
+
import asyncio
255
+
from crewai import Crew, Agent, Task
256
+
257
+
agent = Agent(
258
+
role="Researcher",
259
+
goal="Research and summarize topics",
260
+
backstory="You are an expert researcher."
261
+
)
262
+
263
+
task = Task(
264
+
description="Research the topic: {topic}",
265
+
agent=agent,
266
+
expected_output="A comprehensive summary of the topic."
267
+
)
268
+
269
+
crew = Crew(
270
+
agents=[agent],
271
+
tasks=[task],
272
+
stream=True# Enable streaming
273
+
)
274
+
275
+
asyncdefmain():
276
+
streaming_output =await crew.akickoff(inputs={"topic": "AI trends in 2024"})
277
+
278
+
# Async iteration over streaming chunks
279
+
asyncfor chunk in streaming_output:
280
+
print(f"Chunk: {chunk.content}")
281
+
282
+
# Access final result after streaming completes
283
+
result = streaming_output.result
284
+
print(f"Final result: {result.raw}")
285
+
286
+
asyncio.run(main())
287
+
```
288
+
289
+
## Potential Use Cases
290
+
291
+
-**Parallel Content Generation**: Kickoff multiple independent crews asynchronously, each responsible for generating content on different topics. For example, one crew might research and draft an article on AI trends, while another crew generates social media posts about a new product launch.
292
+
293
+
-**Concurrent Market Research Tasks**: Launch multiple crews asynchronously to conduct market research in parallel. One crew might analyze industry trends, while another examines competitor strategies, and yet another evaluates consumer sentiment.
294
+
295
+
-**Independent Travel Planning Modules**: Execute separate crews to independently plan different aspects of a trip. One crew might handle flight options, another handles accommodation, and a third plans activities.
296
+
297
+
## Choosing Between `akickoff()` and `kickoff_async()`
298
+
299
+
| Feature |`akickoff()`|`kickoff_async()`|
300
+
|---------|--------------|-------------------|
301
+
| Execution model | Native async/await | Thread-based wrapper |
302
+
| Task execution | Async with `aexecute_sync()`| Sync in thread pool |
303
+
| Memory operations | Async | Sync in thread pool |
304
+
| Knowledge retrieval | Async | Sync in thread pool |
305
+
| Best for | High-concurrency, I/O-bound workloads | Simple async integration |
0 commit comments