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.
Multi-agent graphs in 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.
multi_agent_graph = MultiAgentGraph() # No agents added yet print(len(multi_agent_graph.agents)) # Output: 0
multi_agent_graph.add_agent(name="agent1", agent=agent1_instance) print(len(multi_agent_graph.agents)) # Output: 1
multi_agent_graph.add_agent(name="agent2", agent=agent2_instance) multi_agent_graph.add_edge(from_agent="agent1", to_agent="agent2", description="sends message")
multi_agent_graph.visualize()
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.
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()
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.
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.