Lesson 49.4 · 4 of 5 · 4 min read · Reviewed 24 Nov 2026
Swarms and peer-to-peer
By the end, you can
- Describe swarm and peer-to-peer topologies.
- Know why they're rare in production.
The shape
[A] <---> [B]
^ \ / ^
| [C]-- |
v v
[D] <---> [E]
Any agentAgent: A system where a model decides its own next steps, calling tools in a loop until the task is done. can message any other. Work flows wherever agents send it.
Appeal
- Flexible: agents recruit help when they need it.
- No single bottleneck.
- Interesting for simulations and open-ended exploration.
Problems in practice
| Problem | Why |
|---|---|
| Unbounded cost | Messages can multiply with no one to stop them |
| Loops | A asks B, B asks C, C asks A |
| Debugging | No single trace tells the story |
| Accountability | Who owns the final answer? |
Taming it
If you do use peer messaging:
- Cap total messages, tokensToken: The unit a model reads and writes: a word, part of a word, a space or a symbol, mapped to an integer id. and time for the whole run.
- Give each agent a clear role and stop condition.
- Keep a shared log or blackboard everyone writes to, so there is one record.
- Have one agent own the final answer.
At that point you’ve rebuilt much of a supervisor, which is why most teams start there.
Key points
- In a swarm, agents talk to each other directly with no central controller, passing work as they see fit.
- It can explore widely and adapt, but is hard to predict, bound and debug.
- Costs and loops can grow quickly without a coordinator to stop them.
- Most production systems prefer a supervisor or pipeline, using peer messaging only within clear limits.
Tame a looping swarm
Bound a peer-to-peer swarm with a hop limit, a budget and an owner for the final answer.
You will have: A swarm simulation that answers what it can and stops cleanly on what it can't.
Steps
- Run the starter: the 'tax' question circles 1,000 times.
- Add a hop limit and a credit budget.
- Make the owning agent report the outcome when a limit is hit.
- Check that answerable questions are not affected.
Done when
- 'shipping' is answered by C after 2 hops.
- 'tax' stops after 6 hops and 30 credits with an owner's report.
- No run can spend more than the budget.
Open in the Playground No API key needed.
Starter code (Python)
# Peers pass a question around. Without bounds and an owner, it can circle for ever.
PEERS = {"A": "B", "B": "C", "C": "A"} # who each agent forwards to
KNOWS = {"A": {"billing"}, "B": {"returns"}, "C": {"shipping"}}
CREDITS_PER_MESSAGE = 5
SAFETY_NET = 1000 # only here so the starter ends
def ask_swarm(topic, owner="A"):
agent, hops, spent = owner, 0, 0
# TODO: add MAX_HOPS = 6 and BUDGET = 40 credits; when either is hit, the owner reports what was tried
while hops < SAFETY_NET:
if topic in KNOWS[agent]:
return f"{topic}: answered by {agent} after {hops} hops, {spent} credits"
agent = PEERS[agent]
hops += 1
spent += CREDITS_PER_MESSAGE
return f"{topic}: ran to the safety net after {hops} hops, {spent} credits"
print(ask_swarm("shipping"))
print(ask_swarm("tax"))
Show a solution
# Peers pass a question around. Without bounds and an owner, it can circle for ever.
PEERS = {"A": "B", "B": "C", "C": "A"} # who each agent forwards to
KNOWS = {"A": {"billing"}, "B": {"returns"}, "C": {"shipping"}}
CREDITS_PER_MESSAGE = 5
SAFETY_NET = 1000 # only here so the starter ends
def ask_swarm(topic, owner="A"):
agent, hops, spent = owner, 0, 0
MAX_HOPS, BUDGET = 6, 40
while hops < SAFETY_NET:
if hops >= MAX_HOPS or spent + CREDITS_PER_MESSAGE > BUDGET:
return f"{topic}: stopped after {hops} hops, {spent} credits. {owner} reports: no peer could answer"
if topic in KNOWS[agent]:
return f"{topic}: answered by {agent} after {hops} hops, {spent} credits"
agent = PEERS[agent]
hops += 1
spent += CREDITS_PER_MESSAGE
return f"{topic}: ran to the safety net after {hops} hops, {spent} credits"
print(ask_swarm("shipping"))
print(ask_swarm("tax"))