Skip to content

Latest commit

 

History

History

04-workers-in-parallel

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

README.md

Lesson 4 · Workers in parallel

▶ Watch this lesson (starts at 8:03)

Goal

Lesson four. Workers in parallel.

Commands and files (as shown in the video)

team.py · code research

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 notes

team.py · code pool

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 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])

Terminal · term run

uv run --env-file .env team.py "FastAPI, Django or Flask for a beginner's firstAPI in 2026?"

Terminal · term notes

ls workspace/notes
head -n 6 workspace/notes/1.md

Recap

So: 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.