Supervisor Multi-Agent Systems
Data Mastery Series — Episode 58: การพัฒนาระบบ Supervisor Agent เพื่อควบคุมทีม Agent อย่างมีประสิทธิภาพ
Supervisor Multi-Agent Systems
Data Mastery Series — Episode 58: การพัฒนาระบบ Supervisor Agent เพื่อควบคุมทีม Agent อย่างมีประสิทธิภาพ

📌 Connect with me and follow our journey: Linkedin, Facebook
ในซีรีส์ Data Mastery เราได้สำรวจศักยภาพของ LangGraph ผ่านหลายตอนที่ผ่านมา ไม่ว่าจะเป็น LangGraph Introduction, Agentic RAG, Adaptive RAG, Corrective RAG, Self RAG, SQL RAG, Prompt Template Agent, และ Network Multi-Agent Systems
สำหรับ EP.58 นี้ เราจะพาไปรู้จัก Supervisor Multi-Agent Systems ระบบที่มี Supervisor Agent ทำหน้าที่บริหารจัดการการทำงานของ Agent หลายตัวในทีมอย่างเป็นระบบ
🧩 แนวคิดของ Supervisor Multi-Agent Systems
ในการแก้ปัญหาที่มีความซับซ้อน เช่น การตอบคำถามที่ต้องการความรู้หลายด้าน การสรุปข้อมูล หรือการวิเคราะห์ข้อมูลเชิงตัวเลข เราไม่เพียงต้องการ Agent หลายตัวเท่านั้น แต่ยังต้องการระบบที่สามารถจัดการการทำงานร่วมกันได้อย่างมีประสิทธิภาพ
แนวคิดของ Supervisor คือ:
- 👥 จัดสรรให้ Agent แต่ละตัวมีความเชี่ยวชาญเฉพาะด้าน เช่น การค้นคว้าข้อมูล การคำนวณ หรือการเขียนโค้ด
- 🧠 มี Supervisor Agent ทำหน้าที่มองภาพรวม และตัดสินใจว่า Agent ตัวใดควรทำงานในแต่ละขั้นตอน
- ✅ กำหนดเงื่อนไขการสิ้นสุดกระบวนการทำงานของ Workflow
✨ กรณีศึกษานี้อ้างอิงจากเอกสารทางการของ LangGraph ในหัวข้อ Multi-Agent Systems → Supervisor: Route based on model choice
🔁 Workflow Graph & Function ที่เกี่ยวข้อง

