0
0
LangChainframework~5 mins

Multi-agent graphs in LangChain

Choose your learning style9 modes available
Introduction

Multi-agent graphs help you organize and connect different agents so they can work together smoothly. It makes managing many agents easier by showing how they relate and share information.

When you have several agents that need to collaborate on a task.
When you want to visualize how different agents communicate or depend on each other.
When you want to manage complex workflows involving multiple AI agents.
When you want to track the flow of information between agents.
When building systems where agents have different roles but must work as a team.
Syntax
LangChain
from langchain.graphs import MultiAgentGraph

# Create a multi-agent graph
multi_agent_graph = MultiAgentGraph()

# Add agents
multi_agent_graph.add_agent(name="agent1", agent=agent1_instance)
multi_agent_graph.add_agent(name="agent2", agent=agent2_instance)

# Connect agents
multi_agent_graph.add_edge(from_agent="agent1", to_agent="agent2", description="passes data")

# Run or visualize the graph
multi_agent_graph.run()
multi_agent_graph.visualize()

You create a MultiAgentGraph object to start.

Add agents by giving each a unique name and the agent instance.

Examples
This shows an empty graph with no agents.
LangChain
multi_agent_graph = MultiAgentGraph()

# No agents added yet
print(len(multi_agent_graph.agents))  # Output: 0
Graph with one agent added.
LangChain
multi_agent_graph.add_agent(name="agent1", agent=agent1_instance)
print(len(multi_agent_graph.agents))  # Output: 1
Connects two agents with a directional edge showing communication.
LangChain
multi_agent_graph.add_agent(name="agent2", agent=agent2_instance)
multi_agent_graph.add_edge(from_agent="agent1", to_agent="agent2", description="sends message")
Shows a visual graph of agents and their connections.
LangChain
multi_agent_graph.visualize()
Sample Program

This program creates two simple agents, adds them to a multi-agent graph, connects them, and shows how data flows from one to the other. It prints what each agent outputs.

LangChain
from langchain.agents import Agent
from langchain.graphs import MultiAgentGraph

# Define two simple agents
class SimpleAgent(Agent):
    def __init__(self, name):
        self.name = name
    def run(self, input_text):
        return f"{self.name} received: {input_text}"

# Create agent instances
agent1 = SimpleAgent("Agent One")
agent2 = SimpleAgent("Agent Two")

# Create a multi-agent graph
multi_agent_graph = MultiAgentGraph()

# Add agents to the graph
multi_agent_graph.add_agent(name="agent1", agent=agent1)
multi_agent_graph.add_agent(name="agent2", agent=agent2)

# Connect agent1 to agent2
multi_agent_graph.add_edge(from_agent="agent1", to_agent="agent2", description="forwards message")

# Simulate running agent1 and passing output to agent2
output1 = agent1.run("Hello")
output2 = agent2.run(output1)

# Print outputs
print("Output from agent1:", output1)
print("Output from agent2:", output2)

# Visualize the graph (this will open a window or save a file depending on environment)
multi_agent_graph.visualize()
OutputSuccess
Important Notes

The time complexity to add an agent or edge is usually O(1).

Visualizing large graphs may slow down your program.

Common mistake: forgetting to connect agents, so they don't communicate.

Use multi-agent graphs when you want clear structure and flow between agents instead of isolated agents.

Summary

Multi-agent graphs organize multiple agents and their connections.

They help manage communication and workflows between agents.

Adding agents and edges is simple and lets you visualize the system.