Multi-Agent Systems
Monolithic Agents often struggle with overly complex tasks. By decomposing tasks and letting multiple domain-specific Agents collaborate, we can build more powerful and stable systems. LangGraphGo's graph structure is naturally suited for orchestrating multi-agent systems.
1. Supervisor Pattern
Background & Functionality
This is the most classic multi-agent organization form. A central "Supervisor" Agent acts as a project manager. It receives user requests, decomposes tasks, and assigns them to subordinate "Worker" Agents (such as Researchers, Coders, Testers). Workers report back to the Supervisor upon completion, and the Supervisor aggregates results or requests rework.
Implementation Principle
In LangGraphGo, Supervisor is a special node (usually an LLM), and its output determines the next activated node (Worker). This is implemented via conditional edges. All Worker nodes' output edges point back to the Supervisor, forming a star topology.
Code Showcase
// 1. Define Worker Nodes
g.AddNode("Researcher", researcherAgent)
g.AddNode("Coder", coderAgent)
// 2. Define Supervisor Node
// Use prebuilt CreateSupervisor to simplify creation
supervisor := prebuilt.CreateSupervisor(
model,
[]string{"Researcher", "Coder"},
prebuilt.WithSupervisorSystemMessage("You are a tech lead, responsible for coordinating research and coding..."),
)
g.AddNode("Supervisor", supervisor)
// 3. Define Routing Logic
// Supervisor output is an instruction telling the graph which Worker to go to next
g.AddConditionalEdge("Supervisor", func(ctx context.Context, state interface{}) string {
return state.(AgentState).Next // e.g., return "Researcher"
})
// 4. Define Loop
// Workers always return to Supervisor after completion
g.AddEdge("Researcher", "Supervisor")
g.AddEdge("Coder", "Supervisor")
2. Hierarchical Teams
Background & Functionality
When team size grows, flat Supervisor pattern hits bottlenecks. Hierarchical Teams introduce a hierarchy: Supervisor manages subgraphs, and each subgraph itself might be a small group managed by a Supervisor.
For example, a "Dev Team" subgraph might contain "Backend" and "Frontend" Agents. The top-level Supervisor only needs to interact with "Dev Team" without caring about specific backend or frontend details.
Implementation Principle
Leveraging LangGraphGo's subgraph feature. We encapsulate each team as a `CompiledGraph` and add it as a node to the upper-level graph. To the upper-level graph, the subgraph is just a normal node.
Code Showcase
// 1. Build Research Team Subgraph
researchGraph := graph.NewStateGraph(...)
// ... Add researcher agents ...
researchTeam := researchGraph.Compile()
// 2. Build Coding Team Subgraph
codingGraph := graph.NewStateGraph(...)
// ... Add coder agents ...
codingTeam := codingGraph.Compile()
// 3. Build Top-level Graph
topGraph := graph.NewStateGraph(...)
topGraph.AddNode("ResearchTeam", researchTeam)
topGraph.AddNode("CodingTeam", codingTeam)
topGraph.AddNode("TopSupervisor", topSupervisor)
// ... Define top-level routing ...
3. Network / Swarm
Background & Functionality
In decentralized network mode, there is no single manager. Each Agent can hand over tasks to other Agents as needed. This is similar to microservices architecture or human social networks.
Implementation Principle
Each Agent node is equipped with routing logic (conditional edges). When Agent A thinks the task is beyond its capability or needs Agent B's assistance, it outputs a special signal triggering a conditional edge jump to Agent B.