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

# PlannerWorkerSwarm Guide

> Split a goal into a queue of tasks, let worker agents claim and run them in parallel, and have a judge decide when the goal is met

A `PlannerWorkerSwarm` keeps planning and doing apart. A planner turns your goal into a list of tasks with priorities and dependencies. Your worker agents pull tasks from a shared queue and run them in parallel, with no coordination between them. A judge then reads the results and decides whether the goal is met. If it is not, the planner gets the judge's feedback and plans the next cycle.

Use it when a goal splits into several pieces of work that can run side by side, and you want a check at the end of each round.

Reference: [PlannerWorkerSwarm](https://docs.swarms.world/api/planner-worker-swarm.md).

## How one cycle runs

| Role | Built from | Does |
| - | - | - |
| Planner | `planner_model_name` | Writes a plan and a list of tasks, each with a title, description, priority and the titles of the tasks it depends on |
| Workers | Your `agents` | Each worker claims the highest-priority task whose dependencies are done, runs it, and claims the next, until the queue is empty |
| Judge | `judge_model_name` | Scores the cycle from 0 to 10, says whether the goal is complete, and lists gaps for the next cycle |

The swarm builds the planner and judge for you. The planner does not see your workers or their descriptions, and any worker can claim any task. Each worker is reset before every task and receives the task's title and description plus the results of the tasks it depends on.

## 1. Plan, work, judge

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

workers = [
    Agent(
        agent_name="Researcher",
        agent_description="Gathers facts, figures and examples",
        model_name="gpt-5.4-mini",
        max_loops=1,
        print_on=False,
    ),
    Agent(
        agent_name="Writer",
        agent_description="Turns notes into short, clear prose",
        model_name="gpt-5.4-mini",
        max_loops=1,
        print_on=False,
    ),
]

swarm = PlannerWorkerSwarm(
    name="Brief-Swarm",
    agents=workers,
    planner_model_name="gpt-5.4-mini",
    judge_model_name="gpt-5.4-mini",
    max_loops=1,
    worker_timeout=300,
)

result = swarm.run(
    "Write a short brief on home heat pumps for homeowners: how they work, "
    "typical costs, and when they are a poor fit."
)

for message in result:
    print(f"--- {message['role']}")
    print(message["content"][:300])
```

The planner and judge default to `gpt-5.4`. A smaller model keeps this example quick; use a stronger one for real work, since planning and judging are the harder jobs. `worker_timeout` caps the time the workers get in each cycle.

The default `output_type="dict-all-except-first"` returns every message after your task, in order:

| Role | Content |
| - | - |
| `Planner` | The plan, in prose |
| A worker's `agent_name` | `[Task: <title>]` followed by the worker's result, one message per finished task |
| `CycleJudge` | `Quality: 9/10 \| Complete: True` followed by the judge's summary |

In our run, the planner made four tasks, the two workers finished all four in parallel, and the judge marked the goal complete with 9/10.

## 2. Read the task queue

`get_status()` reports the last run's queue: each task's status, priority and the worker that took it. `usage` adds up the tokens spent by the planner, the judge and every worker.

```python theme={null}
status = swarm.get_status()

print("Goal:", status["original_task"])
print("Progress:", status["queue"]["progress"])
print("Counts:", status["queue"]["status_counts"])

for task in status["queue"]["tasks"]:
    print(
        f"[{task['status']}] {task['title']} "
        f"(priority {task['priority']}, worker {task['assigned_worker']})"
    )

print(swarm.usage)
```

This prints lines such as `Progress: 4/4` and `[completed] Summarize typical heat pump costs (priority CRITICAL, worker Writer)`. Priorities run `LOW`, `NORMAL`, `HIGH`, `CRITICAL`. A failed task is retried up to twice before it is marked `failed`.

Each call to `run()` empties the queue and starts a new conversation, so one swarm can be reused for many goals.

## 3. Let the judge ask for more cycles

With `max_loops` above 1, a cycle the judge marks incomplete is followed by another. The planner gets the original goal, the judge's follow-up instructions and its list of gaps, and plans new tasks. Finished results stay in the queue, unless the judge decides the work has drifted and asks for a fresh start, which clears every task.

```python theme={null}
swarm = PlannerWorkerSwarm(
    name="Report-Swarm",
    agents=workers,
    planner_model_name="gpt-5.4-mini",
    judge_model_name="gpt-5.4-mini",
    max_loops=2,
    worker_timeout=300,
)

result = swarm.run(
    "Compare solar panels and heat pumps as a first home upgrade. Cover "
    "upfront cost, payback period, and three questions a homeowner should "
    "ask an installer."
)

for message in result:
    if message["role"] in ("Planner", "CycleJudge"):
        print(f"--- {message['role']}")
        print(message["content"][:400])
```

There is one `Planner` and one `CycleJudge` message per cycle. `max_loops` is an upper bound: the swarm stops as soon as the judge marks the goal complete, which in our run happened after the first cycle.

## 4. Break big tasks down and set timeouts

With `max_planner_depth=2`, every task the planner marks `CRITICAL` is cancelled and handed to a sub-planner, which splits it into smaller tasks. `task_timeout` limits each task: a task that runs longer fails with a `TimeoutError` and goes back in the queue to be retried. Here `output_type="final"` returns only the last message, which is the judge's verdict.

```python theme={null}
swarm = PlannerWorkerSwarm(
    name="Launch-Swarm",
    agents=workers,
    planner_model_name="gpt-5.4-mini",
    judge_model_name="gpt-5.4-mini",
    max_planner_depth=2,
    task_timeout=120,
    worker_timeout=300,
    output_type="final",
)

verdict = swarm.run(
    "Plan the launch of a small online bakery: menu, pricing, and a "
    "two-week social media calendar."
)
print(verdict)

for task in swarm.task_queue.get_all_tasks():
    origin = "sub-task" if task.parent_task_id else "top-level"
    print(f"[{task.status.value}] {task.priority.name} {origin}: {task.title}")
```

In our run, the menu and pricing tasks were `CRITICAL`, so each was cancelled and replaced by five or six sub-tasks. The calendar task depended on those two cancelled tasks, and dependencies are not moved to the sub-tasks, so it stayed `pending` and never ran. The judge scored the cycle 6/10 and marked it incomplete because the calendar was missing.

> [!WARNING]
>
> A task that depends on a decomposed `CRITICAL` task can never start. Use `max_planner_depth=2` together with `max_loops` of 2 or more, so the judge can plan the missing work in the next cycle, or keep the default `max_planner_depth=1` for goals whose tasks depend on each other.

When a task is not finished in time, the timed-out call is not stopped: the worker records the failure once that call returns. `worker_timeout` stops workers from claiming new tasks after the deadline.

## Reading the output

| You want | Use |
| - | - |
| Every plan, result and verdict | The default `output_type="dict-all-except-first"` |
| One string | `output_type="str-all-except-first"` |
| Only the judge's last verdict | `output_type="final"` |
| Task status, priorities and workers | `swarm.get_status()` |
| Each task's result object | `swarm.task_queue.get_all_tasks()` (`result`, `error`, `status`, `depends_on`, `parent_task_id`) |
| Tokens spent | `swarm.usage` |

`run()` raises `ValueError` when no task is given. The constructor raises `ValueError` when `agents` is empty or `max_loops` is 0 or less.

## Tips

* **Workers are interchangeable.** The planner writes tasks without knowing who will run them, so give every worker the tools and knowledge any task might need.
* **Keep worker `max_loops=1`** unless a worker needs several tool calls. The swarm's own cycles handle iteration.
* **Set `worker_timeout` in production**, so a slow cycle cannot run on without limit.

## Next steps

* [PlannerWorkerSwarm reference](https://docs.swarms.world/api/planner-worker-swarm.md)
* [PlannerGeneratorEvaluator guide](https://docs.swarms.world/examples/multi-agent/planner-generator-evaluator-example.md) for one generator refined step by step against a grader
* [Runnable scripts](https://github.com/kyegomez/swarms/tree/master/examples/multi_agent/planner_worker_swarm)


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