Skip to main content
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.

How one cycle runs

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

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

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