Skip to main content

Overview

The multi_agent_exec module provides a comprehensive set of utility functions for running multiple agents using various execution strategies. It includes synchronous and asynchronous execution methods, concurrent batch processing, and utility functions for information retrieval.

Installation

Function Overview

Single Agent Functions

run_single_agent()

Runs a single agent synchronously.
Parameters:
  • agent (AgentType): Agent instance to run
  • task (str): Task string to execute
  • *args (Any): Additional positional arguments
  • **kwargs (Any): Additional keyword arguments
Returns: Agent execution result

run_agent_async()

Runs a single agent asynchronously using asyncio.
Parameters:
  • agent (AgentType): Agent instance to run
  • task (str): Task string to execute
Returns: Agent execution result

Concurrent Execution Functions

run_agents_concurrently_async()

Runs multiple agents concurrently using asyncio.
Parameters:
  • agents (List[AgentType]): List of Agent instances to run concurrently
  • task (str): Task string to execute by all agents
Returns: List of outputs from each agent

run_agents_concurrently()

Optimized concurrent agent runner using ThreadPoolExecutor with image support and flexible output formats.
Parameters:
  • agents (List[AgentType]): List of Agent instances to run concurrently
  • task (str): Task string to execute
  • img (Optional[str]): Optional image data to pass to agent run() if supported
  • max_workers (Optional[int]): Maximum number of threads in the executor. Defaults to 95% of CPU cores
  • return_agent_output_dict (bool): If True, returns a dict mapping agent names to outputs
Returns:
  • If return_agent_output_dict=False: List of outputs from each agent in completion order (exceptions included if agents fail)
  • If return_agent_output_dict=True: Dictionary mapping agent names to outputs, preserving agent input order

run_agents_concurrently_multiprocess()

Manages and runs multiple agents concurrently in batches with optimized performance.
Parameters:
  • agents (List[Agent]): List of Agent instances to run concurrently
  • task (str): Task string to execute by all agents
  • batch_size (int): Number of agents to run in parallel in each batch. Defaults to CPU count
Returns: List of outputs from each agent

Batched and Grid Execution

batched_grid_agent_execution()

Runs multiple agents with different tasks concurrently using batched grid execution.
Parameters:
  • agents (List[AgentType]): List of agent instances
  • tasks (List[str]): List of tasks, one for each agent
  • max_workers (int): Maximum number of threads to use. Defaults to 90% of CPU cores
Returns: List of results from each agent Raises: ValueError if the number of agents doesn’t match the number of tasks

batch_agent_execution()

Runs a list of agents on a parallel list of tasks. Each agents[i] runs tasks[i] — unlike batched_grid_agent_execution (which runs every agent on every task), pairings are 1:1.
Parameters:
  • agents (List[Agent | Callable]): Agents to run.
  • tasks (List[str]): One task per agent. Must have the same length as agents.
  • imgs (List[str]): One optional image input per agent. Must be the same length as agents when provided.
  • max_workers (int): Thread pool size. Defaults to ~90% of CPU cores.
Returns: List of results in the order the input pairings were submitted; failed tasks yield None in that slot. Raises: BatchAgentExecutionError wrapping any internal failure. Mismatched len(agents) vs len(tasks) is wrapped from the inner ValueError.
Pair this with run_agents_concurrently (every agent runs the same task) and batched_grid_agent_execution (every agent runs every task) to cover the three common shapes of “run a bunch of agents”.

run_agents_with_different_tasks()

Runs multiple agents with different tasks concurrently, processing them in batches.
Parameters:
  • agent_task_pairs (List[tuple[AgentType, str]]): List of (agent, task) tuples
  • batch_size (int): Number of agents to run in parallel in each batch. Default: 10
  • max_workers (int): Maximum number of threads
Returns: List of outputs from each agent, in the same order as input pairs

Utility Functions

get_swarms_info()

Fetches and formats information about all available swarms in the system.
Parameters:
  • swarms (List[Callable]): List of swarm objects to get information about
Returns: Formatted string containing names and descriptions of all swarms

get_agents_info()

Fetches and formats information about all available agents in the system.
Parameters:
  • agents (List[Union[Agent, Callable]]): List of agent objects to get information about
  • team_name (str, optional): Optional team name to display
Returns: Formatted string containing names and descriptions of all agents

Advanced Multi-Agent Workflow Example

Error Handling and Best Practices

Performance Considerations

Best Practices

  1. Always handle exceptions in results, as some agents may fail
  2. Use appropriate max_workers based on system resources
  3. Monitor memory usage for large agent counts
  4. Consider batch processing for very large numbers of agents
  5. Use return_agent_output_dict=True for structured, named results
  6. Pass image data to agents that support multimodal processing

Source Code

View the source code on GitHub