Teams of agents
One job each
What if I had helpers, each good at one thing?
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
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.
The writer's draft said 40 km. The facts say 25. Who notices?
A checker catches mistakes
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
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.
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."))
$ python lesson6.py
manager gets: Write one sentence about the best beach near Kochi.
researcher gets: Find the best beach near Kochi
researcher answers: Cherai Beach is best: calm water, 25 km from Kochi.
writer gets: Write one sentence: Cherai Beach, calm water, 25 km from Kochi
writer answers: Cherai Beach is the best beach near Kochi: calm, and only 40 km away.
checker gets: Facts: 25 km from Kochi. Draft: calm, and only 40 km away.
checker answers: FIX: the facts say 25 km, not 40 km.
writer gets: Fix it: the beach is 25 km away, not 40 km
writer answers: Cherai Beach is the best beach near Kochi: calm, and only 25 km away.
checker gets: Facts: 25 km from Kochi. Draft: calm, and only 25 km away.
checker answers: OK
manager answers: Cherai Beach is the best beach near Kochi: calm, and only 25 km away.
Cherai Beach is the best beach near Kochi: calm, and only 25 km away.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.
ask_writer?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?