Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.swarms.world/llms.txt

Use this file to discover all available pages before exploring further.

Overview

The BatchedGridWorkflow is a multi-agent orchestration pattern that executes tasks in a batched grid format, where each agent processes a different task simultaneously. This workflow is particularly useful for parallel processing scenarios where you have multiple agents and multiple tasks that can be distributed across them. The BatchedGridWorkflow provides a structured approach to:
  • Execute multiple tasks across multiple agents in parallel
  • Manage conversation state across execution loops
  • Handle error scenarios gracefully
  • Control the number of execution iterations

Architecture

Installation

pip install -U swarms

Key Features

FeatureDescription
Parallel ExecutionMultiple agents work on different tasks simultaneously
Conversation ManagementMaintains conversation state across execution loops
Error HandlingComprehensive error logging and exception handling
Configurable LoopsControl the number of execution iterations
Agent FlexibilitySupports any agent type that implements the AgentType interface

Attributes

id
str
default:"swarm_id()"
Unique identifier for the workflow.
name
str
default:"BatchedGridWorkflow"
Name of the workflow.
description
str
Description of what the workflow does.
agents
List[AgentType]
default:"None"
List of agents to execute tasks.
conversation_args
dict
default:"None"
Arguments for the conversation.
max_loops
int
default:"1"
Maximum number of execution loops to run (must be >= 1).

Methods

step()

Execute one step of the batched grid workflow.
def step(self, tasks: List[str])
Parameters:
  • tasks (List[str]): List of tasks to execute
Returns: Output from the batched grid agent execution

run()

Run the batched grid workflow with the given tasks. This is the main entry point that includes error handling.
def run(self, tasks: List[str]) -> str
Parameters:
  • tasks (List[str]): List of tasks to execute
Returns: str - The final conversation string after all loops

run_()

Internal method that runs the workflow without error handling.
def run_(self, tasks: List[str]) -> str
Parameters:
  • tasks (List[str]): List of tasks to execute
Returns: str - The final conversation string after all loops

Usage Examples

Basic Usage

from swarms import Agent, BatchedGridWorkflow

# Initialize the ETF-focused agent
agent = Agent(
    agent_name="ETF-Research-Agent",
    agent_description="Specialized agent for researching, analyzing, and recommending Exchange-Traded Funds (ETFs) across various sectors and markets.",
    model_name="claude-sonnet-4-20250514",
    dynamic_temperature_enabled=True,
    max_loops=1,
    dynamic_context_window=True,
)


# Create workflow with default settings
workflow = BatchedGridWorkflow(agents=[agent, agent])

# Define simple tasks
tasks = [
    "What are the best GOLD ETFs?",
    "What are the best american energy ETFs?",
]

# Run the workflow
result = workflow.run(tasks)

print(result)

Multi-Loop Execution

from swarms import Agent, BatchedGridWorkflow

# Create workflow with multiple loops
workflow = BatchedGridWorkflow(
    agents=[agent1, agent2, agent3],
    max_loops=3,
    conversation_args={"message_id_on": True}
)

# Execute tasks with multiple iterations
tasks = ["Task 1", "Task 2", "Task 3"]
result = workflow.run(tasks)

Error Handling

The workflow includes comprehensive error handling:
  • Validation: Ensures max_loops is a positive integer
  • Execution Errors: Catches and logs exceptions during execution
  • Detailed Logging: Provides detailed error information including traceback

Best Practices

Best PracticeDescription
Agent SelectionChoose agents with complementary capabilities for diverse task processing
Task DistributionEnsure tasks are well-distributed and can be processed independently
Loop ConfigurationUse multiple loops when iterative refinement is needed
Error MonitoringMonitor logs for execution errors and adjust agent configurations accordingly
Resource ManagementConsider computational resources when setting up multiple agents

Use Cases

Use CaseDescription
Content GenerationMultiple writers working on different topics
Data AnalysisDifferent analysts processing various datasets
Research TasksMultiple researchers investigating different aspects of a problem
Parallel ProcessingAny scenario requiring simultaneous task execution across multiple agents

Source Code

View the source code on GitHub