Fan Out to Subagents Within a Budget
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.
- Go through
subtasksin order. A subtask that repeats an earlier one (the same text afterstrip()andcasefold()) is never run again and never listed again. - Before running a subtask, check that
spent + worker.estimate(subtask)is withinbudget. If it is not, do not run it: add it toskippedand move on, because a cheaper subtask later may still fit. - After a run, add the real
tokenstospent, never the estimate.spentis what the swarm actually paid. - Return
{"answers": {subtask: answer}, "skipped": [subtask, ...], "spent": total}.answersholds each distinct subtask once, keyed by its first spelling, in first-seen order.skippedis 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.