▶ Watch this lesson (starts at 8:03)
Lesson four. Workers in parallel.
import pathlib
import sys
import time
from concurrent.futures import ThreadPoolExecutor
from pydantic import BaseModel
from agent import WEB_SEARCH, Agent, report_cost
WORKSPACE = pathlib.Path("workspace")
NOTES = WORKSPACE / "notes"
class Task(BaseModel):
title: str
brief: str
class Plan(BaseModel):
tasks: list[Task]
planner = Agent("planner", "You plan the research for a small team. Split the goal "
"into 2 to 4 independent tasks that do not overlap. Each brief says "
"exactly what to find and what to bring back.",
model="gpt-6.1-sol", color=35)
def research(task, n):
researcher = Agent(f"researcher {n}", "You research one task on the web. Bring "
"back short bullet points, each with its source URL.",
tools=[WEB_SEARCH])
researcher.log(f"starts: {task.title}")
notes = researcher.run(f"{task.title}\n\n{task.brief}")
(NOTES / f"{n}.md").write_text(f"# {task.title}\n\n{notes}\n")
researcher.log(f"done: notes/{n}.md")
return notesimport pathlib
import sys
import time
from concurrent.futures import ThreadPoolExecutor
from pydantic import BaseModel
from agent import WEB_SEARCH, Agent, report_cost
WORKSPACE = pathlib.Path("workspace")
NOTES = WORKSPACE / "notes"
class Task(BaseModel):
title: str
brief: str
class Plan(BaseModel):
tasks: list[Task]
planner = Agent("planner", "You plan the research for a small team. Split the goal "
"into 2 to 4 independent tasks that do not overlap. Each brief says "
"exactly what to find and what to bring back.",
model="gpt-6.1-sol", color=35)
def research(task, n):
researcher = Agent(f"researcher {n}", "You research one task on the web. Bring "
"back short bullet points, each with its source URL.",
tools=[WEB_SEARCH])
researcher.log(f"starts: {task.title}")
notes = researcher.run(f"{task.title}\n\n{task.brief}")
(NOTES / f"{n}.md").write_text(f"# {task.title}\n\n{notes}\n")
researcher.log(f"done: notes/{n}.md")
return notes
def run_team(goal):
start = time.time()
NOTES.mkdir(parents=True, exist_ok=True)
plan = planner.ask(goal, Plan)
for n, task in enumerate(plan.tasks, 1):
planner.log(f"task {n}: {task.title}")
numbers = range(1, len(plan.tasks) + 1)
with ThreadPoolExecutor() as pool:
notes = list(pool.map(research, plan.tasks, numbers))
print(f"\n{len(notes)} notes in workspace/notes")
report_cost(time.time() - start)
if __name__ == "__main__":
run_team(sys.argv[1])uv run --env-file .env team.py "FastAPI, Django or Flask for a beginner's firstAPI in 2026?"ls workspace/notes
head -n 6 workspace/notes/1.mdSo: the cheap model for the workers, a thread pool so they run at once, and notes saved as files. Now, someone has to write the report.