Supervisor Architecture in Multi-Agent Systems

From single agents to workflows, we have now entered the era of multi-agent systems. Multi-agent systems essentially involve multiple agents collaborating under the guidance of a large model to complete user tasks. In this context, various agent architectures have emerged, such as centralized, decentralized, and hierarchical structures. This article analyzes the multi-agent design under the supervisor architecture based on the open-source project langgraph-supervisor-py. The supervisor architecture consists of one supervisor and multiple agents, where the supervisor is responsible for managing the agents, and the agents perform specific tasks. Execution begins with the supervisor, which continuously adjusts the agents based on the responses from the large model. Each agent returns to the supervisor after execution, which then queries the large model again, repeating this process until a result is obtained.

Usage Example

Let’s start with an official example.

from langchain_openai import ChatOpenAI
from langgraph_supervisor import create_supervisor
from langgraph.prebuilt import create_react_agent
from langchain_core.runnables.graph import MermaidDrawMethod

# Tools
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 Calculation Agent
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."
)

# Search Agent
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."
)

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."
    )
)
app = workflow.compile()

# Draw Graph
img = app.get_graph().draw_mermaid_png(
            draw_method=MermaidDrawMethod.API,
        )
with open("graph.png", "wb") as f:
    f.write(img)
    f.close()

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

In the example above, a supervisor and two agents (the supervisor is also an agent) are created: one for searching and one for performing mathematical calculations. Each agent has its own set of tools. The three nodes form a graph structure as shown below.

Supervisor Architecture in Multi-Agent SystemsNext, we will analyze the example in detail.

create_react_agent

create_react_agent can create a react agent, and its working principle is as follows:

  1. The agent calls the large model to obtain tool invocation information.
  2. The large model returns which tool to invoke.
  3. The agent invokes the tool, obtains the result, and returns to step 1 unless it is determined that no further tool invocation is needed. The implementation is as follows.
def create_react_agent(
    model: Union[str, LanguageModelLike],
    tools: Union[Sequence[Union[BaseTool, Callable]], ToolNode],
    *,
    prompt: Optional[Prompt] = None,
    ...) -> CompiledGraph:
    # Create a Tool node to handle tools uniformly
    tool_node = ToolNode(tools)
    tool_classes = list(tool_node.tools_by_name.values())
    tool_calling_enabled = len(tool_classes) > 0
    # Bind to model, passing tool information when calling the large model
    if _should_bind_tools(model, tool_classes) and tool_calling_enabled:
        model = cast(BaseChatModel, model).bind_tools(tool_classes)
    model_runnable = _get_prompt_runnable(prompt) | model
    # Processing function for the agent node
    def call_model(state: StateSchema, config: RunnableConfig) -> StateSchema:
        state = _get_model_input_state(state)
        response = cast(AIMessage, model_runnable.invoke(state, config))
        # Add agent name to the AIMessage
        response.name = name
        return ...
    # Function to determine if the agent should continue
    def should_continue(state: StateSchema) -> Union[str, list]:
        messages = _get_state_value(state, "messages")
        last_message = messages[-1]
        # If no tool invocation is needed, return to end
        if not isinstance(last_message, AIMessage) or not last_message.tool_calls:
            return END if response_format is None else "generate_structured_response"
        # Jump to Tool node processing
        else:
            if version == "v1":
                return "tools"
            elif version == "v2":
                tool_calls = [
                    tool_node.inject_tool_args(call, state, store)  # type: ignore[arg-type]
                    for call in last_message.tool_calls
                ]
                return [Send("tools", [tool_call]) for tool_call in tool_calls]
    # Start building the execution flow graph
    workflow = StateGraph(state_schema or AgentState, config_schema=config_schema)
    # Add agent node
    workflow.add_node(
        "agent", RunnableCallable(call_model, acall_model), input=input_schema
    )
    # Add Tool node
    workflow.add_node("tools", tool_node)
    # Set the starting execution node
    entrypoint = "agent"
    workflow.set_entry_point(entrypoint)
    # Set conditional judgment node
    workflow.add_conditional_edges(
        "agent",
        should_continue,
        path_map=should_continue_destinations,
    )
    # Set edges from Tool node to Agent, automatically jumping back to Agent after tool execution
    workflow.add_edge("tools", entrypoint)
    return workflow.compile(
        checkpointer=checkpointer,
        store=store,
        interrupt_before=interrupt_before,
        interrupt_after=interrupt_after,
        debug=debug,
        name=name,
    )

