4.9 KiB
Parallel template workflow agent
The ParallelAgent class is a template workflow agent that executes its sub-agents concurrently. This execution strategy can dramatically speed up workflows where two or more tasks can be performed independently. For scenarios prioritizing speed and involving independent, resource-intensive tasks, this templated workflow facilitates parallel execution, which can significantly reduce overall processing time. When using this workflow type, it is important that each sub-agent can operate without depending on the other sub-agents. This workflow type is particularly beneficial for operations like multi-source data retrieval or heavy computations, where parallelization yields substantial performance gains.
As with other templated workflows, the execution of a ParallelAgent object is not controlled by an AI model, and is deterministic in how it executes its sub-agents. The sub-agents specified in the parallel execution set may or may not utilize AI models, but the overall execution of those sub-agents is ultimately managed by the ParallelAgent object you define.
!!! note "Alternative: graph-based workflows"
Starting in ADK 2.0 for Python and Go, templated workflows have been superseded
by more flexible workflow structures, including
[graph-based workflows](/graphs/) and
[dynamic workflows](/graphs/dynamic/).
How it works
When the ParallelAgent's run_async() method is called:
- Concurrent Execution: It initiates the
run_async()method of each sub-agent present in thesub_agentslist concurrently. This means all the agents start running at (approximately) the same time. - Independent Branches: Each sub-agent operates in its own execution branch. There is no automatic sharing of conversation history or state between these branches during execution.
- Result Collection: The
ParallelAgentmanages the parallel execution and, typically, provides a way to access the results from each sub-agent after they have completed (e.g., through a list of results or events). The order of results may not be deterministic.
Independent Execution and State Management
It's crucial to understand that sub-agents within a ParallelAgent run independently. If you need communication or data sharing between these agents, you must implement it explicitly. Possible approaches include:
- Shared
InvocationContext: You could pass a sharedInvocationContextobject to each sub-agent. This object could act as a shared data store. However, you'd need to manage concurrent access to this shared context carefully (e.g., using locks) to avoid race conditions. - External State Management: Use an external database, message queue, or other mechanism to manage shared state and facilitate communication between agents.
- Post-Processing: Collect results from each branch, and then implement logic to coordinate data afterwards.
Full Example: Parallel Web Research
Imagine researching multiple topics simultaneously:
-
Researcher Agent 1: An
LlmAgentthat researches "renewable energy sources." -
Researcher Agent 2: An
LlmAgentthat researches "electric vehicle technology." -
Researcher Agent 3: An
LlmAgentthat researches "carbon capture methods."ParallelAgent(sub_agents=[ResearcherAgent1, ResearcherAgent2, ResearcherAgent3])
These research tasks are independent. Using a ParallelAgent allows them to run concurrently, potentially reducing the total research time significantly compared to running them sequentially. The results from each agent would be collected separately after they finish.
???+ "Full Code"
=== "Python"
```py
--8<-- "examples/python/snippets/agents/workflow-agents/parallel_agent_web_research.py:init"
```
=== "TypeScript"
```typescript
--8<-- "examples/typescript/snippets/agents/workflow-agents/parallel_agent_web_research.ts:init"
```
=== "Go"
```go
--8<-- "examples/go/snippets/agents/workflow-agents/parallel/main.go:init"
```
=== "Java"
```java
--8<-- "examples/java/snippets/src/main/java/agents/workflow/ParallelResearchPipeline.java:full_code"
```
!!! tip "Global instructions for all agents"
Apply consistent rules or identity across all agents in your workflow using `GlobalInstructionPlugin` registered on your `App` or `Runner` object. Do not use the `global_instruction` parameter on `Agent` which is deprecated since ADK Python v1.16.0.