Figure: Supervisor Multi-Agent Systems Workflow
ในระบบนี้ เราออกแบบเครือข่าย Agent 2 ตัวคือ researcherและ coder โดยใช้ LangGraph ให้แต่ละ Agent ทำงานตามบทบาทของตนเอง โดยมี Supervisor เป็นผู้ควบคุมการทำงาน:
✅ supervisor_node(state)
- หน้าที่: ควบคุมการทำงานของ agent ทั้งหมดในระบบ
- วิเคราะห์ข้อความในประวัติการสนทนา (conversation history) เพื่อตัดสินใจว่าจะส่งงานต่อไปยังresearcher,coderหรือFINISH - 🛠 ใช้เครื่องมือ:
-ChatOpenAI(model="gpt-4o")- system_prompt สำหรับกำหนดกรอบการทำงานให้กับ LLM
✅ research_node(state)
- หน้าที่: ค้นคว้าและรวบรวมข้อมูลจากอินเทอร์เน็ตตามโจทย์ที่ได้รับ
- เน้นการค้นหาข้อมูลเท่านั้น ไม่ทำการประมวลผลหรือคำนวณใด ๆ
- เมื่อได้ข้อมูลแล้ว จะส่งข้อความกลับไปยังsupervisorเพื่อตัดสินใจในขั้นตอนถัดไป - 🛠 ใช้เครื่องมือ:
-tavily_toolสำหรับค้นข้อมูลจากแหล่งออนไลน์
-create_react_agent(...)ที่ถูกกำหนดให้ทำงานเฉพาะการค้นข้อมูล
✅ code_node(state)
- หน้าที่: เขียนและรันโค้ด Python ตามความต้องการของโจทย์
- ทำงานกับข้อมูลที่ต้องการการคำนวณหรือวิเคราะห์เชิงคณิตศาสตร์
- ประมวลผลผ่านpython_repl_toolและส่งผลลัพธ์กลับไปยังsupervisor - 🛠 ใช้เครื่องมือ:
-python_repl_toolสำหรับรัน Python code ใน sandbox จำลอง (ต้องระวังความปลอดภัย)
-create_react_agent(...)ที่เน้นการทำงานกับการเขียนและรันโค้ด
🔍 ตัวอย่างที่ 1: การคำนวณพื้นฐาน
"""
User question: What's the square root of 42?
AI Route and Decision:
[DEBUG] output = ((), {'supervisor': None})
step 1: ---SUPERVISOR---
output from step 1: Supervisor decided nothing (None)
[DEBUG] output = (('coder:fb52bc3b-6d7f-3758-9a38-00f28b133189',), {'agent': {'messages': [AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_gKYfSaH2HQZf0OAxXQj4kH4g', 'function': {'arguments': '{"code":"import math\nmath.sqrt(42)"}', 'name': 'python_repl_tool'}, 'type': 'function'}], 'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 24, 'prompt_tokens': 96, 'total_tokens': 120, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 0}}, 'model_name': 'gpt-4o-2024-08-06', 'system_fingerprint': 'fp_a6889ffe71', 'id': 'chatcmpl-BQnd6kurFF2tFJnlyhG6wNTRqaorS', 'finish_reason': 'tool_calls', 'logprobs': None}, id='run-d2ac6a77-f533-4a8e-b673-25f2f44f74c2-0', tool_calls=[{'name': 'python_repl_tool', 'args': {'code': 'import math\nmath.sqrt(42)'}, 'id': 'call_gKYfSaH2HQZf0OAxXQj4kH4g', 'type': 'tool_call'}], usage_metadata={'input_tokens': 96, 'output_tokens': 24, 'total_tokens': 120, 'input_token_details': {'audio': 0, 'cache_read': 0}, 'output_token_details': {'audio': 0, 'reasoning': 0}})]}})
step 2: ---CODER---
[WARNING] cannot parse coder reply: 'coder'
[DEBUG] output = (('coder:fb52bc3b-6d7f-3758-9a38-00f28b133189',), {'tools': {'messages': [ToolMessage(content='Successfully executed:\n\\\\python\nimport math\nmath.sqrt(42)\n\\\\\\nStdout: ', name='python_repl_tool', id='fd82765d-5c97-4afd-b52f-d3628962c14d', tool_call_id='call_gKYfSaH2HQZf0OAxXQj4kH4g')]}})
step 3: ---CODER---
[WARNING] cannot parse coder reply: 'coder'
[DEBUG] output = (('coder:fb52bc3b-6d7f-3758-9a38-00f28b133189',), {'agent': {'messages': [AIMessage(content='The square root of 42 is approximately 6.4807.', additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 16, 'prompt_tokens': 154, 'total_tokens': 170, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 0}}, 'model_name': 'gpt-4o-2024-08-06', 'system_fingerprint': 'fp_a6889ffe71', 'id': 'chatcmpl-BQnd7IeyDfZSTqipVxnBRDUh53JUp', 'finish_reason': 'stop', 'logprobs': None}, id='run-777eafc7-19e7-4e4a-a4da-87d313e2f247-0', usage_metadata={'input_tokens': 154, 'output_tokens': 16, 'total_tokens': 170, 'input_token_details': {'audio': 0, 'cache_read': 0}, 'output_token_details': {'audio': 0, 'reasoning': 0}})]}})
step 4: ---CODER---
[WARNING] cannot parse coder reply: 'coder'
[DEBUG] output = ((), {'coder': {'messages': [HumanMessage(content='The square root of 42 is approximately 6.4807.', additional_kwargs={}, response_metadata={}, name='coder')]}})
step 5: ---CODER---
output from step 5: Coder reply
'The square root of 42 is approximately 6.4807.'
[DEBUG] output = ((), {'supervisor': None})
step 6: ---SUPERVISOR---
output from step 6: Supervisor decided nothing (None)
##################################################
Summary
- User question: What's the square root of 42?
- AI Answer: The square root of 42 is approximately 6.4807.
- Route: start --> supervisor --> coder --> coder --> coder --> coder --> supervisor --> end
##################################################
"""
ลองมาดูตัวอย่างการทำงานของระบบที่ใช้ LangGraph เพื่อให้ Supervisor Agent เลือกส่งงานให้ Coder อย่างเป็นขั้นตอน
- 🧑💼 คำถามจากผู้ใช้:
What's the square root of 42? - 📤 คำตอบจาก Generative AI:
The square root of 42 is approximately 6.4807 - 📍Route:
start → supervisor → coder → coder → coder → coder → supervisor → end
✅ Step 1: supervisor – ประเมินว่าต้องใช้ Agent ตัวไหน
- หน้าที่: ประเมินคำถามและตัดสินใจส่งต่อให้ coder
- Output:
supervisorประเมินคำถาม → เลือกcoderเป็นผู้ดำเนินการ - Route:
start→supervisor
✅ Step 2: coder – coder สั่งรัน Tool
- หน้าที่: Coder สร้างคำสั่ง Python เพื่อหาค่า square root ของ 42 (
import math; math.sqrt(42) - Output: ตัดสินใจใช้
python_repl_tool - Route:
supervisor→coder
✅ Step 3: coder – รับผลลัพธ์จาก tool
- หน้าที่: Tool ทำงานเสร็จ → ส่งค่ากลับมา (
6.48074069840786) - Output: ข้อมูลดิบจาก stdout ของ tool (รันสำเร็จ แต่ยังไม่ได้สรุปผลให้ user)
- Route:
coder→coder
✅ Step 4: coder – สรุปคำตอบ
- หน้าที่: Coder นำผลลัพธ์ (
6.4807) มาเขียนสรุปเป็นข้อความภาษาคน - Output: The square root of 42 is approximately 6.4807.
- Route:
coder→coder
✅ Step 5: coder – ส่งข้อความสรุปสุดท้ายกลับไปยังระบบ
- หน้าที่: จัดข้อความในรูปแบบที่ system ใช้ได้ (
HumanMessage) เพื่อส่งกลับไปยังระบบ supervisor - Output: ส่งคำตอบให้ user ผ่านระบบ
- Route:
coder→coder
✅ Step 6: supervisor – ตรวจสอบการทำงานอีกครั้งก่อนปิดงาน
- หน้าที่: Supervisor เช็กสถานะอีกครั้ง แต่ไม่ต้องทำอะไรเพิ่ม เพราะได้คำตอบแล้ว
- Output: ไม่มี action เพิ่มเติม → ระบบไปที่ end
- Route:
supervisor→end
🔍 ตัวอย่างที่ 2: การค้นข้อมูลและคำนวณ
"""
User question: Find the latest GDP of New York and California, then calculate the average
AI Route and Decision:
[DEBUG] output = ((), {'supervisor': None})
step 1: ---SUPERVISOR---
output from step 1: Supervisor decided nothing (None)
[DEBUG] output = (('researcher:cfcbdf87-9d31-daf2-b3a4-b50114946de3',), {'agent': {'messages': [AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_vjnB1D3E01pMaduEs7h0uAiO', 'function': {'arguments': '{"query": "latest GDP of New York 2023"}', 'name': 'tavily_search_results_json'}, 'type': 'function'}, {'id': 'call_ublHBXn41i1GLYtM7FyIxvQs', 'function': {'arguments': '{"query": "latest GDP of California 2023"}', 'name': 'tavily_search_results_json'}, 'type': 'function'}], 'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 66, 'prompt_tokens': 104, 'total_tokens': 170, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 0}}, 'model_name': 'gpt-4o-2024-08-06', 'system_fingerprint': 'fp_55d88aaf2f', 'id': 'chatcmpl-BQndGpGDMjidHAZqONceKWoxiyq9L', 'finish_reason': 'tool_calls', 'logprobs': None}, id='run-7f84a1f8-bff1-43b8-8dee-175e778f8bfa-0', tool_calls=[{'name': 'tavily_search_results_json', 'args': {'query': 'latest GDP of New York 2023'}, 'id': 'call_vjnB1D3E01pMaduEs7h0uAiO', 'type': 'tool_call'}, {'name': 'tavily_search_results_json', 'args': {'query': 'latest GDP of California 2023'}, 'id': 'call_ublHBXn41i1GLYtM7FyIxvQs', 'type': 'tool_call'}], usage_metadata={'input_tokens': 104, 'output_tokens': 66, 'total_tokens': 170, 'input_token_details': {'audio': 0, 'cache_read': 0}, 'output_token_details': {'audio': 0, 'reasoning': 0}})]}})
step 2: ---RESEARCHER---
[WARNING] cannot parse researcher reply: 'researcher'
[DEBUG] output = (('researcher:cfcbdf87-9d31-daf2-b3a4-b50114946de3',), {'tools': {'messages': [ToolMessage(content='[{"title": "Real GDP New York U.S. 2023 - Statista", "url": "https://www.statista.com/statistics/188087/gdp-of-the-us-federal-state-of-new-york-since-1997/", "content": "In 2023, the real gross domestic product (GDP) of New York was about 1.78 trillion U.S. dollars. This is a slight increase from the previous", "score": 0.8785752}, {"title": "What is the gross domestic product (GDP) in New York? - USAFacts", "url": "https://usafacts.org/answers/what-is-the-gross-domestic-product-gdp/state/new-york/", "content": "The GDP starts at approximately $1.1 trillion in 1997 and reaches $1.8 trillion in 2023, which is the last value on the chart. Source. The Bureau of Economic", "score": 0.8455478}, {"title": "New York state ranks among slowest economies in U.S. | Economics", "url": "https://www.romesentinel.com/townnews/economics/new-york-gdp-growth-2023-trends-decline/article_ba9cbc6a-42b5-4275-90fd-6a2eb695dce1.html", "content": "New York's overall share of the U.S. economy is still very high, but its 7.9% in 2023 was down slightly from 8% the year prior. The state’s population loss of more than 100,000 people is also worth noting, since fewer people means a smaller labor pool, and eventually, fewer spenders. Not great when buyers set the wheel in motion.\n\nHere's a breakdown of the numbers:\n\nGDP growth in state, 2022-2023: +1.5% (+$26.1 billion)\n\nState GDP in 2023: $1.8 trillion (7.9% of US economy) [...] Log In Using Your Account\n\n\n\n\n\nIs New York losing its edge? State ranks among slowest economies\n\nWill Phillips\n\nStaff writer\n\nWhile the U.S. economy continues to outpace other high-income countries with a solid 2.9% GDP increase in 2023, New York isn’t exactly spearheading the mission. According to the latest numbers from the Bureau of Economic Analysis, New York’s economy was below the national average, and the No. 8 slowest grower overall. [...] State GDP in 2022: $1.8 trillion (8.0% of US economy)\n\nChange in personal income, 2022-2023: +5.7% (+$86.5 billion)\n\nPopulation change, 2022-2023: -0.54% (-105,935 people)\n\nPersonal income per capita: $82,323 in 2023, $77,486 in 2022\n\nLargest driver of economic growth in mid-2024: Retail trade (+0.73 ppts. to GDP from Q2 to Q3 2024)\n\nLargest drag on economic growth in mid-2024: Finance and insurance (-0.26 ppts. to GDP from Q2 to Q3 2024)", "score": 0.8338803}, {"title": "GDP by industry New York U.S. 2023 - Statista", "url": "https://www.statista.com/statistics/304883/new-york-real-gdp-by-industry/", "content": "... New York was 82,323 current U.S. dollars. Read more. Real value added to the gross domestic product of New York in the United States in 2023", "score": 0.8332299}, {"title": "Economy of New York (state) - Wikipedia", "url": "https://en.wikipedia.org/wiki/Economy_of_New_York_(state)", "content": "New York City, the economic capital of New York (state) andthe world\nStatistics\nGDP | $2.297 trillion (2024)[1]\nGDP per capita | $115,619 (2024)[2]\nPopulation belowpoverty line | 13.6%[3]\nGini coefficient | 0.5157 ± 0.0029 (2023)[4]\nLabour force | 9,645,984 (2023)[5]\nUnemployment | 4.3% (Feb. 2025)[6]\nPublic finances\nRevenues | $63.5 billion[7]\nExpenses | $54.6 billion[8] [...] The economy of the State of New York is reflected in its gross state product in 2024 of $2.297 trillion, ranking third in size behind the larger states of California and Texas. If New York State were an independent nation, it would rank as the 8th largest economy in the world by nominal GDP. However, in 2024, the multi-state, New York City-centered metropolitan statistical area produced a gross metropolitan product (GMP) over $US2.6 trillion, ranking first nationally by a wide margin and also [...] 49 | 475 | * | ABM Industries | New York | 127,000 | $7,807 | Diversified Outsourcing Services | 4th\n50 | 496 | * | KKR | New York | 4,150 | $7,273 | Securities | 9th\n* = not amongFortune's Global 500Sources:Fortune, Volume 183, Number 3 (June/July 2023) and Volume 184, Number 1 (August/September 2023);Fortunewebsite;50pros.com\nSee also", "score": 0.7971311}]', name='tavily_search_results_json', id='15ca5336-406e-4434-bf24-e38924ffe681', tool_call_id='call_vjnB1D3E01pMaduEs7h0uAiO', artifact={'query': 'latest GDP of New York 2023', 'follow_up_questions': None, 'answer': None, 'images': [], 'results': [{'url': 'https://www.statista.com/statistics/188087/gdp-of-the-us-federal-state-of-new-york-since-1997/', 'title': 'Real GDP New York U.S. 2023 - Statista', 'content': 'In 2023, the real gross domestic product (GDP) of New York was about 1.78 trillion U.S. dollars. This is a slight increase from the previous', 'score': 0.8785752, 'raw_content': None}, {'url': 'https://usafacts.org/answers/what-is-the-gross-domestic-product-gdp/state/new-york/', 'title': 'What is the gross domestic product (GDP) in New York? - USAFacts', 'content': 'The GDP starts at approximately $1.1 trillion in 1997 and reaches $1.8 trillion in 2023, which is the last value on the chart. Source. The Bureau of Economic', 'score': 0.8455478, 'raw_content': None}, {'url': 'https://www.romesentinel.com/townnews/economics/new-york-gdp-growth-2023-trends-decline/article_ba9cbc6a-42b5-4275-90fd-6a2eb695dce1.html', 'title': 'New York state ranks among slowest economies in U.S. | Economics', 'content': "New York's overall share of the U.S. economy is still very high, but its 7.9% in 2023 was down slightly from 8% the year prior. The state’s population loss of more than 100,000 people is also worth noting, since fewer people means a smaller labor pool, and eventually, fewer spenders. Not great when buyers set the wheel in motion.\n\nHere's a breakdown of the numbers:\n\nGDP growth in state, 2022-2023: +1.5% (+$26.1 billion)\n\nState GDP in 2023: $1.8 trillion (7.9% of US economy) [...] Log In Using Your Account\n\n\n\n\n\nIs New York losing its edge? State ranks among slowest economies\n\nWill Phillips\n\nStaff writer\n\nWhile the U.S. economy continues to outpace other high-income countries with a solid 2.9% GDP increase in 2023, New York isn’t exactly spearheading the mission. According to the latest numbers from the Bureau of Economic Analysis, New York’s economy was below the national average, and the No. 8 slowest grower overall. [...] State GDP in 2022: $1.8 trillion (8.0% of US economy)\n\nChange in personal income, 2022-2023: +5.7% (+$86.5 billion)\n\nPopulation change, 2022-2023: -0.54% (-105,935 people)\n\nPersonal income per capita: $82,323 in 2023, $77,486 in 2022\n\nLargest driver of economic growth in mid-2024: Retail trade (+0.73 ppts. to GDP from Q2 to Q3 2024)\n\nLargest drag on economic growth in mid-2024: Finance and insurance (-0.26 ppts. to GDP from Q2 to Q3 2024)", 'score': 0.8338803, 'raw_content': None}, {'url': 'https://www.statista.com/statistics/304883/new-york-real-gdp-by-industry/', 'title': 'GDP by industry New York U.S. 2023 - Statista', 'content': '... New York was 82,323 current U.S. dollars. Read more. Real value added to the gross domestic product of New York in the United States in 2023', 'score': 0.8332299, 'raw_content': None}, {'url': 'https://en.wikipedia.org/wiki/Economy_of_New_York_(state)', 'title': 'Economy of New York (state) - Wikipedia', 'content': "New York City, the economic capital of New York (state) andthe world\nStatistics\nGDP | $2.297 trillion (2024)[1]\nGDP per capita | $115,619 (2024)[2]\nPopulation belowpoverty line | 13.6%[3]\nGini coefficient | 0.5157 ± 0.0029 (2023)[4]\nLabour force | 9,645,984 (2023)[5]\nUnemployment | 4.3% (Feb. 2025)[6]\nPublic finances\nRevenues | $63.5 billion[7]\nExpenses | $54.6 billion[8] [...] The economy of the State of New York is reflected in its gross state product in 2024 of $2.297 trillion, ranking third in size behind the larger states of California and Texas. If New York State were an independent nation, it would rank as the 8th largest economy in the world by nominal GDP. However, in 2024, the multi-state, New York City-centered metropolitan statistical area produced a gross metropolitan product (GMP) over $US2.6 trillion, ranking first nationally by a wide margin and also [...] 49 | 475 | * | ABM Industries | New York | 127,000 | $7,807 | Diversified Outsourcing Services | 4th\n50 | 496 | * | KKR | New York | 4,150 | $7,273 | Securities | 9th\n* = not amongFortune's Global 500Sources:Fortune, Volume 183, Number 3 (June/July 2023) and Volume 184, Number 1 (August/September 2023);Fortunewebsite;50pros.com\nSee also", 'score': 0.7971311, 'raw_content': None}], 'response_time': 3.24}), ToolMessage(content='[{"title": "Real GDP California U.S. 2023 - Statista", "url": "https://www.statista.com/statistics/187834/gdp-of-the-us-federal-state-of-california-since-1997/", "content": "In 2023, the real gross domestic product (GDP) of California was 3.23 trillion U.S. dollars. This is a slight increase from the previous year,", "score": 0.8886914}, {"title": "Gross domestic product by state U.S. 2023 - Statista", "url": "https://www.statista.com/statistics/248023/us-gross-domestic-product-gdp-by-state/", "content": "The gross domestic product (GDP) of California was about 3.23 trillion US dollars in 2023, meaning that it contributed the most out of any state to the country", "score": 0.8849276}, {"title": "[PDF] California's Economy", "url": "https://www.ppic.org/wp-content/uploads/californias-economy.pdf", "content": "⊲ In 2023, California's gross domestic product (GDP) was about $3.9 trillion, comprising 14% of national GDP. ($27.7 trillion). Texas and New York are the", "score": 0.85421735}, {"title": "California's economy leads the nation | Governor of California - CA.gov", "url": "https://www.gov.ca.gov/2024/07/15/californias-economy-leads-the-nation/", "content": "California is the 5th largest economy in the world for the seventh consecutive year, with a nominal GDP of nearly $3.9 trillion in 2023 and a growth rate of 6.1% since the year prior, according to the U.S. Bureau of Economic Analysis. On a per capita basis, California is the second largest economy in the world among large economies.\n\nHighest level of tourism spending [...] California has the largest market share of tourism in the nation. Travel spending in the state reached an all-time high of $150.4 billion last year, surpassing the record $144.9 billion spent in 2019 – spending that is 3.8% higher than 2019 and 5.6% higher than 2022.\n\nThe new travel-spending record generated $12.7 billion in state and local tax revenue by visitors in 2023, marking a 3% increase over 2019. Tourism created 64,900 new jobs in 2023, bringing total industry employment to 1,155,000.", "score": 0.84767824}, {"title": "What is the gross domestic product (GDP) in California? - USAFacts", "url": "https://usafacts.org/answers/what-is-the-gross-domestic-product-gdp/state/california/", "content": "Real GDP increased from 2023 to reach $3.4 trillion in 2024. Real GDP in California, adjusted for inflation (chained 2017 dollars)", "score": 0.7596005}]', name='tavily_search_results_json', id='52bda191-f050-405d-bb0e-856807cd27ec', tool_call_id='call_ublHBXn41i1GLYtM7FyIxvQs', artifact={'query': 'latest GDP of California 2023', 'follow_up_questions': None, 'answer': None, 'images': [], 'results': [{'url': 'https://www.statista.com/statistics/187834/gdp-of-the-us-federal-state-of-california-since-1997/', 'title': 'Real GDP California U.S. 2023 - Statista', 'content': 'In 2023, the real gross domestic product (GDP) of California was 3.23 trillion U.S. dollars. This is a slight increase from the previous year,', 'score': 0.8886914, 'raw_content': None}, {'url': 'https://www.statista.com/statistics/248023/us-gross-domestic-product-gdp-by-state/', 'title': 'Gross domestic product by state U.S. 2023 - Statista', 'content': 'The gross domestic product (GDP) of California was about 3.23 trillion US dollars in 2023, meaning that it contributed the most out of any state to the country', 'score': 0.8849276, 'raw_content': None}, {'url': 'https://www.ppic.org/wp-content/uploads/californias-economy.pdf', 'title': "[PDF] California's Economy", 'content': "⊲ In 2023, California's gross domestic product (GDP) was about $3.9 trillion, comprising 14% of national GDP. ($27.7 trillion). Texas and New York are the", 'score': 0.85421735, 'raw_content': None}, {'url': 'https://www.gov.ca.gov/2024/07/15/californias-economy-leads-the-nation/', 'title': "California's economy leads the nation | Governor of California - CA.gov", 'content': 'California is the 5th largest economy in the world for the seventh consecutive year, with a nominal GDP of nearly $3.9 trillion in 2023 and a growth rate of 6.1% since the year prior, according to the U.S. Bureau of Economic Analysis. On a per capita basis, California is the second largest economy in the world among large economies.\n\nHighest level of tourism spending [...] California has the largest market share of tourism in the nation. Travel spending in the state reached an all-time high of $150.4 billion last year, surpassing the record $144.9 billion spent in 2019 – spending that is 3.8% higher than 2019 and 5.6% higher than 2022.\n\nThe new travel-spending record generated $12.7 billion in state and local tax revenue by visitors in 2023, marking a 3% increase over 2019. Tourism created 64,900 new jobs in 2023, bringing total industry employment to 1,155,000.', 'score': 0.84767824, 'raw_content': None}, {'url': 'https://usafacts.org/answers/what-is-the-gross-domestic-product-gdp/state/california/', 'title': 'What is the gross domestic product (GDP) in California? - USAFacts', 'content': 'Real GDP increased from 2023 to reach $3.4 trillion in 2024. Real GDP in California, adjusted for inflation (chained 2017 dollars)', 'score': 0.7596005, 'raw_content': None}], 'response_time': 3.22})]}})
step 3: ---RESEARCHER---
[WARNING] cannot parse researcher reply: 'researcher'
[DEBUG] output = (('researcher:cfcbdf87-9d31-daf2-b3a4-b50114946de3',), {'agent': {'messages': [AIMessage(content='The latest GDP for New York in 2023 is approximately $1.8 trillion, and for California, it is approximately $3.23 trillion. To find the average GDP of these two states, you would add the two GDPs together and then divide by two.', additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 57, 'prompt_tokens': 2092, 'total_tokens': 2149, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 0}}, 'model_name': 'gpt-4o-2024-08-06', 'system_fingerprint': 'fp_55d88aaf2f', 'id': 'chatcmpl-BQndKl474c0Sd0V39AkfHd3RKh9Iw', 'finish_reason': 'stop', 'logprobs': None}, id='run-ca7edab4-04e2-4f03-a3a6-d76191c03cda-0', usage_metadata={'input_tokens': 2092, 'output_tokens': 57, 'total_tokens': 2149, 'input_token_details': {'audio': 0, 'cache_read': 0}, 'output_token_details': {'audio': 0, 'reasoning': 0}})]}})
step 4: ---RESEARCHER---
[WARNING] cannot parse researcher reply: 'researcher'
[DEBUG] output = ((), {'researcher': {'messages': [HumanMessage(content='The latest GDP for New York in 2023 is approximately $1.8 trillion, and for California, it is approximately $3.23 trillion. To find the average GDP of these two states, you would add the two GDPs together and then divide by two.', additional_kwargs={}, response_metadata={}, name='researcher')]}})
step 5: ---RESEARCHER---
output from step 5: Researcher reply
('The latest GDP for New York in 2023 is approximately $1.8 trillion, and for '
'California, it is approximately $3.23 trillion. To find the average GDP of '
'these two states, you would add the two GDPs together and then divide by '
'two.')
[DEBUG] output = ((), {'supervisor': None})
step 6: ---SUPERVISOR---
output from step 6: Supervisor decided nothing (None)
[DEBUG] output = (('coder:bf86fd1d-2023-41ea-977a-af8b5e84d5f6',), {'agent': {'messages': [AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_9musgIkNa65mP6K5kt4jcrom', 'function': {'arguments': '{"code":"# Given GDP values for New York and California\nny_gdp = 1.8 # in trillion dollars\nca_gdp = 3.23 # in trillion dollars\n\n# Calculate the average GDP\naverage_gdp = (ny_gdp + ca_gdp) / 2\naverage_gdp"}', 'name': 'python_repl_tool'}, 'type': 'function'}], 'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 81, 'prompt_tokens': 164, 'total_tokens': 245, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 0}}, 'model_name': 'gpt-4o-2024-08-06', 'system_fingerprint': 'fp_a6889ffe71', 'id': 'chatcmpl-BQndM1GF5wfOPc65SvE8lfs3ZRMSf', 'finish_reason': 'tool_calls', 'logprobs': None}, id='run-f26ad470-a4f8-4693-978c-d73340aaee72-0', tool_calls=[{'name': 'python_repl_tool', 'args': {'code': '# Given GDP values for New York and California\nny_gdp = 1.8 # in trillion dollars\nca_gdp = 3.23 # in trillion dollars\n\n# Calculate the average GDP\naverage_gdp = (ny_gdp + ca_gdp) / 2\naverage_gdp'}, 'id': 'call_9musgIkNa65mP6K5kt4jcrom', 'type': 'tool_call'}], usage_metadata={'input_tokens': 164, 'output_tokens': 81, 'total_tokens': 245, 'input_token_details': {'audio': 0, 'cache_read': 0}, 'output_token_details': {'audio': 0, 'reasoning': 0}})]}})
step 7: ---CODER---
[WARNING] cannot parse coder reply: 'coder'
[DEBUG] output = (('coder:bf86fd1d-2023-41ea-977a-af8b5e84d5f6',), {'tools': {'messages': [ToolMessage(content='Successfully executed:\n\\\\python\n# Given GDP values for New York and California\nny_gdp = 1.8 # in trillion dollars\nca_gdp = 3.23 # in trillion dollars\n\n# Calculate the average GDP\naverage_gdp = (ny_gdp + ca_gdp) / 2\naverage_gdp\n\\\\\\nStdout: ', name='python_repl_tool', id='e766f52f-86f8-4592-89e4-bda744b2047d', tool_call_id='call_9musgIkNa65mP6K5kt4jcrom')]}})
step 8: ---CODER---
[WARNING] cannot parse coder reply: 'coder'
[DEBUG] output = (('coder:bf86fd1d-2023-41ea-977a-af8b5e84d5f6',), {'agent': {'messages': [AIMessage(content='The average GDP of New York and California is approximately $2.515 trillion.', additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 18, 'prompt_tokens': 336, 'total_tokens': 354, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 0}}, 'model_name': 'gpt-4o-2024-08-06', 'system_fingerprint': 'fp_a6889ffe71', 'id': 'chatcmpl-BQndNAD2E2UmtYiZnqunTiDEyeRUt', 'finish_reason': 'stop', 'logprobs': None}, id='run-c2e7758c-c64b-4427-ad07-5331160db5ad-0', usage_metadata={'input_tokens': 336, 'output_tokens': 18, 'total_tokens': 354, 'input_token_details': {'audio': 0, 'cache_read': 0}, 'output_token_details': {'audio': 0, 'reasoning': 0}})]}})
step 9: ---CODER---
[WARNING] cannot parse coder reply: 'coder'
[DEBUG] output = ((), {'coder': {'messages': [HumanMessage(content='The average GDP of New York and California is approximately $2.515 trillion.', additional_kwargs={}, response_metadata={}, name='coder')]}})
step 10: ---CODER---
output from step 10: Coder reply
'The average GDP of New York and California is approximately $2.515 trillion.'
[DEBUG] output = ((), {'supervisor': None})
step 11: ---SUPERVISOR---
output from step 11: Supervisor decided nothing (None)
##################################################
Summary
- User question: Find the latest GDP of New York and California, then calculate the average
- AI Answer: The average GDP of New York and California is approximately $2.515 trillion.
- Route: start --> supervisor --> researcher --> researcher --> researcher --> researcher --> supervisor --> coder --> coder --> coder --> coder --> supervisor --> end
##################################################
ลองมาดูตัวอย่างการทำงานของระบบที่ใช้ LangGraph เพื่อให้ 3 Agent (Supervisor + Researcher + Coder) ทำงานร่วมกันอย่างเป็นขั้นตอน
The average GDP of New York and California is approximately $2.515 trillion.
- 🧑💼 คำถามจากผู้ใช้:
Find the latest GDP of New York and California, then calculate the average - 📤 คำตอบจาก Generative AI:
The average GDP of New York and California is approximately $2.515 trillion - 📍Route:
start → supervisor → researcher → researcher → researcher → researcher → supervisor → coder → coder → coder → coder → supervisor → end
✅ Step 1: supervisor – ประเมินว่าต้องใช้ Agent ตัวไหน
- หน้าที่: Supervisor รับคำถามเข้ามา แต่ไม่ได้ตัดสินใจทำอะไรพิเศษเอง
- Output: ส่งคำถามไปให้ Researcher เป็นผู้ดำเนินการต่อ
- Route:
start→researcher
✅ Step 2–5: researcher – ค้นหาข้อมูล GDP ของ New York และ California
- Step 2: Researcher สร้าง 2 คำสั่งค้นหาข้อมูลจากเว็บ (ใช้
tavily_search_results_json) — หาค่า GDP ของ New York และ California ปี 2023 - Step 3: ได้ผลลัพธ์ GDP ของ New York ≈ 1.8 trillion USD
- Step 4: ได้ผลลัพธ์ GDP ของ California ≈ 3.23 trillion USD
- Step 5: สรุปข้อมูลทั้งสอง
- Route:
researcher→researcher→researcher→researcher
✅ Step 6: supervisor – ประเมินสถานะอีกครั้ง
- หน้าที่: Supervisor รอรับผลจาก Researcher → เห็นว่ายังต้องมีการ “คำนวณเฉลี่ย” ต่อ
- Output: ส่งงานต่อให้ Coder ทำการคำนวณ
- Route:
supervisor→coder
✅ Step 7–10: coder – คำนวณค่าเฉลี่ย GDP
- Step 7: Coder เขียน Python code เพื่อคำนวณค่าเฉลี่ย
ny_gdp = 1.8
ca_gdp = 3.23
average_gdp = (ny_gdp + ca_gdp) / 2
- Step 8: ส่ง code ไปรันด้วย
python_repl_tool - Step 9: รับผลลัพธ์จาก tool (ได้ค่าเฉลี่ย ≈ 2.515 trillion USD)
- Step 10: สรุปผลลัพธ์เป็นข้อความภาษาคนสำหรับส่งกลับผู้ใช้
- Route:
coder→coder→coder→coder
✅ Step 11: supervisor – ปิดงาน
- หน้าที่: Supervisor ตรวจสอบว่าคำตอบเสร็จสมบูรณ์แล้ว → ปิด workflow
- Route: supervisor → end
Supervisor Multi-Agent Systems นับเป็นก้าวสำคัญในการพัฒนาระบบ AI ที่มีความสามารถสูงขึ้น:
- ควบคุมลำดับการทำงานของ Agent หลายตัวได้อย่างยืดหยุ่น
- กระจายงานให้เหมาะสมกับความเชี่ยวชาญของแต่ละ Agent
- เพิ่มประสิทธิภาพในการรับมือกับโจทย์ที่มีความซับซ้อน
- เป็นพื้นฐานสำหรับการพัฒนาระบบ Team AI หรือ Multi-Agent Collaboration ในอนาคต
ในตอนต่อไป EP.59 เราจะพาไปสำรวจ Hierarchical Multi-Agent Systems ซึ่งเป็นการต่อยอดแนวคิดนี้ให้ซับซ้อนและมีประสิทธิภาพมากยิ่งขึ้น!
ฝากติดตามซีรีส์ Data Mastery ด้วยนะครับ! 🚀
Data Science Explore the world of data science with Donato_Story
Dashboard Discover the power of data visualization with Donato_Story
Donato_Journey Join me on my journey (Thai version)
Course_Review Discover the training courses with Donato_Story (Thai version)
Let’s Connect!
Your thoughts and feedback are invaluable. Feel free to share them in the comments or connect with me on
- 🌐 Medium: medium.com/donato-story
- 📘Facebook: web.facebook.com/DonatoStory
- 💼 Linkedin: linkedin.com/in/nattapong-thanngam
Originally published on Medium
Related
Corrective RAG
Data Mastery Series — Episode 53: RAG ที่ “คิด” ก่อน “ตอบ” และ “แก้ไข” เมื่อผิดพลาด
Hierarchical Multi-Agent Systems
Data Mastery Series — Episode 59: การสร้างระบบ AI ทีมงานด้วย Supervisor Agent กับทีมย่อย
LangGraph Introduction
Data Mastery Series — Episode 50: Next-Level Chat with Document
LLM Note 2
Data Mastery Series — Episode 47: Summarization Techniques and Advanced RAG