🤖 LangGraph Multi-Agent Supervisor
A Python library for creating hierarchical multi-agent systems using LangGraph. Hierarchical systems are a type of multi-agent architecture where specialized agents are coordinated by a central supervisor agent. The supervisor controls all communication flow and task delegation, making decisions about which agent to invoke based on the current context and task requirements.
Features
- 🤖 Create a supervisor agent to orchestrate multiple specialized agents
- 🛠️ Tool-based agent handoff mechanism for communication between agents
- 📝 Flexible message history management for conversation control
This library is built on top of LangGraph, a powerful framework for building agent applications, and comes with out-of-box support for streaming, short-term and long-term memory and human-in-the-loop
Installation
pip install langgraph-supervisor
Quickstart
Here's a simple example of a supervisor managing two specialized agents:
pip install langgraph-supervisor langchain-openai
export OPENAI_API_KEY=<your_api_key>
from langchain_openai import ChatOpenAI
from langgraph_supervisor import create_supervisor
from langgraph.prebuilt import create_react_agent
model = ChatOpenAI(model="gpt-4o")
# Create specialized agents
def add(a: float, b: float) -> float:
"""Add two numbers."""
return a + b
def multiply(a: float, b: float) -> float:
"""Multiply two numbers."""
return a * b
def web_search(query: str) -> str:
"""Search the web for information."""
return (
"Here are the headcounts for each of the FAANG companies in 2024:\n"
"1. **Facebook (Meta)**: 67,317 employees.\n"
"2. **Apple**: 164,000 employees.\n"
"3. **Amazon**: 1,551,000 employees.\n"
"4. **Netflix**: 14,000 employees.\n"
"5. **Google (Alphabet)**: 181,269 employees."
)
math_agent = create_react_agent(
model=model,
tools=[add, multiply],
name="math_expert",
prompt="You are a math expert. Always use one tool at a time."
)
research_agent = create_react_agent(
model=model,
tools=[web_search],
name="research_expert",
prompt="You are a world class researcher with access to web search. Do not do any math."
)
# Create supervisor workflow
workflow = create_supervisor(
[research_agent, math_agent],
model=model,
prompt=(
"You are a team supervisor managing a research expert and a math expert. "
"For current events, use research_agent. "
"For math problems, use math_agent."
)
)
# Compile and run
app = workflow.compile()
result = app.invoke({
"messages": [
{
"role": "user",
"content": "what's the combined headcount of the FAANG companies in 2024?"
}
]
})
Message History Management
You can control how agent messages are added to the overall conversation history of the multi-agent system:
Include full message history from an agent:
workflow = create_supervisor(
agents=[agent1, agent2],
output_mode="full_history"
)
Include only the final agent response:
workflow = create_supervisor(
agents=[agent1, agent2],
output_mode="last_message"
)
Multi-level Hierarchies
You can create multi-level hierarchical systems by creating a supervisor that manages multiple supervisors.
research_team = create_supervisor(
[research_agent, math_agent],
model=model,
).compile(name="research_team")
writing_team = create_supervisor(
[writing_agent, publishing_agent],
model=model,
).compile(name="writing_team")
top_level_supervisor = create_supervisor(
[research_team, writing_team],
model=model,
).compile(name="top_level_supervisor")
Adding Memory
You can add short-term and long-term memory to your supervisor multi-agent system. Since create_supervisor() returns an instance of StateGraph that needs to be compiled before use, you can directly pass a checkpointer or a store instance to the .compile() method:
from langgraph.checkpoint.memory import InMemorySaver
from langgraph.store.memory import InMemoryStore
checkpointer = InMemorySaver()
store = InMemoryStore()
model = ...
research_agent = ...
math_agent = ...
workflow = create_supervisor(
[research_agent, math_agent],
model=model,
prompt="You are a team supervisor managing a research expert and a math expert.",
)
# Compile with checkpointer/store
app = workflow.compile(
checkpointer=checkpointer,
store=store
)
Using Functional API
Here's a simple example of a supervisor managing two specialized agentic workflows created using Functional API:
pip install langgraph-supervisor langchain-openai
export OPENAI_API_KEY=<your_api_key>
from langgraph.prebuilt import create_react_agent
from langgraph_supervisor import create_supervisor
from langchain_openai import ChatOpenAI
from langgraph.func import entrypoint, task
from langgraph.graph import add_messages
model = ChatOpenAI(model="gpt-4o")
# Create specialized agents
# Functional API - Agent 1 (Joke Generator)
@task
def generate_joke(messages):
"""First LLM call to generate initial joke"""
system_message = {
"role": "system",
"content": "Write a short joke"
}
msg = model.invoke(
[system_message] + messages
)
return msg
@entrypoint()
def joke_agent(state):
joke = generate_joke(state['messages']).result()
messages = add_messages(state["messages"], [joke])
return {"messages": messages}
joke_agent.name = "joke_agent"
# Graph API - Agent 2 (Research Expert)
def web_search(query: str) -> str:
"""Search the web for information."""
return (
"Here are the headcounts for each of the FAANG companies in 2024:\n"
"1. **Facebook (Meta)**: 67,317 employees.\n"
"2. **Apple**: 164,000 employees.\n"
"3. **Amazon**: 1,551,000 employees.\n"
"4. **Netflix**: 14,000 employees.\n"
"5. **Google (Alphabet)**: 181,269 employees."
)
research_agent = create_react_agent(
model=model,
tools=[web_search],
name="research_expert",
prompt="You are a world class researcher with access to web search. Do not do any math."
)
# Create supervisor workflow
workflow = create_supervisor(
[research_agent, joke_agent],
model=model,
prompt=(
"You are a team supervisor managing a research expert and a joke expert. "
"For current events, use research_agent. "
"For any jokes, use joke_agent."
)
)
# Compile and run
app = workflow.compile()
result = app.invoke({
"messages": [
{
"role": "user",
"content": "Share a joke to relax and start vibe coding for my next project idea."
}
]
})
for m in result["messages"]:
m.pretty_print()
Release files for langgraph-supervisor 0.0.9
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| langgraph_supervisor-0.0.9.tar.gz | 12.7 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| langgraph_supervisor-0.0.9-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 22.7 kB
Release files / langgraph_supervisor-0.0.9.tar.gz
| Download URL | langgraph_supervisor-0.0.9.tar.gz |
|---|---|
| Size | 12.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
86c0b6e5bf0bc20ebc53ffa4319014e8f293a32da894e4ef9f10fc737f843bed
|
|
BLAKE2b-256 checksum How to use checksums |
96deae854b7721a56d37f9a800bd2e7c7b41551d564c50dcaaf892ea3e6659bf
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Release files / langgraph_supervisor-0.0.9-py3-none-any.whl
| Download URL | langgraph_supervisor-0.0.9-py3-none-any.whl |
|---|---|
| Size | 10.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
6dd365871a006d3a99accde94f0c349d6db5329cbe6eb9cca188c3bd9d3071b3
|
|
BLAKE2b-256 checksum How to use checksums |
384f2631393c62e9ec4f717ca7fea6e81c3ff9c100f45f555ded7f00d381cd8a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|