As can be seen, the ToolNode internally calls specific tools based on their names, obtains results, and then jumps back to the agent. The structure diagram is as follows.

Supervisor Architecture in Multi-Agent Systems

create_supervisor

Next, we will analyze the internal architecture of the supervisor starting from create_supervisor.

def create_supervisor(
    agents: list[Pregel],
    *,
    model: LanguageModelLike,
    tools: list[BaseTool | Callable] | None = None,
    prompt: Prompt | None = None,
    state_schema: StateSchemaType = AgentState,
    config_schema: Type[Any] | None = None,
    add_handoff_messages: bool = True,
    handoff_tool_prefix: Optional[str] = None,
    add_handoff_back_messages: Optional[bool] = None,
    supervisor_name: str = "supervisor",
) -> StateGraph:
    # Default to recording handoff information in messages
    if add_handoff_back_messages is None:
        add_handoff_back_messages = add_handoff_messages
    # Create handoff tools for transferring control
    all_tools = [
        create_handoff_tool(
            agent_name=agent.name,
            name=(None),
            add_handoff_messages=add_handoff_messages,
        )
        for agent in agents
    ]
    # Bind tools, passing them when calling the large model
    model = model.bind_tools(all_tools)
    # Create an agent with the role of supervisor
    supervisor_agent = create_react_agent(
        name=supervisor_name,
        model=model,
        tools=all_tools,
        prompt=prompt,
        state_schema=state_schema,  # AgentState, reducer is add_messages
        response_format=response_format,
    )
    builder = StateGraph(state_schema, config_schema=config_schema)
    # Add supervisor to the graph, downstream are various agents and END
    builder.add_node(supervisor_agent, destinations=tuple(agent_names) + (END,))
    # Set the execution starting point to supervisor
    builder.add_edge(START, supervisor_agent.name)
    for agent in agents:
        # Add each agent to the graph
        builder.add_node(
            agent.name,
            _make_call_agent(
                agent,
                output_mode,
                add_handoff_back_messages=add_handoff_back_messages,
                supervisor_name=supervisor_name,
            ),
        )
        # Each agent has an edge pointing to the supervisor
        builder.add_edge(agent.name, supervisor_agent.name)
    return builder

The general logic of create_supervisor is to create a supervisor agent and then construct an execution graph. Two areas need to be analyzed: create_handoff_tool and _make_call_agent. Let’s first look at create_handoff_tool.

def create_handoff_tool(
    *,
    agent_name: str,
    name: str | None = None,
    description: str | None = None,
    add_handoff_messages: bool = True,
) -> BaseTool:
    # Tool name and description
    if name is None:
        name = f"transfer_to_{_normalize_agent_name(agent_name)}"
    if description is None:
        description = f"Ask agent '{agent_name}' for help"
    @tool(name, description=description)
    def handoff_to_agent(
        state: Annotated[dict, InjectedState],
        tool_call_id: Annotated[str, InjectedToolCallId],
    ):
        tool_message = ToolMessage(
            content=f"Successfully transferred to {agent_name}",
            name=name,
            tool_call_id=tool_call_id,
            response_metadata={METADATA_KEY_HANDOFF_DESTINATION: agent_name},
        )
        # Append handoff message
        handoff_messages = state["messages"] + [tool_message]
        # Jump to the specific agent
        return Command(
            goto=agent_name,
            graph=Command.PARENT,
            update={**state, "messages": handoff_messages},
        )
    handoff_to_agent.metadata = {METADATA_KEY_HANDOFF_DESTINATION: agent_name}
    return handoff_to_agent

