← Build an AI Team Lesson 3 of 12

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.

Step through. A customer question only Production can answer.

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.

Write a message. See who replies next.

Try:

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.

Step through. No one uses @. Watch the count.

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

Work each one out. Type the answer, press Check.
The CEO writes: “New frock photos are up. @Marketing @Production take a look.” How many bots reply next?
5 bots take turns in a fixed order. A message needs only Production. How many writes are wasted?
With a coordinator, a job ends with the CEO's task and 6 bot replies in the chat. How many times did the coordinator write?
Same job. How many more LLM writes does the coordinator way take than @mentions?

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.

Step through one message.

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.

Pick the team size and the chat length. Compare.

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

Pick a round limit. Step through.

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

Work each one out. Type the answer, press Check.
“Every bot decides”, with 4 bots. The bots post 5 messages. How many deciding writes?
Two bots keep thanking each other, each @mentioning the other. Round limit 3. How many thank-you replies are posted?
3 bots. The CEO's task, then 10 bot messages. Of the three ways, what is the most deciding writes any of them takes?
The CEO writes “@Operations thanks!”. Then Operations and Marketing keep thanking each other, each naming the other. Round limit 4. How many messages are in the chat at the end, counting the CEO's?

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

Example answers, made for this page. What does the program do with each?

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))

The 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.

1. PredictWith @mentions, Marketing writes: “Caption ready. @Production please check the sizes.” Who replies next?
2. CountWith a coordinator, the chat ends with 8 messages. How many times did the coordinator write?
3. Work it out“Every bot decides”, 5 bots, 4 bot messages. How many deciding writes?
4. WhyTwo bots keep @mentioning each other. What stops them?
5. PredictThe real coordinator answers: 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.

This leaves a question
The chat grows long. How does the team keep track of the work?
Lesson 4 answers it.
For your business: which way would you use to pick who speaks next, and why? Write one message you'd post, with the @mentions in it.