Lesson 31 of 33 ยท Part 4: Multi-Agent Teams

Code: Agents in Parallel

Rita researches several topics at the same time, so the team finishes in about the time of one job.

Last time: Max the boss handed out jobs with tool calls, and each helper was a whole agent.
Today: Run Rita's research jobs at the same time with ThreadPoolExecutor, and time both races.
Source Code
1# part4/parallel_agents.py
2# Rita researches some topics: first one at a time, then all at once. Which is faster?
3import time
4from concurrent.futures import ThreadPoolExecutor
5from openai import OpenAI
6client = OpenAI()
7
8# Same helper as before: one agent = one API call with its own system prompt.
9def run_agent(system_prompt, task):
10    response = client.chat.completions.create(
11        model="gpt-4o-mini",
12        messages=[
13            {"role": "system", "content": system_prompt},
14            {"role": "user", "content": task},
15        ],
16    )
17    return response.choices[0].message.content
18
19researcher_prompt = "You are Rita, a researcher. Give ONE amazing, true fact about the topic, in one sentence."
20writer_prompt = "You are Wally, a writer for kids. Turn these facts into a fun 4-sentence poster."
21
22topics = ["Mars", "Jupiter", "Saturn"]
23
24# One job for Rita: ONE topic in, ONE fact out
25def research(topic):
26    return run_agent(researcher_prompt, f"Topic: {topic}")
27
28# Race 1: one at a time. Each job waits for its turn.
29start = time.time()  # press the stopwatch
30facts = [research(topic) for topic in topics]
31print(f"One at a time: {time.time() - start:.1f} seconds")
32
33# Race 2: all at once. A team of helpers, one job each.
34# (We research twice on purpose, to compare. That doubles the cost of this part.)
35start = time.time()  # reset the stopwatch
36with ThreadPoolExecutor() as pool:
37    facts = list(pool.map(research, topics))
38print(f"All at once: {time.time() - start:.1f} seconds")
39
40# Wally needs ALL the facts, so he waits until the whole team is done
41fact_list = "\n".join(facts)
42poster = run_agent(writer_prompt, f"Facts:\n{fact_list}")
43print("\nWally's poster:\n" + poster)

No output yet...

โ† Prev
1/0
Next: Shared Memory โ†’