create_handoff_tool is used to create a tool, and this tool information will be sent to the large model. The large model will determine which tool the supervisor should execute based on the user’s question and the tool information (name and description). After receiving the response from the large model, the supervisor will execute the create_handoff_tool function and ultimately jump to the specific agent. The execution of the agent is encapsulated by _make_call_agent.

def _make_call_agent(
    agent: Pregel,
    output_mode: OutputMode,
    add_handoff_back_messages: bool,
    supervisor_name: str,
) -> Callable[[dict], dict] | RunnableCallable:
    def _process_output(output: dict) -> dict:
        messages = output["messages"]
        # Append handoff message, here is the message to handoff back to supervisor
        if add_handoff_back_messages:
            messages.extend(create_handoff_back_messages(agent.name, supervisor_name))
        # The agent has an edge pointing to the supervisor, so it will automatically jump back to the supervisor after execution
        return {
            **output,
            "messages": messages,
        }
    def call_agent(state: dict, config: RunnableConfig) -> dict:
        # Call the large model
        output = agent.invoke(state, config)
        return _process_output(output)
    return RunnableCallable(call_agent, ...)

_make_call_agent performs some processing and appends the handoff message before ultimately jumping back to the supervisor. Since the agent has an edge pointing to the supervisor, it will automatically jump back, unlike when the supervisor jumps to the agent, which requires manual control. From the above analysis, we can see that in the supervisor architecture, the processing flow is as follows:

  1. The supervisor passes messages and a list of tools, then calls the large model, which returns which tool to invoke.
  2. Handoff to a specific agent.
  3. The agent passes messages and its own list of tools, then calls the large model, which returns which tool to invoke.
  4. The agent invokes the tool, obtains the tool’s return, and calls the large model again to get the final result.
  5. Handoff back to the supervisor, returning to step 1, until the large model returns the final result.

Invocation Logs

Below are the logs of the large model invocation from the previous example.

  1. Supervisor
# Supervisor calls the large model and passes the list of tools, i.e., which agents
request: {  "tools": [    {      "type": "function",      "function": {        "name": "transfer_to_research_expert",        "description": "Ask agent 'research_expert' for help",        "parameters": {          "properties": {},          "type": "object"        }      }    },    {      "type": "function",      "function": {        "name": "transfer_to_math_expert",        "description": "Ask agent 'math_expert' for help",        "parameters": {          "properties": {},          "type": "object"        }      }    }  ],  "parallel_tool_calls": false,  "messages": [    {      "content": "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.",      "role": "system"    },    {      "content": "what's the combined headcount of the FAANG companies in 2024?",      "role": "user"    }  ]}
# The large model returns the call to transfer_to_research_expert agent
response: {  "id": "02174685630333191645265e627e21d5dcc187f9552fedf6fda0b",  "choices": [    {      "finish_reason": "tool_calls",      "index": 0,      "logprobs": null,      "message": {        "content": "",        "role": "assistant",        "tool_calls": [          {            "id": "call_fyew4c06vn9ide7nhk6sijws",            "function": {              "arguments": " {}",              "name": "transfer_to_research_expert"            },            "type": "function"          }        ]      }    }  ],}
  1. Handoff to the search agent
