Building a Multi-Agent System With LangGraph Supervisor

Introduction
Hello!
I'm Alex, a Generative AI Engineer on the Generative AI Development Project team at KINTO Technologies.
Lately, there's been a growing interest in building multi-agent systems using large language models (LLMs). So in this article, I'd like to share a case where I built a Supervisor-style multi-agent system in less than 30 minutes using the latest LangGraph update, langgraph_supervisor. This system allows a central supervisor agent to coordinate and manage multiple specialized agents. It can be applied to a wide range of use cases, from automating workflows to generating customizable proposal scenarios.
What is LangGraph?
LangGraph is a Python library designed to simplify the development of AI agents and Retrieval-Augmented Generation (RAG) systems. Combined with LangChain, complex workflows and tasks can be designed and implemented efficiently. It supports state persistence, tool invocation, and features like human-in-the-loop and post-task validation through a centralized persistence layer which served as the foundation for this Supervisor-style system.
What is langgraph-supervisor?
langgraph-supervisor is a Python library for building hierarchical multi-agent systems using LangGraph, which was recently released by LangChain. A central supervisory agent coordinates task assignments and facilitates communication among specialized agents. This enables the development of adaptable systems that can tackle complex tasks with ease and efficiency.
Key Features
-
Supervisor agent creation: Manages multiple specialized agents and orchestrates the overall workflow.
-
Tool-based agent handoff mechanism: Provides a mechanism for achieving smooth communication between agents.
-
Flexible message history management: Enables easy control over conversation flow by managing message history dynamically.
What is a Supervisor-style Multi Agent System?
Source: https://langchain-ai.github.io/langgraph/tutorials/multi_agent/agent_supervisor/
A Supervisor-style multi-agent system is a structure where a central controlling agent called the Supervisor coordinates with tool-enabled LLM agents, deciding which agent to call, when to do so, and what arguments to pass along.
Building a Multi-Agent System with langgraph-supervisor
The following steps outline how to build a multi-agent system using langgraph-supervisor. In this example, we’ll create a system that recommends a car and engine type based on simple, anonymized customer information. Additionally, the agent will retrieve car and engine information from the locally stored "Vehicle_Information.csv" file.
Environment Setup
Prepare Azure OpenAI API keys
This time, we'll be using GPT-4o via Azure OpenAI. Depending on your situation, you can also use the OpenAI API, or Anthropic API, etc.
And set Azure OpenAI API keys and endpoint as environment variables.
os.environ["AZURE_OPENAI_API_KEY"] = "YOUR API KEY"
os.environ["AZURE_OPENAI_ENDPOINT"] = "YOUR ENDPOINT"
os.environ["AZURE_OPENAI_API_VERSION"] = "YOUR API VERSION"
os.environ["AZURE_OPENAI_DEPLOYMENT"] = "YOUR DEPLOYMENT NAME"
Install LangGraph and LangChain
pip install langgraph
pip install langchain
pip install langchain_openai
Install langgraph-supervisor
pip install langgraph-supervisor
Setup of tool functions used by LLM and each Agent
Define the model using Azure OpenAI's GPT-4o. Next, define the tool functions for agents.
from langchain_openai import AzureChatOpenAI
import pandas as pd
# LLMの初期化
llm = AzureChatOpenAI(
azure_deployment=os.getenv("AZURE_OPENAI_DEPLOYMENT"),
api_version=os.getenv("AZURE_OPENAI_API_VERSION"),
)
car_information_path = "/xxx/xxx/車両情報.csv"
# ツール関数の定義例
# CSVファイルから車両情報を読み込み、候補を生成する例
def get_car_features():
"""The python code to get car information."""
path = car_information_path # CSVファイルのパスを指定
df = pd.read_csv(car_information_path)
car_features = df[["車種名", "ボディタイプ", "説明"]].drop_duplicates()
return car_features.to_dict(orient="records")
# 選択された車種に対してエンジンタイプを抽出する例
def get_engine_type(selected_car):
"""The python code to get engine type and engine information."""
path = car_information_path
df = pd.read_csv(car_information_path)
engine_types = list(df[df["車種名"] == selected_car]["エンジンタイプ"].unique())
return engine_types, "エンジンに関する補足情報"
Defining Each Agent
We use LangGraph's "create_react_agent" to define each specialized agent (car model recommendation, engine type selection).
from langgraph.prebuilt import create_react_agent
# 車種推薦エージェント
car_agent = create_react_agent(
model=llm,
tools=[get_car_features],
name="car_agent",
prompt="""
# Instructions
Based on the recommended pattern, choose a car model to recommend and explain the reasoning in about 200 characters.
"""
)
# Engine type selection agent
engine_agent = create_react_agent(
model=llm,
tools=[get_engine_type],
name="engine_agent",
prompt="""
# Instructions
Choose the most suitable engine type for the recommended car and explain the reasoning in about 200 characters.
"""
)
Defining the Supervisor
Create a Supervisor to oversee each agent and generate a final recommendation based on customer information. One improvement we made here was modularizing the prompt input to the Supervisor, allowing for greater versatility. By changing the contents of the role and task variables and the associated agent, the system can easily be used for other tasks.
from langgraph_supervisor import create_supervisor
# Create the contents of each module in the prompt
role = "車のセールスマン"
task = "顧客情報をもとに、最適な車とエンジンタイプを提案してください。"
guideline = """
- 出力は必ず上記JSON形式で行ってください。
- 各根拠は200文字程度の豊かな文章で記述してください。"
"""
output_format = """
{
"提案内容": {
"提案する車種",
"車種の根拠",
"選択したエンジンタイプ",
"エンジン選定理由"
}
}
"""
# Create a prompt
system_prompt = f"""
## Role
あなたは優れた{role}です。
## Task
{task}
## Guidelines
{guideline}
## 最終生成物のフォーマット
{output_format}
"""
# Create the Supervisor
workflow = create_supervisor(
# 先ほど作成したエージェントと紐づく
[car_agent, engine_agent],
model=llm,
prompt=system_prompt
)
# Compile the graph
app = workflow.compile()
Example Run
from langchain.schema import HumanMessage
import time
# Example of customer information (add more details as needed)
customer_info = "顧客情報: Age 35, prioritizes fuel efficiency, currently drives a compact car.
# Example run
start_time = time.time()
result = app.invoke({
"messages": [
{"role": "user", "content": customer_info}
]
})
end_time = time.time()
# Display the final output
print("最終提案:")
print(result["messages"][-1].content)
print("実行時間: {:.3f}秒".format(end_time - start_time))
Response from the Supervisor
最終提案:
{
"提案内容": {
"提案する車種": "ハイブリッド技術を採用した最新コンパクトカー",
"車種の根拠": "顧客は現在コンパクトカーに乗車中であり、燃費性能の良さを重視しています。 ハイブリッド技術を採用した車は、燃料使用の効率が高く、経済的負担や環境への配慮が優れています。 また、ハイブリッドシステムは短距離や都市部での走行にも最適です。 そのため、最新のハイブリッドコンパクトカーが最適な選択肢となります。",
"選択したエンジンタイプ": "ハイブリッドエンジン",
"エンジン選定理由": "ハイブリッドエンジンは燃費性能が非常に優れており、顧客のニーズである燃料効率を最優先に考慮しています。 さらに、コンパクトカーとの相性も良く、都市部での利用や日常の移動において高いパフォーマンスを発揮します。 このエンジンの選択は、快適性、経済性、環境性能を全て満たします。"
}
}
Run Time: 21.938 seconds
Impressions After Building the System
Rapid Prototyping
The complex inter-agent coordination and state management typically required in LangGraph were implemented with simple code using langgraph_supervisor, and a prototype was successfully built in under 30 minutes. A pretty impressive result.
Flexibility and Scalability
Each agent can be implemented with its own prompt and tool functions, allowing for easy customization to meet specific business needs. In addition, since state management is handled centrally, future improvements and feature additions are expected to be carried out smoothly.
We Are Hiring!
KINTO Technologies is looking for passionate colleagues to help drive AI adoption in our businesses. We're happy to start with a casual interview. If you’re even slightly interested, feel free to reach out via the link below or through X DMs. We look forward to hearing from you!!
Thanks for reading all the way through!
関連記事 | Related Posts
We are hiring!
【PjM】プロジェクト推進G/東京
新サービス開発部 プロジェクト推進グループについてプロジェクト推進グループでは、クルマのサブスクリプションサービスである『 KINTO ONE 』をはじめ、国内向けサービスのプロジェクト計画立案からリリース、運用保守に至るまでのプロジェクト管理を行っています。
【フロントエンドエンジニア リーダークラス(DX等)】DX開発G/東京・大阪
DX開発グループについて全国約4,000店舗のトヨタ販売店の営業プロセスを中心に、販売店スタッフのお困りごとをテクノロジーとクリエイティブの力で解決に導く事業を展開しています。募集背景当グループでは、全国約4,000店舗のトヨタ販売店の営業プロセスを中心に、販売店スタッフのお困りごとをテクノロジーとクリエイティブの力で解決に導く「販売店DX事業」を展開しています。