Lesson 21 of 23 · Part 4: Multi-Agent Teams

Code: Assembly Line (Researcher → Writer)

Two agents in a row: Rita finds the facts, then our code hands them to Wally to write up.

Last time: Teams come in four shapes; pick the simplest one that fits how your task behaves.
Today: We code our first team: Rita finds facts, then our code hands them to Wally.
Source Code
1# 1_assembly_line.py
2# Two agents in a row: Rita finds the facts, then Wally writes them up.
3from openai import OpenAI
4client = OpenAI()
5
6# Every agent is the SAME helper: one API call with its own system prompt.
7def run_agent(system_prompt, task):
8    response = client.chat.completions.create(
9        model="gpt-4o-mini",
10        messages=[
11            {"role": "system", "content": system_prompt},
12            {"role": "user", "content": task},
13        ],
14    )
15    return response.choices[0].message.content
16
17# Agent 1: Rita the researcher
18researcher_prompt = "You are Rita, a researcher. List 3 short, true facts about the topic. Facts only."
19
20# Agent 2: Wally the writer
21writer_prompt = "You are Wally, a writer for kids. Turn these facts into a fun 4-sentence paragraph."
22
23topic = "volcanoes"
24
25# Station 1: Rita finds the facts
26print(f"Rita is researching {topic}...")
27facts = run_agent(researcher_prompt, f"Topic: {topic}")
28print("\nRita's facts:")
29print(facts)
30
31# Station 2: our code hands Rita's facts to Wally
32print("\nWally is writing...")
33article = run_agent(writer_prompt, f"Facts:\n{facts}")
34print("\nWally's paragraph:")
35print(article)

No output yet...

← Prev
1/0
Next: Writer & Critic →