> ## 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.

# BatchedGridWorkflow Guide

> Give each agent its own task and run them all at once, repeat the grid over several loops, and read the per-loop results

A `BatchedGridWorkflow` pairs agents with tasks by position: the first agent gets the first task, the second agent the second task, and so on. All pairs run at the same time, one thread per agent. Use it when you have a different job for each agent and the jobs don't depend on each other. `run()` returns a list with one entry per loop, and each entry is a list of the agents' outputs in agent order.

Reference: [BatchedGridWorkflow](https://docs.swarms.world/api/batched-grid-workflow.md).

## 1. One task per agent

Three agents each get their own task. The tasks run in parallel, and `results[0]` holds the first loop's outputs in the same order as `agents`.

```python theme={null}
from swarms import Agent, BatchedGridWorkflow


def make_agent(name: str, system_prompt: str) -> Agent:
    """Build a one-loop agent on a small model."""
    return Agent(
        agent_name=name,
        system_prompt=system_prompt,
        model_name="gpt-5.4-mini",
        max_loops=1,
    )


agents = [
    make_agent("Headline-Writer", "You write one punchy headline. Reply with the headline only."),
    make_agent("Summarizer", "You summarize text in one sentence."),
    make_agent("Translator", "You translate English into French. Reply with the translation only."),
]

tasks = [
    "Write a headline for a story about a town that banned cars from its center.",
    "Summarize: The town council voted 7-2 to close the center to cars on weekends, starting in May.",
    "Translate: The town center is closed to cars on weekends.",
]

workflow = BatchedGridWorkflow(name="Newsroom-Grid", agents=agents)
results = workflow.run(tasks)

for agent, output in zip(agents, results[0]):
    print(f"{agent.agent_name}: {output}\n")
```

```text theme={null}
Headline-Writer: Town Turns Center into a Car-Free Haven

Summarizer: The town council voted 7-2 to ban cars from the center on weekends starting in May.

Translator: Le centre-ville est fermé aux voitures le week-end.
```

Use a separate agent object for each position. The pairs run in parallel, so one agent listed twice would run two tasks on the same conversation at once.

## 2. Read the output

`BatchedGridWorkflow` has no `output_type` and keeps no shared conversation. Each output is exactly what that agent's `run()` returned, so the agent's own `output_type` decides its shape:

| Shape | Meaning |
| - | - |
| `results` | A list with one entry per loop |
| `results[loop]` | A list with one output per agent, in agent order |
| `results[loop][i]` | What `agents[i].run(tasks[i])` returned in that loop |

With the agent default, `output_type="str-all-except-first"`, an output is everything in the agent's history after its first task, as text. On the first loop that is just the answer. On later loops it also holds the earlier answers and the repeated task. Set `output_type="final"` on the agents when you want only the latest answer.

## 3. Run the grid more than once

With `max_loops` above 1, the workflow sends the same tasks again. Outputs are not passed between agents. Each agent keeps its own conversation from one loop to the next, so it can see and improve its earlier answer. Every `run()` call starts each agent from a clean history first, so nothing carries over from an earlier `run()`.

This block reuses `tasks` from section 1 and gives its agents `output_type="final"`:

```python theme={null}
headline_writer = Agent(
    agent_name="Headline-Writer",
    system_prompt="You write one punchy headline. If you wrote one before, write a better one.",
    model_name="gpt-5.4-mini",
    max_loops=1,
    output_type="final",
)
summarizer = Agent(
    agent_name="Summarizer",
    system_prompt="You summarize text in one sentence. If you summarized it before, make it shorter.",
    model_name="gpt-5.4-mini",
    max_loops=1,
    output_type="final",
)

two_pass = BatchedGridWorkflow(
    name="Two-Pass-Grid",
    agents=[headline_writer, summarizer],
    max_loops=2,
)
results = two_pass.run(tasks[:2])

for loop_number, loop_outputs in enumerate(results, start=1):
    print(f"--- Loop {loop_number} ---")
    print(f"Headline-Writer: {loop_outputs[0]}")
    print(f"Summarizer: {loop_outputs[1]}")
```

```text theme={null}
--- Loop 1 ---
Headline-Writer: Town Center Goes Car-Free, Residents Rediscover the Streets
Summarizer: The council voted 7-2 to close the center to cars on weekends starting in May.
--- Loop 2 ---
Headline-Writer: Town Center Goes Car-Free, Residents Rediscover the Streets
Summarizer: Council voted 7-2 to close the center to cars on weekends from May.
```

`max_loops` must be a positive integer, or the constructor raises `ValueError`.

## 4. Handle errors

The number of tasks must match the number of agents. A mismatch raises `ValueError`, which `run()` logs and raises again. An agent that fails does not stop the others: its exception object takes its place in the results, so check for it before you use an output.

```python theme={null}
try:
    workflow.run(tasks[:2])
except ValueError as error:
    print(f"Rejected: {error}")

results = workflow.run(tasks)

for agent, output in zip(agents, results[0]):
    if isinstance(output, Exception):
        print(f"{agent.agent_name} failed: {output}")
    else:
        print(f"{agent.agent_name}: {output}")
```

```text theme={null}
Rejected: The number of agents must match the number of tasks.
Headline-Writer: Town Ditches Cars, Reclaims Its Heart
Summarizer: The town council approved a 7-2 vote to close the center to cars on weekends starting in May.
Translator: Le centre-ville est fermé aux voitures le week-end.
```

## Tips

* **Same task for everyone?** Use a [ConcurrentWorkflow](https://docs.swarms.world/api/concurrent-workflow.md) instead. `BatchedGridWorkflow` is for a different task per agent.
* **Need one agent's output in another?** Use a [SequentialWorkflow](https://docs.swarms.world/api/sequential-workflow.md) or [GraphWorkflow](https://docs.swarms.world/examples/multi-agent/graph-workflow-example.md). The grid never passes outputs between agents.
* **Check the cost.** `workflow.usage` sums the token usage of every agent in the grid.
* **Run one pass yourself.** `workflow.step(tasks)` runs the grid once and returns one list of outputs, without the loop and without resetting the agents.

## Next steps

* [BatchedGridWorkflow reference](https://docs.swarms.world/api/batched-grid-workflow.md)
* [Runnable scripts](https://github.com/kyegomez/swarms/tree/master/examples/multi_agent/batched_grid_workflow)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.