Comprehensive Guide to Creating Multi-Agent Methods in Langgraph

Comprehensive Guide to Creating Multi-Agent Methods in Langgraph

There are five ways to create multi-agents in Langgraph:

  • Network: Each agent can communicate with all other agents, and all agents can decide which agent to call next.

  • Supervisor: Each agent can communicate with a supervisor agent, which decides which agent to call next.

  • Supervisor (tool-calling): This is a special case of the supervisor architecture where a single agent can be represented as a tool. Here, the supervisor agent calls an LLM to decide which agent to respond.

  • Hierarchical: There can be lower-level supervisors under a supervisor.

  • Custom multi-agent workflow: Each agent can only communicate with its subset of agents, the workflow is defined, and only some agents can decide which agents to call next.

Network

In a network structure, agents are defined as nodes in a graph. Each agent can communicate with all other agents and decide which agent to call next. Although this is very flexible, as the number of agents increases, it becomes difficult to determine which agent to call and what messages to pass, so the scalability of this architecture is not very good. It is recommended to avoid using this architecture in production.

Supervisor

In this architecture, agents are defined as nodes, and a supervisor node is added to determine agent calls. Conditional edges are used to decide which agent the supervisor routes to. This architecture is also suitable for running multiple agents in parallel or using a map-reduce pattern.

Supervisor (tool-calling)

This implements a ReAct-style agent with two nodes, one for the LLM and one for executing tool calls.

Hierarchical

As the number of agents in the system increases, a single supervisor cannot manage them well; it cannot keep track in complex contexts to determine which agent to call next. At this point, a hierarchical structure can be used. By creating several independent supervisor multi-agent systems and then using a top-level supervisor to manage these sub-supervisors.

Custom multi-agent workflow

In this architecture, a single agent is defined as a node, and the order of calls is predefined in a custom workflow. There are two ways to define the workflow:

  • Explicit control flow normal edges: Control flow is explicitly defined through normal graph edges, making the order of agent calls deterministic.

  • Dynamic control flow conditional edges: Using LLM to decide control flow, which can be achieved through conditional edges.

Communication Between Agents

The most important thing when building multi-agents is to figure out how agents communicate with each other.

Do agents communicate through graph states or tool calls?

Generally, agents communicate through graph states, and only in the Supervisor (tool-calling) architecture do they communicate through tool calls. By defining agents as nodes in a graph, they can be added as functions or entire subgraphs. At each step of graph execution, agent nodes receive the current state of the graph, execute the agent’s code, and then pass the updated state to the next node.

What to do if the state patterns of two agents are different?

The state patterns of some agents may differ from others. In Langgraph, there are two ways to implement this:

  • Define subgraph agents with separate state patterns. If there are no shared state keys (channels) between the subgraph and the parent graph, input-output transformations need to be added for communication.

  • Define agent node functions with a private input state pattern that differs from the overall graph state pattern. This allows passing messages of this specific state pattern.

How to communicate through a shared message list?

The most common way for agents to communicate is through a shared state channel, usually a message list. This means that there is always at least one channel (key) available in the shared state of the agents.

  • Share the complete history of thought processes: Share the complete history in a message-list-like format. The benefit of this is that it may help other agents make better inferences, improving the overall capability of the system. However, the downside is that as the number and complexity of agents increase, messages will grow rapidly.

  • Share final results: Agents keep their own historical messages but only share final results with other agents. This approach is more suitable for systems with multiple agents or more complex agents.

Leave a Comment