Skip to content

< CollectionsA multi-agent research system · 2 of 9 ·89 · Level 05, Agents

Fan Out to Subagents Within a Budget

medium · implement · Agents

A supervisor splits a question into subtasks and hands each to a subagent. Every subagent costs tokens, and the whole swarm shares one budget. Implement run_swarm(subtasks, worker, budget).

worker(subtask) runs one subagent and returns (answer, tokens). worker.estimate(subtask) is its guess of the cost before running; the real bill can be higher. budget is the total tokens the swarm may spend.

  1. Go through subtasks in order. A subtask that repeats an earlier one (the same text after strip() and casefold()) is never run again and never listed again.
  2. Before running a subtask, check that spent + worker.estimate(subtask) is within budget. If it is not, do not run it: add it to skipped and move on, because a cheaper subtask later may still fit.
  3. After a run, add the real tokens to spent, never the estimate. spent is what the swarm actually paid.
  4. Return {"answers": {subtask: answer}, "skipped": [subtask, ...], "spent": total}. answers holds each distinct subtask once, keyed by its first spelling, in first-seen order. skipped is in order.

The catch is rule 3. Estimates are guesses, and a subagent that overruns is billed for what it used. A supervisor that counts estimates believes it has budget left when it has none, and keeps fanning out past it.