Skip to main content
A SelfOrganizingSwarm runs several copies of one model as a team with no leader, no roles and no turn order. Every member gets the same task and its own history. Members may also get a private input and a private set of tools. They talk only through three messaging tools, and each decides for itself when to talk. A member finishes by writing a line that starts with ####, and the team succeeds if any member is right. Each run() builds a fresh team with random names such as agent_17, so runs never share history. It returns a dict with the team answer, whether it was right, every message sent and the tokens each member used. Reference: SelfOrganizingSwarm.

1. Members share private information

Three members each hold one number. No member can answer alone, so they have to message each other. The verifier checks a submitted answer; the run stops as soon as one passes.
This prints something like True [7, 19, 42] agent_19. The verifier gets the text after ####, not the whole reply.
  • private_inputs needs one string per member. Each string is added to that member’s first message. A list of the wrong length raises ValueError.
  • rules is added to every member’s system prompt.
  • max_tokens_per_agent caps each member’s output tokens. A member that reaches it stops working. timeout caps the work phase in seconds.

2. Read the result

The result is a plain dict. Continuing from step 1:
A run of this example printed:
The cost part of team_reward is token_cost * min(1, total_tokens / (num_agents * max_tokens_per_agent)). Here 291 tokens out of an allowance of 3 x 2048 cost about 1 percent.

3. Compare with a best-of-N baseline

communication=False gives the members no messaging tools and a solo prompt, so the run is N independent attempts. Run the same task both ways to see whether talking helps. Continuing from step 1:
Output from one run:
Each solo member only knew its own number, so each submitted a list with one number in it.

4. Give members private tools that end the run

task_tools is a factory. The swarm calls it once per member as task_tools(agent_name, solve), and it returns that member’s own list of tools. When a tool calls solve(answer), the team is marked solved and every member stops. No verifier is needed. Here each member knows one digit of a lock code and has its own copy of the lock:
This prints something like True agent_55 583 opened by agent_55. The string passed to solve() becomes answer.
A tool that raises returns its error to the model as text. This stops the agent’s tool retry from running the whole turn again and sending a message twice.

5. Run without a verifier

With no verifier and no solve() call, the run ends when every member is idle or finished. success and team_reward are None, and answer is the answer submitted most often.
Output:

How a run ends

The work phase ends when:
  • an answer passes the verifier (with stop_on_success=True, the default),
  • a task tool calls solve(),
  • every member is idle or finished, or
  • timeout runs out.
Members without an answer are then asked once for a final one. In that last step the final non-empty line of a reply counts as the answer if it has no #### line. Pass final_prompt=None to skip it.

Tips

  • Pick a tool-calling model. Messaging and task tools are function calls.
  • Spell out the answer format in the task. Members finish with ####, and the verifier sees only the text after it.
  • Use seed to make the member names and teammate order repeatable.
  • Pass agent settings through agent_kwargs. Members are built with print_on=False, reasoning_prompt_on=False, persistent_memory=False and tool_retry_attempts=1; values you pass override these.
  • Use verbose=True to log every message as it is sent.

Next steps