Skip to main content

An implementation of a supervisor multi-agent architecture using LangGraph

Project description

🤖 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
  • 🔄 Support for both router and orchestrator patterns
  • 🛠️ 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.",
)

# Compile and run
app = workflow.compile()
result = app.invoke({
    "messages": [
        {
            "role": "user",
            "content": "what's the combined headcount of the FAANG companies in 2024?"
        }
    ]
})

Agent Interaction Patterns

Orchestrator Pattern

In orchestrator mode (is_router=False), agents always return control to the supervisor. The supervisor can then decide who to call next, or respond to the user.

orchestrator = create_supervisor(
    [agent1, agent2],
    is_router=False,
    ...
)

Router Pattern

In router mode (is_router=True), agents can respond directly to the user. The supervisor just routes the user's message to the appropriate agent.

router = create_supervisor(
    [agent1, agent2],
    is_router=True,
    ...
)

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],
    agent_output_mode="full_history"
)

Include only the final agent response:

workflow = create_supervisor(
    agents=[agent1, agent2],
    agent_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")

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

langgraph_supervisor-0.0.1.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

langgraph_supervisor-0.0.1-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

Details for the file langgraph_supervisor-0.0.1.tar.gz.

File metadata

File hashes

Hashes for langgraph_supervisor-0.0.1.tar.gz
Algorithm Hash digest
SHA256 cdf7b4f2e68838b96bbb039862e1ccf57623adf408590c10d84033e58ba3d55c
MD5 727781f1c3330406c7bac003fa9fb688
BLAKE2b-256 e605702f0d7583fa1d28f6a4936b7e40c9a392fbc6617f0afb353bb50691e142

See more details on using hashes here.

File details

Details for the file langgraph_supervisor-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for langgraph_supervisor-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 910354de37d442d86e69e5d2830feba622f586243f2648aa21ede9fdfb220b3f
MD5 57323182ccaad847e9e616db5c044fee
BLAKE2b-256 9c5984998ebf1077d8369485ea548a7230e4d057ba988ccdb946c981f54d0414

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page