Skip to main content
A SubagentRegistry runs agent tasks in the background on a thread pool. spawn() hands an agent a task and returns a task id at once; the agent keeps working while your code moves on. Later you wait with gather() and read each task’s status, result or error by id. The registry also handles retries, cancels queued tasks and enforces a depth limit for tasks that spawn more tasks. An Agent running with max_loops="auto" uses the same registry when it creates sub-agents and assigns them work. The last section shows that. Reference: SubagentRegistry.

1. Fan out and gather

Three researchers each get one question. Keep the id that spawn() returns for each task, so you can match results to tasks later.
This prints one line per topic, such as task-5005430c completed wind power: The main drawback of wind power is that it is intermittent, .... gather() blocks until the tasks finish and returns their results as a list. The list is not keyed by task and its order is not guaranteed, so read results with get_task(task_id) as above.
Give each task its own agent. The registry runs the object you pass, as it is. An Agent keeps its conversation between runs, so one agent spawned for several tasks mixes their histories into its answers, and running it on two threads at once mixes them further.

2. Inspect the tasks

Every task is a SubagentTask record. Continuing from step 1:
Output:
registry.tasks is a copy of every task, keyed by id. get_results() maps the id of each completed task to its result, and of each failed task to its exception; it does not wait. shutdown() stops the pool from taking new tasks without waiting for running ones.

3. Retry some errors and record failures

max_retries reruns a task that raised. retry_on limits retries to the listed exception types; leave it out to retry any exception. With fail_fast=False a failed task stores its error and returns None instead of raising in its thread. Any object with a run(task) method can be spawned; its agent_name, if it has one, is used in the logs. This stand-in times out once, then answers. The second task uses a model that does not exist, so it fails with an error that is not on the retry list:
Output, after the model error is logged:
The flaky task succeeded on its first retry. The broken agent raised AgentLLMError, which is not in retry_on, so it failed at once with no retries. Either way a failed task ends with status failed and its exception in error. fail_fast only changes what gather() returns for a task that fails while it waits: with fail_fast=True (the default) the list holds the exception, and with fail_fast=False it holds None.

4. Take the first result and cancel queued work

gather(strategy="wait_first") returns as soon as one more task finishes. Tasks still running are left out of that list and keep going. cancel() stops a task that is still waiting for a free thread; a task that has started cannot be cancelled. With one worker thread, tasks run one at a time, so the later ones wait in the queue:
Output:
Each gather() call covers every task in the registry. Tasks that had already finished come first, then the ones that finished during the wait, in no set order. A cancelled task shows up as a CancelledError. Pass timeout in seconds to stop waiting; tasks still running then are left out.

5. Build a tree of tasks with a depth limit

A task can lead to more tasks. Record the link with parent_id and depth when you spawn. The registry does not work out the depth itself; it stores what you pass and refuses any depth above max_depth. A planner splits a topic, then one writer per subtopic works at depth 1:
Output from one run:
The refused spawn raises before a task is created, so nothing is left in the registry. The default max_depth is 3.

6. Let an autonomous agent delegate

An Agent with max_loops="auto" has four sub-agent tools: selected_tools limits the autonomous tools to the ones you list. This lead agent gets the planning tools and the sub-agent tools, and nothing else:
The answer includes both sub-agents’ one-sentence replies and the comparison. The loop then prints the sub-agents the lead made:
How sub-agents are built:
  • They use the lead’s model_name and get the lead’s tools.
  • A sub-agent with tools gets max_loops=5, and one without gets max_loops=1, as here. Sub-agents never run in "auto" mode, so they cannot start sub-agents of their own.
  • The lead’s registry is created on first use with max_depth=3.
See Autonomous mode for the rest of the autonomous tools.

Tips

  • Keep the ids from spawn(). They are the only way to tie a result to its task.
  • Size the pool with max_workers. The default comes from Python’s ThreadPoolExecutor. One agent call holds a thread for its whole run.
  • Call shutdown() when you are done. Spawning after shutdown() raises RuntimeError.

Next steps