# The search agent calls the large model and passes the list of tools
request: {  "tools": [    {      "type": "function",      "function": {        "name": "web_search",        "description": "Search the web for information.",        "parameters": {          "properties": {            "query": {              "type": "string"            }          },          "required": [            "query"          ],          "type": "object"        }      }    }  ],  "messages": [    {      "content": "You are a world class researcher with access to web search. Do not do any math.",      "role": "system"    },    {      "content": "what's the combined headcount of the FAANG companies in 2024?",      "role": "user"    },    {      "content": null,      "name": "supervisor",      "role": "assistant",      "tool_calls": [        {          "type": "function",          "id": "call_fyew4c06vn9ide7nhk6sijws",          "function": {            "name": "transfer_to_research_expert",            "arguments": "{}"          }        }      ]    },    {      "content": "Successfully transferred to research_expert",      "role": "tool",      "tool_call_id": "call_fyew4c06vn9ide7nhk6sijws"    }  ]}
# The large model returns the search tool invocation and provides parameters
response: {  "id": "02174685630508391645265e627e21d5dcc187f9552fedf724cf2",  "choices": [    {      "finish_reason": "tool_calls",      "index": 0,      "logprobs": null,      "message": {        "content": "",        "role": "assistant",        "tool_calls": [          {            "id": "call_t52sv5g0zm4x1tft5hcnwjjs",            "function": {              "name": "web_search",              "arguments": " {\n        \"query\": \"Combined headcount of FAANG companies in 2024\"\n    }\n",              "type": "function"            }          }        ]      }    }  ],}
# The search agent obtains the results from the search tool and calls the large model again
request: {  "tools": [    {      "type": "function",      "function": {        "name": "web_search",        "description": "Search the web for information.",        "parameters": {          "properties": {            "query": {              "type": "string"            }          },          "required": [            "query"          ],          "type": "object"        }      }    }  ],  "messages": [    {      "content": "You are a world class researcher with access to web search. Do not do any math.",      "role": "system"    },    {      "content": "what's the combined headcount of the FAANG companies in 2024?",      "role": "user"    },    {      "content": null,      "name": "supervisor",      "role": "assistant",      "tool_calls": [        {          "type": "function",          "id": "call_fyew4c06vn9ide7nhk6sijws",          "function": {            "name": "transfer_to_research_expert",            "arguments": "{}"          }        }      ]    },    {      "content": "Successfully transferred to research_expert",      "role": "tool",      "tool_call_id": "call_fyew4c06vn9ide7nhk6sijws"    },    {      "content": null,      "name": "research_expert",      "role": "assistant",      "tool_calls": [        {          "type": "function",          "id": "call_t52sv5g0zm4x1tft5hcnwjjs",          "function": {            "name": "web_search",            "arguments": "{\"query\": \"Combined headcount of FAANG companies in 2024\"}"          }        }      ]    },    {      "content": "Here are the headcounts for each of the FAANG companies in 2024:\n1. **Facebook (Meta)**: 67,317 employees.\n2. **Apple**: 164,000 employees.\n3. **Amazon**: 1,551,000 employees.\n4. **Netflix**: 14,000 employees.\n5. **Google (Alphabet)**: 181,269 employees.",      "role": "tool",      "tool_call_id": "call_t52sv5g0zm4x1tft5hcnwjjs"    }  ]}
# The large model determines that this information is sufficient and does not require further searching
response: {  "id": "02174685630701191645265e627e21d5dcc187f9552fedf6f3f21",  "choices": [    {      "finish_reason": "stop",      "index": 0,
      "logprobs": null,
      "message": {        "content": "The individual headcounts of FAANG companies in 2024 are as follows: Meta has 67,317 employees, Apple has 164,000 employees, Amazon has 1,551,000 employees, Netflix has 14,000 employees, and Alphabet has 181,269 employees. You can sum these figures to get the combined headcount.",        "role": "assistant"
      }
    }
  ],}
# Handoff back to supervisor
  1. Handoff back to supervisor, the supervisor calls the large model again
