Skip to content

Parallel agents

The ParallelAgent

The ParallelAgent is a workflow agent that executes its sub-agents concurrently. This dramatically speeds up workflows where tasks can be performed independently.

Use ParallelAgent when: For scenarios prioritizing speed and involving independent, resource-intensive tasks, a ParallelAgent facilitates efficient parallel execution. When sub-agents operate without dependencies, their tasks can be performed concurrently, significantly reducing overall processing time.

As with other workflow agents, the ParallelAgent is not powered by an LLM, and is thus deterministic in how it executes. That being said, workflow agents are only concerned only with their execution (i.e. in parallel), and not their internal logic; the tools or sub-agents of a workflow agent may or may not utilize LLMs.

Example

This approach is particularly beneficial for operations like multi-source data retrieval or heavy computations, where parallelization yields substantial performance gains. Importantly, this strategy assumes no inherent need for shared state or direct information exchange between the concurrently executing agents.

How it works

When the ParallelAgent's run_async() method is called:

  1. Concurrent Execution: It initiates the run() method of each sub-agent present in the sub_agents list concurrently. This means all the agents start running at (approximately) the same time.
  2. 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.
  3. Result Collection: The ParallelAgent manages 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 shared InvocationContext object 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.

Parallel Agent

Full Example: Parallel Web Research

Imagine researching multiple topics simultaneously:

  1. Researcher Agent 1: An LlmAgent that researches "renewable energy sources."
  2. Researcher Agent 2: An LlmAgent that researches "electric vehicle technology."
  3. Researcher Agent 3: An LlmAgent that 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.

Code