Overview
Themulti_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.agent(AgentType): Agent instance to runtask(str): Task string to execute*args(Any): Additional positional arguments**kwargs(Any): Additional keyword arguments
run_agent_async()
Runs a single agent asynchronously using asyncio.agent(AgentType): Agent instance to runtask(str): Task string to execute
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.agents(List[AgentType]): List of Agent instances to run concurrentlytask(str): Task string to execute by all agents
run_agents_concurrently()
Optimized concurrent agent runner using ThreadPoolExecutor with image support and flexible output formats.agents(List[AgentType]): List of Agent instances to run concurrentlytask(str): Task string to executeimg(Optional[str]): Optional image data to pass to agentrun()if supported. Only passed when notNonemax_workers(Optional[int]): Maximum number of threads in the executor. Defaults to 95% of CPU coresreturn_agent_output_dict(bool): If True, returns a dict mapping agent names to outputs. Keys useagent_name, thenname, thenstr(agent)
- If
return_agent_output_dict=False: List of outputs from each agent in input order, soresults[i]isagents[i]’s (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()
Runs multiple agents on the same task in batches. Despite the name, it does not start processes: each batch runs throughrun_agents_concurrently_async on a fresh event loop, and batches run one after another.
agents(List[Agent]): List of Agent instances to run concurrentlytask(str): Task string to execute by all agentsbatch_size(int): Number of agents to run in parallel in each batch. Defaults to CPU count
Batched and Grid Execution
batched_grid_agent_execution()
Runsagents[i] on tasks[i] for every i, concurrently in a thread pool.
agents(List[AgentType]): List of agent instancestasks(List[str]): List of tasks, one for each agentmax_workers(int): Maximum number of threads to use. Defaults to 95% of CPU cores
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. Eachagents[i] runs tasks[i]. Unlike batched_grid_agent_execution, it accepts plain callables and returns None for a failed slot instead of the exception.
agents(List[Agent | Callable]): Agents to run. An object with arunmethod is called asagent.run(task, img); any other callable is called asagent(task, img).tasks(List[str]): One task per agent. Must have the same length asagents. Required in practice: passingNonefails.imgs(List[str], optional): One image input per agent. Must be the same length asagentswhen provided. WhenNone, every call receivesimg=None.max_workers(int): Thread pool size. Defaults to ~90% of CPU cores.
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.
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.agent_task_pairs(List[tuple[AgentType, str]]): List of (agent, task) tuplesbatch_size(int): Number of agents to run in parallel in each batch. Batches run one after another. Default: 10max_workers(int): Maximum number of threads per batch. Defaults to 95% of CPU cores
[]
Utility Functions
get_swarms_info()
Fetches and formats information about all available swarms in the system.swarms(List[Callable]): List of swarm objects to get information about
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.agents(List[Union[Agent, Callable]]): List of agent objects to get information aboutteam_name(str, optional): Optional team name to display
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
- Always handle exceptions in results, as some agents may fail
- Use appropriate
max_workersbased on system resources - Monitor memory usage for large agent counts
- Consider batch processing for very large numbers of agents
- Use
return_agent_output_dict=Truefor structured, named results - Pass image data to agents that support multimodal processing