# Handoff back to supervisor, the supervisor calls the large model again
request: {  "tools": [    {      "type": "function",      "function": {        "name": "transfer_to_research_expert",        "description": "Ask agent 'research_expert' for help",        "parameters": {          "properties": {},          "type": "object"        }      }    },    {      "type": "function",      "function": {        "name": "transfer_to_math_expert",        "description": "Ask agent 'math_expert' for help",        "parameters": {          "properties": {},          "type": "object"        }      }    }  ],  "parallel_tool_calls": false,  "messages": [    {      "content": "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.",      "role": "system"    },    {      "content": "what's the combined headcount of the FAANG companies in 2024?",      "role": "user"    },    {      "content": null,      "name": "supervisor",      "role": "assistant",      "tool_calls": [        {          "type": "function",          "id": "call_fyew4c06vn9ide7nhk6sijws",          "function": {            "name": "transfer_to_research_expert",            "arguments": "{}"          }        }      ]    },    {      "content": "Successfully transferred to research_expert",      "role": "tool",      "tool_call_id": "call_fyew4c06vn9ide7nhk6sijws"    },    {      "content": "The individual headcounts of FAANG companies in 2024 are as follows: Meta has 67,317 employees, Apple has 164,000 employees, Amazon has 1,551,000 employees, Netflix has 14,000 employees, and Alphabet has 181,269 employees. You can sum these figures to get the combined head - count.",      "name": "research_expert",      "role": "assistant"    },    {      "content": "Transferring back to supervisor",      "name": "research_expert",      "role": "assistant",      "tool_calls": [        {          "type": "function",          "id": "267ff0b0-d27b-4764-a68b-71eda788379a",          "function": {            "name": "transfer_back_to_supervisor",            "arguments": "{}"          }        }      ]    },    {      "content": "Successfully transferred back to supervisor",      "role": "tool",      "tool_call_id": "267ff0b0-d27b-4764-a68b-71eda788379a"    }  ]}
# The large model returns the call to the math calculation agent
response: {  "id": "02174685631050391645265e627e21d5dcc187f9552fedf85877e",  "choices": [    {      "finish_reason": "tool_calls",      "index": 0,      "logprobs": null,      "message": {        "content": "",        "role": "assistant",        "tool_calls": [          {            "id": "call_z9x3nnqjvnp9hxuehejkivgn",            "function": {              "arguments": " {}",              "name": "transfer_to_math_expert"            },            "type": "function"          }        ]      }    }  ],}
  1. Handoff to the math calculation agent
