Skip to main content
A ForestSwarm holds trees of agents. Each Tree groups the agents for one domain, such as tax or investing. When a task comes in, the forest walks its trees in order and hands the task to the first agent that matches it. No model makes the choice: matching uses keywords taken from each agent’s system prompt, then the embedding similarity between the task and that prompt. One agent answers each task. ForestSwarm, Tree and TreeAgent are not exported from the top-level swarms package. Import them from swarms.structs.tree_swarm. Reference: ForestSwarm.

How an agent is picked

1. Build a forest and run a task

Each specialist is a TreeAgent: a regular Agent with an embedding model for its system prompt. Put the domain words you want to match at the start of the prompt and repeat the most important one. The keywords are the five most frequent words, and ties go to the word that comes first.
The task contains tax and delaware, two of the Corporate-Tax-Advisor’s keywords, so that agent answers.
The default embedding model is text-embedding-ada-002. In our tests it scored unrelated text above 0.7, so the first agent in the first tree matched almost every task, including a question about stocks and one about retirement accounts. text-embedding-3-small scores unrelated text far lower, so routing follows the keywords and the embedding check passes only on a close match. Set embedding_model_name on every agent.

2. Check the routing before you run

find_relevant_tree() and Tree.find_relevant_agent() run the same matching as run() without calling the language model. Use them to test your system prompts. They do call the embedding model for any agent whose keywords miss.
The keywords print as, for example, ['retirement', 'planner', 'savings', '401k', 'roth']. The first task goes to Retirement-Planner through 401k and roth, and the second to Stock-Analyst through earnings. Nothing matches the banana bread question, so both lookups return None.

3. Run several tasks

batched_run() calls run() once per task and returns the results in task order.
The banana bread task returns the string "No relevant agent found to handle this task." instead of an answer.
batched_run() also accepts max_workers to run tasks in threads. Leave it unset when two tasks can reach the same agent. A TreeAgent has one conversation, and in our test two tasks that ran at the same time on the same agent came back with the same answer.

Reading the output

  • One answer per task. run() returns the selected agent’s output, shaped by that agent’s output_type (the Agent default is "str-all-except-first"). The agent is reset before each task, so the output covers only this task.
  • No match. run() returns "No relevant agent found to handle this task."
  • Errors. run() never raises. If routing or the agent fails, it returns None. Pass verbose=True to the forest to see the error in the log.
  • No shared history. The forest’s conversation is not updated by run(). Keep your own record of which agent answered, using find_relevant_tree() as in step 2.
  • Token usage. forest.usage sums the provider token usage of every agent in every tree.

Tips

  • Order matters. The first match wins and agents are not ranked against each other. Put the most specific trees first.
  • Keep keywords specific. A keyword such as advisor or planner matches any task that uses the word. Name agents and write prompts so the frequent words are domain terms.
  • Building costs one embedding call per agent. Build the forest once and reuse it.

Next steps