LangGraph Supervisor¶
langgraph_supervisor.supervisor
¶
| FUNCTION | DESCRIPTION |
|---|---|
create_supervisor |
Create a multi-agent supervisor. |
create_supervisor
¶
create_supervisor(
agents: list[Pregel],
*,
model: LanguageModelLike,
tools: list[BaseTool | Callable] | ToolNode | None = None,
prompt: Prompt | None = None,
response_format: StructuredResponseSchema
| tuple[str, StructuredResponseSchema]
| None = None,
pre_model_hook: RunnableLike | None = None,
post_model_hook: RunnableLike | None = None,
parallel_tool_calls: bool = False,
state_schema: StateSchemaType | None = None,
context_schema: Type[Any] | None = None,
output_mode: OutputMode = "last_message",
add_handoff_messages: bool = True,
handoff_tool_prefix: str | None = None,
add_handoff_back_messages: bool | None = None,
supervisor_name: str = "supervisor",
include_agent_name: AgentNameMode | None = None,
**deprecated_kwargs: Unpack[DeprecatedKwargs],
) -> StateGraph
Create a multi-agent supervisor.
| PARAMETER | DESCRIPTION |
|---|---|
agents
|
List of agents to manage.
An agent can be a LangGraph |
model
|
Language model to use for the supervisor
TYPE:
|
tools
|
Tools to use for the supervisor
TYPE:
|
prompt
|
Optional prompt to use for the supervisor. Can be one of:
TYPE:
|
response_format
|
An optional schema for the final supervisor output. If provided, output will be formatted to match the given schema and returned in the If not provided, Can be passed in as: Important
Note
TYPE:
|
pre_model_hook
|
An optional node to add before the LLM node in the supervisor agent (i.e., the node that calls the LLM). Useful for managing long message histories (e.g., message trimming, summarization, etc.). Pre-model hook must be a callable or a runnable that takes in current graph state and returns a state update in the form of Important At least one of
TYPE:
|
post_model_hook
|
An optional node to add after the LLM node in the supervisor agent (i.e., the node that calls the LLM). Useful for implementing human-in-the-loop, guardrails, validation, or other post-processing. Post-model hook must be a callable or a runnable that takes in current graph state and returns a state update.
TYPE:
|
parallel_tool_calls
|
Whether to allow the supervisor LLM to call tools in parallel (only OpenAI and Anthropic). Use this to control whether the supervisor can hand off to multiple agents at once. If If Important This is currently supported only by OpenAI and Anthropic models. To control parallel tool calling for other providers, add explicit instructions for tool use to the system prompt.
TYPE:
|
state_schema
|
State schema to use for the supervisor graph.
TYPE:
|
context_schema
|
Specifies the schema for the context object that will be passed to the workflow. |
output_mode
|
Mode for adding managed agents' outputs to the message history in the multi-agent workflow. Can be one of:
TYPE:
|
add_handoff_messages
|
Whether to add a pair of
TYPE:
|
handoff_tool_prefix
|
Optional prefix for the handoff tools (e.g., If provided, the handoff tools will be named If not provided, the handoff tools will be named
TYPE:
|
add_handoff_back_messages
|
Whether to add a pair of
TYPE:
|
supervisor_name
|
Name of the supervisor node.
TYPE:
|
include_agent_name
|
Use to specify how to expose the agent name to the underlying supervisor LLM.
TYPE:
|
Example
from langchain_openai import ChatOpenAI
from langgraph_supervisor import create_supervisor
from langgraph.prebuilt import create_react_agent
# Create specialized agents
def add(a: float, b: float) -> float:
'''Add 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...'
math_agent = create_react_agent(
model="openai:gpt-4o",
tools=[add],
name="math_expert",
)
research_agent = create_react_agent(
model="openai:gpt-4o",
tools=[web_search],
name="research_expert",
)
# Create supervisor workflow
workflow = create_supervisor(
[research_agent, math_agent],
model=ChatOpenAI(model="gpt-4o"),
)
# Compile and run
app = workflow.compile()
result = app.invoke({
"messages": [
{
"role": "user",
"content": "what's the combined headcount of the FAANG companies in 2024?"
}
]
})
langgraph_supervisor.handoff
¶
| FUNCTION | DESCRIPTION |
|---|---|
create_handoff_tool |
Create a tool that can handoff control to the requested agent. |
create_forward_message_tool |
Create a tool the supervisor can use to forward a worker message by name. |
create_handoff_tool
¶
create_handoff_tool(
*,
agent_name: str,
name: str | None = None,
description: str | None = None,
add_handoff_messages: bool = True,
) -> BaseTool
Create a tool that can handoff control to the requested agent.
| PARAMETER | DESCRIPTION |
|---|---|
agent_name
|
The name of the agent to handoff control to, i.e.
the name of the agent node in the multi-agent graph.
Agent names should be simple, clear and unique, preferably in snake_case,
although you are only limited to the names accepted by LangGraph
nodes as well as the tool names accepted by LLM providers
(the tool name will look like this:
TYPE:
|
name
|
Optional name of the tool to use for the handoff.
If not provided, the tool name will be
TYPE:
|
description
|
Optional description for the handoff tool.
If not provided, the description will be
TYPE:
|
add_handoff_messages
|
Whether to add handoff messages to the message history.
If
TYPE:
|
create_forward_message_tool
¶
Create a tool the supervisor can use to forward a worker message by name.
This helps avoid information loss any time the supervisor rewrites a worker query to the user and also can save some tokens.
| PARAMETER | DESCRIPTION |
|---|---|
supervisor_name
|
The name of the supervisor node (used for namespacing the tool).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
BaseTool
|
The 'forward_message' tool.
TYPE:
|