Max the boss uses other agents as his tools: each helper is a whole LLM call.
1# 3_boss_agent.py - The Boss Agent (its tools are other agents!)
2import json
3from openai import OpenAI
4client = OpenAI()
5
6# Every agent is the same helper: one LLM call with its own job.
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# --- The helpers: each one is a whole agent ---
18def ask_researcher(question):
19 return run_agent("You are Rita, a researcher. Answer with short, true facts.", question)
20
21def ask_math_whiz(problem):
22 return run_agent("You are Milo, a math whiz. Solve it step by step.", problem)
23
24# --- The menu Max sees (same format as Part 2) ---
25tools = [
26 {"type": "function", "function": {
27 "name": "ask_researcher",
28 "description": "Ask Rita the researcher to find facts.",
29 "parameters": {"type": "object", "properties": {
30 "question": {"type": "string"}
31 }, "required": ["question"]}
32 }},
33 {"type": "function", "function": {
34 "name": "ask_math_whiz",
35 "description": "Ask Milo the math whiz to do a calculation.",
36 "parameters": {"type": "object", "properties": {
37 "problem": {"type": "string"}
38 }, "required": ["problem"]}
39 }},
40]
41helpers = {"ask_researcher": ask_researcher, "ask_math_whiz": ask_math_whiz}
42
43# --- Max the boss gets the request ---
44boss_prompt = ("You are Max, the boss. Break the request into parts, ask your helpers "
45 "using the tools, then write the final answer. Don't do the work yourself.")
46request = "How tall is Mount Everest, and how many 30-story buildings (3 m per floor) stacked would reach it?"
47print("You:", request)
48messages = [
49 {"role": "system", "content": boss_prompt},
50 {"role": "user", "content": request},
51]
52
53# --- The boss loop: Max decides, helpers work, repeat ---
54while True:
55 response = client.chat.completions.create(
56 model="gpt-4o-mini", messages=messages, tools=tools
57 )
58 message = response.choices[0].message
59 messages.append(message) # Max's reply goes on his notepad
60
61 if not message.tool_calls: # no order slips = Max is done
62 print("Max:", message.content)
63 break
64
65 for tool_call in message.tool_calls: # one order slip at a time
66 name = tool_call.function.name
67 args = json.loads(tool_call.function.arguments)
68 print(f"Max asks {name}: {args}")
69 result = helpers[name](**args) # the helper agent does the job
70 messages.append({
71 "role": "tool",
72 "tool_call_id": tool_call.id,
73 "content": result,
74 })No output yet...