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. Every function on this page is exported from the top-level swarms package, so from swarms import run_agents_concurrently works. They also remain importable from swarms.structs.multi_agent_exec and swarms.structs.batch_agent_execution.
These helpers call agent.run() on the agents you pass and do not reset their conversations. Running the same Agent instance twice, or in two threads at once, carries one task’s history into the next. Pass distinct agent instances when tasks must not share context.

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. The blocking agent.run() call runs in a worker thread via asyncio.to_thread, so it does not block the event loop

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, in input order Raises: The first exception raised by any agent. Results from the other agents are not returned in that case

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. Only passed when not None
  • 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. Keys use agent_name, then name, then str(agent)
Returns:
  • If return_agent_output_dict=False: List of outputs from each agent in input order, so results[i] is agents[i]’s (exceptions included if agents fail)
  • If return_agent_output_dict=True: Dictionary mapping agent names to outputs, preserving agent input order
The executor carries the caller’s OpenTelemetry context into each worker thread, so agent spans nest under the span that called this function.

run_agents_concurrently_multiprocess()

Runs multiple agents on the same task in batches. Despite the name, it does not start processes: each batch runs through run_agents_concurrently_async on a fresh event loop, and batches run one after another.
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, in input order
It creates and runs its own event loop, so call it from synchronous code. Inside a running event loop, await run_agents_concurrently_async(...) instead.

Batched and Grid Execution

batched_grid_agent_execution()

Runs agents[i] on tasks[i] for every i, concurrently in a thread pool.
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 95% of CPU cores
Returns: List of results in input order, so results[i] is agents[i]’s. A failed agent’s slot holds the exception it raised 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, with an optional parallel list of images. Each agents[i] runs tasks[i]. Unlike batched_grid_agent_execution, it accepts plain callables and returns None for a failed slot instead of the exception.
Parameters:
  • agents (List[Agent | Callable]): Agents to run. An object with a run method is called as agent.run(task, img); any other callable is called as agent(task, img).
  • tasks (List[str]): One task per agent. Must have the same length as agents. Required in practice: passing None fails.
  • imgs (List[str], optional): One image input per agent. Must be the same length as agents when provided. When None, every call receives img=None.
  • max_workers (int): Thread pool size. Defaults to ~90% of CPU cores.
Returns: List of results in agent order, so results[i] is agents[i]’s. A failed call is logged and leaves None in its slot. Raises: BatchAgentExecutionError wrapping any setup failure, including the ValueError for mismatched agents/tasks or agents/imgs lengths.
Use run_agents_concurrently when every agent runs the same task, and batch_agent_execution, batched_grid_agent_execution or run_agents_with_different_tasks when each agent gets its own task.

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. Batches run one after another. Default: 10
  • max_workers (int): Maximum number of threads per batch. Defaults to 95% of CPU cores
Returns: List of outputs from each agent, in the same order as input pairs. A failed agent’s slot holds the exception it raised. An empty input returns []

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 with each swarm’s name, description, agent count and swarm_type. Every swarm must have those four attributes. Returns "No swarms currently available in the system." for an empty list

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 with each agent’s agent_name, agent_description, role, model_name and max_loops. Returns "No agents currently available in the system." for an empty list

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