# The math calculation agent calls the large model and passes the list of tools
request: {  "tools": [    {      "type": "function",      "function": {        "name": "add",        "description": "Add two numbers.",        "parameters": {          "properties": {            "a": {              "type": "number"            },            "b": {              "type": "number"            }          },          "required": [            "a",            "b"          ],          "type": "object"        }      }    },    {      "type": "function",      "function": {        "name": "multiply",        "description": "Multiply two numbers.",        "parameters": {          "properties": {            "a": {              "type": "number"            },            "b": {              "type": "number"            }          },          "required": [            "a",            "b"          ],          "type": "object"        }      }    }  ],  "messages": [    {      "content": "You are a math expert. Always use one tool at a time.",      "role": "system"    },    {      "content": "what's the combined headcount of the FAANG companies in 2024?",      "role": "user"    },    {      "content": null,      "name": "supervisor",      "role": "assistant",      "tool_calls": [        {          "type": "function",          "id": "call_fyew4c06vn9ide7nhk6sijws",          "function": {            "name": "transfer_to_research_expert",            "arguments": "{}"          }        }      ]    },    {      "content": "Successfully transferred to research_expert",      "role": "tool",      "tool_call_id": "call_fyew4c06vn9ide7nhk6sijws"    },    {      "content": "The individual headcounts of FAANG companies in 2024 are as follows: Meta has 67,317 employees, Apple has 164,000 employees, Amazon has 1,551,000 employees, Netflix has 14,000 employees, and Alphabet has 181,269 employees. You can sum these figures to get the combined head - count.",      "name": "research_expert",      "role": "assistant"    },    {      "content": "Transferring back to supervisor",      "name": "research_expert",      "role": "assistant",      "tool_calls": [        {          "type": "function",          "id": "267ff0b0-d27b-4764-a68b-71eda788379a",          "function": {            "name": "transfer_back_to_supervisor",            "arguments": "{}"          }        }      ]    },    {      "content": "Successfully transferred back to supervisor",      "role": "tool",      "tool_call_id": "267ff0b0-d27b-4764-a68b-71eda788379a"    },    {      "content": null,      "name": "supervisor",      "role": "assistant",      "tool_calls": [        {          "type": "function",          "id": "call_z9x3nnqjvnp9hxuehejkivgn",          "function": {            "name": "transfer_to_math_expert",            "arguments": "{}"          }        }      ]    },    {      "content": "Successfully transferred to math_expert",      "role": "tool",      "tool_call_id": "call_z9x3nnqjvnp9hxuehejkivgn"    }  ]}
# The large model returns the call to the addition tool and provides parameters
response: {  "id": "02174685631226191645265e627e21d5dcc187f9552fedf93e39a",  "choices": [    {      "finish_reason": "tool_calls",      "index": 0,      "logprobs": null,      "message": {        "content": "",        "role": "assistant",        "tool_calls": [          {            "id": "call_jd5esfvy2qsh9tdz23361pha",            "function": {              "name": "add",              "arguments": " {\n        \"a\": 67317, \n        \"b\": 164000\n    }\n",              "type": "function"            }          }        ]      }    }  ],}
# The math calculation agent calls the large model and passes the list of tools
request: {  "tools": [    {      "type": "function",      "function": {        "name": "add",        "description": "Add two numbers.",        "parameters": {          "properties": {            "a": {              "type": "number"            },            "b": {              "type": "number"            }          },          "required": [            "a",            "b"          ],          "type": "object"        }      }    },    {      "type": "function",      "function": {        "name": "multiply",        "description": "Multiply two numbers.",        "parameters": {          "properties": {            "a": {              "type": "number"            },            "b": {              "type": "number"            }          },          "required": [            "a",            "b"          ],          "type": "object"        }      }    }  ],  "messages": [    {      "content": "You are a math expert. Always use one tool at a time.",      "role": "system"    },    {      "content": "what's the combined headcount of the FAANG companies in 2024?",      "role": "user"    },    {      "content": null,      "name": "supervisor",      "role": "assistant",      "tool_calls": [        {          "type": "function",          "id": "call_fyew4c06vn9ide7nhk6sijws",          "function": {            "name": "transfer_to_research_expert",            "arguments": "{}"          }        }      ]    },    {      "content": "Successfully transferred to research_expert",      "role": "tool",      "tool_call_id": "call_fyew4c06vn9ide7nhk6sijws"    },    {      "content": "The individual headcounts of FAANG companies in 2024 are as follows: Meta has 67,317 employees, Apple has 164,000 employees, Amazon has 1,551,000 employees, Netflix has 14,000 employees, and Alphabet has 181,269 employees. You can sum these figures to get the combined head - count.",      "name": "research_expert",      "role": "assistant"    },    {      "content": "Transferring back to supervisor",      "name": "research_expert",      "role": "assistant",      "tool_calls": [        {          "type": "function",          "id": "267ff0b0-d27b-4764-a68b-71eda788379a",          "function": {            "name": "transfer_back_to_supervisor",            "arguments": "{}"          }        }      ]    },    {      "content": "Successfully transferred back to supervisor",      "role": "tool",      "tool_call_id": "267ff0b0-d27b-4764-a68b-71eda788379a"    },    {      "content": null,      "name": "supervisor",      "role": "assistant",      "tool_calls": [        {          "type": "function",          "id": "call_z9x3nnqjvnp9hxuehejkivgn",          "function": {            "name": "transfer_to_math_expert",            "arguments": "{}"          }        }      ]    },    {      "content": "Successfully transferred to math_expert",      "role": "tool",      "tool_call_id": "call_z9x3nnqjvnp9hxuehejkivgn"    },    {      "content": null,      "name": "math_expert",      "role": "assistant",      "tool_calls": [        {          "type": "function",          "id": "call_jd5esfvy2qsh9tdz23361pha",          "function": {            "name": "add",            "arguments": "{\"a\": 67317, \"b\": 164000}"          }        }      ]    },    {      "content": "231317.0",      "role": "tool",      "tool_call_id": "call_jd5esfvy2qsh9tdz23361pha"    }  ]}
# The large model returns the addition tool invocation and provides parameters
response: {  "id": "02174685631455691645265e627e21d5dcc187f9552fedf732682",  "choices": [    {      "finish_reason": "tool_calls",      "index": 0,      "logprobs": null,      "message": {        "content": "",        "role": "assistant",        "tool_calls": [          {            "id": "call_owov4sx70lmdpz7zooldnue3",            "function": {              "arguments": " {\"a\": 231317, \"b\": 1551000}",              "name": "add"            }          }        ]      }    }  ],}
# The math calculation agent obtains the result of the addition tool and calls the large model again
request: {  "tools": [    {      "type": "function",      "function": {        "name": "add",        "description": "Add two numbers.",        "parameters": {          "properties": {            "a": {              "type": "number"            },            "b": {              "type": "number"            }          },          "required": [            "a",            "b"          ],          "type": "object"        }      }    },    {      "type": "function",      "function": {        "name": "multiply",        "description": "Multiply two numbers.",        "parameters": {          "properties": {            "a": {              "type": "number"            },            "b": {              "type": "number"            }          },          "required": [            "a",            "b"          ],          "type": "object"        }      }    }  ],  "messages": [    {      "content": "You are a math expert. Always use one tool at a time.",      "role": "system"    },    {      "content": "what's the combined headcount of the FAANG companies in 2024?",      "role": "user"    },    {      "content": null,      "name": "supervisor",      "role": "assistant",      "tool_calls": [        {          "type": "function",          "id": "call_fyew4c06vn9ide7nhk6sijws",          "function": {            "name": "transfer_to_research_expert",            "arguments": "{}"          }        }      ]    },    {      "content": "Successfully transferred to research_expert",      "role": "tool",      "tool_call_id": "call_fyew4c06vn9ide7nhk6sijws"    },    {      "content": "The individual headcounts of FAANG companies in 2024 are as follows: Meta has 67,317 employees, Apple has 164,000 employees, Amazon has 1,551,000 employees, Netflix has 14,000 employees, and Alphabet has 181,269 employees. You can sum these figures to get the combined head - count.",      "name": "research_expert",      "role": "assistant"    },    {      "content": "Transferring back to supervisor",      "name": "research_expert",      "role": "assistant",      "tool_calls": [        {          "type": "function",          "id": "267ff0b0-d27b-4764-a68b-71eda788379a",          "function": {            "name": "transfer_back_to_supervisor",            "arguments": "{}"          }        }      ]    },    {      "content": "Successfully transferred back to supervisor",      "role": "tool",      "tool_call_id": "267ff0b0-d27b-4764-a68b-71eda788379a"    },    {      "content": null,      "name": "supervisor",      "role": "assistant",      "tool_calls": [        {          "type": "function",          "id": "call_z9x3nnqjvnp9hxuehejkivgn",          "function": {            "name": "transfer_to_math_expert",            "arguments": "{}"          }        }      ]    },    {      "content": "Successfully transferred to math_expert",      "role": "tool",      "tool_call_id": "call_z9x3nnqjvnp9hxuehejkivgn"    },    {      "content": null,      "name": "math_expert",      "role": "assistant",      "tool_calls": [        {          "type": "function",          "id": "call_jd5esfvy2qsh9tdz23361pha",          "function": {            "name": "add",            "arguments": "{\"a\": 67317, \"b\": 164000}"          }        }      ]    },    {      "content": "231317.0",      "role": "tool",      "tool_call_id": "call_jd5esfvy2qsh9tdz23361pha"    }  ]}
# The large model returns the addition tool invocation and provides parameters
response: {  "id": "02174685631635591645265e627e21d5dcc187f9552fedf2db39e",  "choices": [    {      "finish_reason": "tool_calls",      "index": 0,      "logprobs": null,      "message": {        "content": "",        "role": "assistant",        "tool_calls": [          {            "id": "call_w2tp827rhg290ekal228ei1c",            "function": {              "arguments": " {\"a\": 1782317, \"b\": 14000}",              "name": "add"            }          }        ]      }    }  ],}
# The math calculation agent calls the large model again
request: {  "tools": [    {      "type": "function",      "function": {        "name": "add",        "description": "Add two numbers.",        "parameters": {          "properties": {            "a": {              "type": "number"            },            "b": {              "type": "number"            }          },          "required": [            "a",            "b"          ],          "type": "object"        }      }    },    {      "type": "function",      "function": {        "name": "multiply",        "description": "Multiply two numbers.",        "parameters": {          "properties": {            "a": {              "type": "number"            },            "b": {              "type": "number"            }          },          "required": [            "a",            "b"          ],          "type": "object"        }      }    }  ],  "messages": [    {      "content": "You are a math expert. Always use one tool at a time.",      "role": "system"    },    {      "content": "what's the combined headcount of the FAANG companies in 2024?",      "role": "user"    },    {      "content": null,      "name": "supervisor",      "role": "assistant",      "tool_calls": [        {          "type": "function",          "id": "call_fyew4c06vn9ide7nhk6sijws",          "function": {            "name": "transfer_to_research_expert",            "arguments": "{}"          }        }      ]    },    {      "content": "Successfully transferred to research_expert",      "role": "tool",      "tool_call_id": "call_fyew4c06vn9ide7nhk6sijws"    },    {      "content": "The individual headcounts of FAANG companies in 2024 are as follows: Meta has 67,317 employees, Apple has 164,000 employees, Amazon has 1,551,000 employees, Netflix has 14,000 employees, and Alphabet has 181,269 employees. You can sum these figures to get the combined head - count.",      "name": "research_expert",      "role": "assistant"    },    {      "content": "Transferring back to supervisor",      "name": "research_expert",      "role": "assistant",      "tool_calls": [        {          "type": "function",          "id": "267ff0b0-d27b-4764-a68b-71eda788379a",          "function": {            "name": "transfer_back_to_supervisor",            "arguments": "{}"          }        }      ]    },    {      "content": "Successfully transferred back to supervisor",      "role": "tool",      "tool_call_id": "267ff0b0-d27b-4764-a68b-71eda788379a"    },    {      "content": null,      "name": "supervisor",      "role": "assistant",      "tool_calls": [        {          "type": "function",          "id": "call_z9x3nnqjvnp9hxuehejkivgn",          "function": {            "name": "transfer_to_math_expert",            "arguments": "{}"          }        }      ]    },    {      "content": "Successfully transferred to math_expert",      "role": "tool",      "tool_call_id": "call_z9x3nnqjvnp9hxuehejkivgn"    },    {      "content": null,      "name": "math_expert",      "role": "assistant",      "tool_calls": [        {          "type": "function",          "id": "call_jd5esfvy2qsh9tdz23361pha",          "function": {            "name": "add",            "arguments": "{\"a\": 231317, \"b\": 1551000}"          }        }      ]    },    {      "content": "1782317.0",      "role": "tool",      "tool_call_id": "call_jd5esfvy2qsh9tdz23361pha"    }  ]}
# The large model returns the addition tool invocation and provides parameters
response: {  "id": "02174685632091091645265e627e21d5dcc187f9552fedfe2a3f5",  "choices": [    {      "finish_reason": "stop",      "index": 0,      "logprobs": null,      "message": {        "content": "The combined headcount of the FAANG companies in 2024 is 1,977,586.",        "role": "assistant"      }
    }
  ],}
  1. Obtain the final result.

Leave a Comment