← Build an AI Agent Lesson 6 of 8

Teams of agents

One job each

What if I had helpers, each good at one thing?

Meet the team. Then give each job to the right agent.
researcher — finds facts. Tool: search(words) writer — writes short, friendly text. No tools. checker — compares a draft with the facts. No tools.

Each agent gets its own instructions and its own tools. Its job is small and clear, and its text stays short.
an agent's job = its instructions + its tools
The researcher has search, so finding facts is its job.

Three agents. Who tells them what to do?

A manager hands out work

Step through. Watch the manager ask for help.

To the manager, the other agents are just tools. ACT: ask_writer(…) runs the writer's whole loop, and its answer comes back on a LOOK: line.
to the manager, an agent is a tool

Try it on new numbers.

✏️ Practice 6.1

New numbers, same ideas.
Remember: every agent runs its own loop: times an agent writes = rounds + 1
The manager uses one round each time it asks a worker.

Work each one out. Type the answer, press Check.
The manager asks 3 workers, once each, then answers. How many times does the manager write?
Each worker uses 1 tool, then answers. How many writes do the 3 workers make together?
Same job. How many LLM writes does the whole team make?

The writer's draft said 40 km. The facts say 25. Who notices?

A checker catches mistakes

Step through. The draft has a mistake.

The writer slipped: 40 km, not 25. The checker's only job is to compare the draft with the facts, so it catches the slip. The manager sends the fix back to the writer, then checks again.
write → check → fix, until OK

Better work. But what does a team cost?

More agents, more cost

Same job, one agent or a team. Compare. (Each write takes about 2 s.)
LLM writes
Time

The team's answer was checked, but it took 12 writes instead of 2. More writes take more time and money. Start with one agent, and add a team only when the job needs it.
a team: better on big jobs, but costs more

Try it on new numbers.

✏️ Practice 6.2

New numbers, same ideas.
Remember: write → check → fix, until OK
Every fix is one more ask to the writer. Cost and time grow with every LLM write.

Work each one out. Type the answer, press Check.
The checker says FIX twice, then OK. How many times is the writer asked?
One agent makes 2 LLM writes. The team makes 12. How many times as many writes is that?
Each LLM write costs ₹0.25. What does the team's 12-write job cost, in ₹?

Here's the team in Python.

The code

make_agent makes a new function for each agent and hands it back. That function remembers its own name, instructions, tools and replies. The manager's tools are the other agents. Guess what each print shows. Then press ▶ Run.

import re

# The loop from Lesson 3, but each agent brings its own instructions and tools
def run_agent(instructions, question, tools, llm, step_limit=6):
    text = instructions + "\n\nQuestion: " + question + "\n"
    for step in range(step_limit):
        reply = llm(text)
        text += reply + "\n"
        if "ANSWER:" in reply:
            return reply.split("ANSWER:")[1].strip()
        act = re.search(r"ACT: (\w+)\((.*)\)", reply)
        if act is None or act.group(1) not in tools:
            look = "Error: write ACT: tool(input) with a tool you have"
        else:
            look = tools[act.group(1)](act.group(2))
        text += "LOOK: " + look + "\n"
    return "Stopped: the step limit was reached."

# Make an agent: a name, its instructions, its tools, and its (fake) LLM's replies.
# make_agent makes a new function for each agent and hands it back.
# That function remembers its own name, instructions, tools and replies.
def make_agent(name, instructions, tools, replies):
    def fake_llm(text):        # this agent's fake LLM: hands back its next reply
        return replies.pop(0)
    def agent(task):
        print(f"  {name} gets: {task}")
        answer = run_agent(instructions, task, tools, fake_llm)
        print(f"  {name} answers: {answer}")
        return answer
    return agent

def search(words):
    return "Cherai Beach: calm water, 25 km from Kochi. Fort Kochi Beach: rocky."

researcher = make_agent("researcher", "You find facts. Tool: search(words).", {"search": search}, [
    "ACT: search(beaches near Kochi)",
    "ANSWER: Cherai Beach is best: calm water, 25 km from Kochi.",
])
writer = make_agent("writer", "You write short, friendly text. No tools.", {}, [
    "ANSWER: Cherai Beach is the best beach near Kochi: calm, and only 40 km away.",
    "ANSWER: Cherai Beach is the best beach near Kochi: calm, and only 25 km away.",
])
checker = make_agent("checker", "You check a draft against the facts. Answer OK, or FIX: and what's wrong.", {}, [
    "ANSWER: FIX: the facts say 25 km, not 40 km.",
    "ANSWER: OK",
])

# The manager: its tools are the other agents
manager_replies = [
    "ACT: ask_researcher(Find the best beach near Kochi)",
    "ACT: ask_writer(Write one sentence: Cherai Beach, calm water, 25 km from Kochi)",
    "ACT: ask_checker(Facts: 25 km from Kochi. Draft: calm, and only 40 km away.)",
    "ACT: ask_writer(Fix it: the beach is 25 km away, not 40 km)",
    "ACT: ask_checker(Facts: 25 km from Kochi. Draft: calm, and only 25 km away.)",
    "ANSWER: Cherai Beach is the best beach near Kochi: calm, and only 25 km away.",
]
manager = make_agent("manager", "You lead a team. Tools: ask_researcher, ask_writer, ask_checker.",
                     {"ask_researcher": researcher, "ask_writer": writer, "ask_checker": checker},
                     manager_replies)

print(manager("Write one sentence about the best beach near Kochi."))

The notebook has all of this, then the same team with the real LLM from Lesson 3 playing every agent. Its first run downloads the model (about 3 GB).

Quick check: did it all make sense?

Lesson 6 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. ApplyWhich agent should find today's train times?
2. IdeaTo the manager, what is ask_writer?
3. WhyWhy use a separate checker instead of trusting the writer?
4. Think“What's 2 + 2?” Should a team of 4 agents answer it?
5. CodeIn the code, what are the manager's tools?

What could still go wrong.

On its own

Your agents plan, act and check each other, mostly on their own. Some tools send emails or delete files.

I can do so much now. But who makes sure I don't do something I shouldn't?

This leaves a question
Agents now act on their own. How do we keep them safe?
Lesson 7 answers it.