Who speaks next
Taking turns wastes turns
With bots taking turns in a fixed order, every bot writes after every message, even when it has nothing to add. Each of those writes is one more time an LLM runs: more waiting and more cost.
4 bots wrote, 1 was needed: 3 wasted writes.
fixed order: every bot writes, needed or not
What if the message itself said who should answer?
Way 1: @mentions
Whoever writes a message names who should answer, with @ and the name. The program reads the message, finds the names, and only those bots reply. If no bot is named, the chat waits for the CEO.
next = the bots @mentioned
Finding names is plain Python, so deciding costs no LLM writes.
Easy for bots. But people in a group chat don't @mention all the time.
Way 2: a coordinator
A coordinator is one more bot whose only job is to read the latest message and name who replies next, or CEO when it's done. Nobody has to @mention anyone. But the coordinator is an LLM too, so it writes once after every message.
coordinator writes = messages in the chat
5 messages (the task + 4 replies) → 5 coordinator writes. With the 4 replies: 9 LLM writes.
Count it on new chats.
✏️ Practice 3.1
Remember:
fixed order: every bot writes, needed or not
next = the bots @mentioned (no LLM writes to decide)
coordinator writes = messages in the chat
One more way: no coordinator, no @.
Way 3: every bot decides
Like people in a real group chat: after each bot's message, every other bot reads it and asks itself “is this for me?”. Each of those questions is one LLM write.
deciding writes = bot messages × (bots − 1)
3 bots, one message → 1 × 2 = 2 deciding writes. And two bots can say yes at the same time.
Three ways. Which should a CEO pick?
Which way?
The ways differ in how many LLM writes it takes just to decide who's next. The replies themselves cost the same in every way.
A good mix: @mention when you know who should answer (free). Let a coordinator decide when you don't. Save “every bot decides” for small teams.
But what if two bots keep @mentioning each other?
A round limit
Bots can be too polite: one thanks the other, the other says you're welcome, and so on, forever. So after the CEO's message, the program allows only a set number of rounds, the round limit. In each round, every bot named replies once.
stop when no one is named, or at the round limit
Try it on new numbers.
✏️ Practice 3.2
Remember:
coordinator writes = messages in the chat
deciding writes = bot messages × (bots − 1)
stop when no one is named, or at the round limit
A real coordinator doesn't always answer neatly.
A real coordinator
In Colab, the coordinator is a real LLM. It's told to reply with one name, but it may write a whole sentence. So the program goes through the team's names, in order, and takes the first one that appears in the answer, spelled exactly. No bot's name: wait for the CEO.
next = the first bot name found in its answer
Here's all of it in Python.
The code
The same job, run twice: with @mentions, then with a coordinator. Guess each print, then press ▶ Run.
# The shared chat, and a list of who the LLM wrote for.
# Every LLM write costs time and money, so we count them: len(calls).
chat = []
calls = []
def post(who, text):
chat.append({"who": who, "text": text})
print(f"{who}: {text}")
BOTS = ["Marketing", "Production", "Operations"]
INSTRUCTIONS = {
"Marketing": "Plan Instagram posts and captions for Amal Designers. Use only facts in the chat.",
"Production": "Check sizes, stock and prices. Catalogue: pastel party frock, sizes 1 to 8 years, ₹1,450.",
"Operations": "Check packing and shipping. Shipping takes 3 days to most of India.",
}
def text_for(name):
lines = [INSTRUCTIONS[name], "", "Chat so far:"]
for m in chat:
lines.append(m["who"] + ": " + m["text"])
lines.append(name + ":")
return "\n".join(lines)
# Fake LLM replies for each bot, in order. A fresh copy for each run.
def fresh_replies():
return {
"Marketing": ["Caption: “Twirl-ready pastel frock 💫 Sizes 1 to 12 years. Ships in 2 days!” @Production please check.",
"Fixed: “…Sizes 1 to 8 years. Ships in 3 days.” @CEO ready to post."],
"Production": ["Sizes are 1 to 8 years, not 12. @Operations please check the shipping."],
"Operations": ["Shipping takes 3 days, not 2. @Marketing"],
}
def fake_llm(name, text):
calls.append(name)
return replies[name].pop(0)
# Way 1: @mentions. The bots named with @ reply next. No LLM write to decide.
def by_mentions(text):
names = []
for name in BOTS:
if "@" + name in text:
names.append(name)
return names
print(by_mentions("@Production please check."))
# Way 2: a coordinator reads the latest message and names who replies next.
# It is an LLM too, so it costs one write each time.
def by_coordinator(text):
calls.append("Coordinator")
name = coordinator_replies.pop(0)
if name in BOTS:
return [name]
return [] # not a bot's name: wait for the CEO
# The loop: after each message, ask the router who replies next.
# chat[-1] is the last message (-1 counts from the end).
def run_chat(task, router, round_limit=5):
post("CEO", task)
for i in range(round_limit):
names = router(chat[-1]["text"])
if names == []:
return "Waiting for the CEO."
for name in names:
post(name, fake_llm(name, text_for(name)))
return "Stopped: the round limit was reached."
replies = fresh_replies()
print(run_chat("Plan a post for the pastel frock. @Marketing", by_mentions))
print("LLM writes:", len(calls))
# The same job with a coordinator: start a fresh chat and count again
chat.clear()
calls.clear()
replies = fresh_replies()
coordinator_replies = ["Marketing", "Production", "Operations", "Marketing", "CEO"]
print(run_chat("Plan a post for the pastel frock. @Marketing", by_coordinator))
print("LLM writes:", len(calls))
$ python lesson3.py
['Production']
CEO: Plan a post for the pastel frock. @Marketing
Marketing: Caption: “Twirl-ready pastel frock 💫 Sizes 1 to 12 years. Ships in 2 days!” @Production please check.
Production: Sizes are 1 to 8 years, not 12. @Operations please check the shipping.
Operations: Shipping takes 3 days, not 2. @Marketing
Marketing: Fixed: “…Sizes 1 to 8 years. Ships in 3 days.” @CEO ready to post.
Waiting for the CEO.
LLM writes: 4
CEO: Plan a post for the pastel frock. @Marketing
Marketing: Caption: “Twirl-ready pastel frock 💫 Sizes 1 to 12 years. Ships in 2 days!” @Production please check.
Production: Sizes are 1 to 8 years, not 12. @Operations please check the shipping.
Operations: Shipping takes 3 days, not 2. @Marketing
Marketing: Fixed: “…Sizes 1 to 8 years. Ships in 3 days.” @CEO ready to post.
Waiting for the CEO.
LLM writes: 9The notebook has all of this, then a real coordinator and real bots. Its first run downloads the model (about 3 GB), so it takes a few minutes. You just need a Google account.
Quick check: did it all make sense?
Lesson 3 test
5 questions, one for each idea in this lesson. Answer them all right to pass. A wrong answer tells you why, and which page to look at again.
maybe production? What does the program do?What the team still can't do.
A long chat
Your bots now pass work to the right teammate. But every bot reads the whole chat before it writes, and after a week of jobs the chat is very long: slow, costly, and easy to get lost in.
Wait, did anyone already post the red frock? I'd have to read 300 messages to find out.