python --version
pip install openai
export OPENAI_API_KEY=sk-...
Create a supervisor agent that decomposes a task and delegates sub-tasks to specialized worker agents, then aggregates their outputs.
1 from openai import OpenAI 2 3 client = OpenAI() 4 5 def research_agent(topic: str) -> str: 6 resp = client.chat.completions.create( 7 model="gpt-4o-mini", 8 messages=[ 9 {"role": "system", "content": "You are a research agent. Summarize the topic in exactly 3 key bullet points."}, 10 {"role": "user", "content": f"Topic: {topic}"}, 11 ], 12 ) 13 return resp.choices[0].message.content or "" 14 15 def writer_agent(bullet_points: str) -> str: 16 resp = client.chat.completions.create( 17 model="gpt-4o-mini", 18 messages=[ 19 {"role": "system", "content": "You are a writer. Turn bullet points into a clear, flowing paragraph."}, 20 {"role": "user", "content": bullet_points}, 21 ], 22 ) 23 return resp.choices[0].message.content or "" 24 25 def supervisor(task: str) -> str: 26 print(f"[Supervisor] Task: {task}") 27 28 research = research_agent(task) 29 print(f"[Research] Output:\n{research}\n") 30 31 final = writer_agent(research) 32 print(f"[Writer] Output:\n{final}\n") 33 34 return final 35 36 if __name__ == "__main__": 37 result = supervisor("Explain how transformer attention mechanisms work") 38 print("=== Final Result ===") 39 print(result) 40
Sign in to share your feedback and join the discussion.