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. Theverifier checks a submitted answer; the run stops as soon as one passes.
True [7, 19, 42] agent_19. The verifier gets the text after ####, not the whole reply.
private_inputsneeds one string per member. Each string is added to that member’s first message. A list of the wrong length raisesValueError.rulesis added to every member’s system prompt.max_tokens_per_agentcaps each member’s output tokens. A member that reaches it stops working.timeoutcaps the work phase in seconds.
2. Read the result
The result is a plain dict. Continuing from step 1:
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:
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:
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 nosolve() 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.
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
timeoutruns out.
#### 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
seedto make the member names and teammate order repeatable. - Pass agent settings through
agent_kwargs. Members are built withprint_on=False,reasoning_prompt_on=False,persistent_memory=Falseandtool_retry_attempts=1; values you pass override these. - Use
verbose=Trueto log every message as it is sent.
Next steps
- SelfOrganizingSwarm reference
- HierarchicalSwarm, when you want a director to assign the work
- MajorityVoting, when members don’t need each other’s information