The Multi-Agent System is one of the core features of the trpc-agent-go framework, allowing you to create complex systems composed of multiple specialized Agents. These Agents can collaborate in different ways to implement various application scenarios from simple to complex.
Overview
The Multi-Agent System is built on the SubAgent concept, implementing various collaboration patterns through the WithSubAgents option:
Basic Concepts
SubAgent - Specialized Agents configured through the WithSubAgents option, serving as the foundation for building complex collaboration patterns
Parallel Agent (ParallelAgent) - Uses SubAgents to process different aspects of the same input simultaneously
Cycle Agent (CycleAgent) - Uses SubAgents to iterate in loops until specific conditions are met
Auxiliary Functions
Agent Tool (AgentTool) - Wraps Agents as tools for other Agents to call
Agent Transfer - Implements task delegation between Agents through the transfer_to_agent tool
SubAgent Basics
SubAgent is the core concept of the Multi-Agent System, implemented through the WithSubAgents option. It allows you to combine multiple specialized Agents to build complex collaboration patterns.
Role of SubAgent
Specialized Division of Labor: Each SubAgent focuses on specific domains or task types
Modular Design: Decomposes complex systems into manageable components
Flexible Combination: Can combine different SubAgents as needed
Unified Interface: All collaboration patterns are based on the same WithSubAgents mechanism
import("trpc.group/trpc-go/trpc-agent-go/agent""trpc.group/trpc-go/trpc-agent-go/agent/llmagent")// Create SubAgent.mathAgent:=llmagent.New("math-agent",llmagent.WithModel(modelInstance),llmagent.WithDescription("Handles mathematical calculations and numerical problems"),llmagent.WithInstruction("You are a mathematics expert, focusing on mathematical operations and numerical reasoning..."),)weatherAgent:=llmagent.New("weather-agent",llmagent.WithModel(modelInstance),llmagent.WithDescription("Provides weather information and suggestions"),llmagent.WithInstruction("You are a weather expert, providing weather analysis and activity suggestions..."),)// Use WithSubAgents option to configure SubAgent.mainAgent:=llmagent.New("coordinator-agent",llmagent.WithModel(modelInstance),llmagent.WithDescription("Coordinator Agent responsible for task delegation"),llmagent.WithInstruction("You are a coordinator, analyzing user requests and delegating to appropriate experts..."),llmagent.WithSubAgents([]agent.Agent{mathAgent,weatherAgent}),)
Core Collaboration Patterns
All collaboration patterns are based on the SubAgent concept, implemented through different execution strategies:
Chain Agent (ChainAgent)
Chain Agent uses SubAgents connected sequentially to form processing pipelines. Each SubAgent focuses on specific tasks and passes results to the next SubAgent.
Use Cases
Content Creation Workflow: Planning → Research → Writing
Problem Solving Workflow: Analysis → Design → Implementation
Data Processing Workflow: Collection → Cleaning → Analysis
🔗 Multi-Agent Chain Demo
Chain Flow: Planning → Research → Writing
==================================================
👤 User: Explain the benefits of renewable energy
📋 Planning Agent: I will create a structured analysis plan...
🔍 Research Agent:
🔧 Using tools:
• web_search (ID: call_123)
🔄 Executing...
✅ Tool result: Latest renewable energy data...
✍️ Writing Agent: Based on planning and research:
[Structured comprehensive response]
Parallel Agent (ParallelAgent)
Parallel Agent uses SubAgents to process different aspects of the same input simultaneously, providing multi-perspective analysis.
🔄 Multi-Agent Cycle Demo
Max iterations: 3
Cycle: Generate → Evaluate → Improve → Repeat
==================================================
👤 User: Write a short joke
🤖 Cycle Response:
🤖 Generate Agent: Why don't skeletons fight each other?
Because they don't have the guts!
👀 Evaluate Agent:
🔧 Using tools:
• record_score (ID: call_123)
🔄 Executing...
✅ Quality score: 75/100
⚠️ Needs improvement - continue iteration
🔄 **2nd Iteration**
🤖 Generate Agent: This is an improved version with a new twist:
**Why do skeletons never win arguments?**
Because they always lose their backbone halfway through!
👀 Evaluate Agent:
🔧 Using tools:
• record_score (ID: call_456)
🔄 Executing...
✅ Quality score: 85/100
🎉 Quality threshold reached - cycle completed
🏁 Cycle completed after 2 iterations
Auxiliary Functions
Agent Tool (AgentTool)
Agent Tool is an important foundational function for building complex multi-agent systems. It allows you to wrap any Agent as a callable tool for use by other Agents or applications.
Use Cases
Specialized Delegation: Different Agents handle specific types of tasks
Tool Integration: Agents can be integrated as tools into larger systems
Modular Design: Reusable Agent components can be combined together
import("trpc.group/trpc-go/trpc-agent-go/agent/llmagent""trpc.group/trpc-go/trpc-agent-go/tool"agenttool"trpc.group/trpc-go/trpc-agent-go/tool/agent""trpc.group/trpc-go/trpc-agent-go/tool/function")// Create specialized Agent.mathAgent:=llmagent.New("math-specialist",llmagent.WithModel(modelInstance),llmagent.WithDescription("Agent specialized in mathematical operations"),llmagent.WithInstruction("You are a mathematics expert, focusing on mathematical operations, calculations and numerical reasoning..."),llmagent.WithTools([]tool.Tool{calculatorTool}),)// Wrap Agent as tool.agentTool:=agenttool.NewTool(mathAgent,agenttool.WithSkipSummarization(false),)// Use Agent tool in main Agent.mainAgent:=llmagent.New("chat-assistant",llmagent.WithTools([]tool.Tool{timeTool,agentTool}),)
🚀 Agent Tool Example
Model: deepseek-chat
Available tools: current_time, math-specialist
==================================================
👤 User: Calculate 923476 * 273472354
🤖 Assistant: I will use the math specialist Agent to calculate this result.
🔧 Tool call initiated:
• math-specialist (ID: call_0_e53a77e9-c994-4421-bfc3-f63fe85678a1)
Parameters: {"request":"Calculate 923476 multiplied by 273472354"}
🔄 Executing tool...
✅ Tool response (ID: call_0_e53a77e9-c994-4421-bfc3-f63fe85678a1):
"The result of calculating 923,476 multiplied by 273,472,354 is:
\[
923,\!476 \times 273,\!472,\!354 = 252,\!545,\!155,\!582,\!504
\]"
✅ Tool execution completed.
Agent Transfer
Agent Transfer implements task delegation between Agents through the transfer_to_agent tool, allowing the main Agent to automatically select appropriate SubAgents based on task type.
Use Cases
Task Classification: Automatically select appropriate SubAgents based on user requests
Intelligent Routing: Route complex tasks to the most suitable handlers
Specialized Processing: Each SubAgent focuses on specific domains
Seamless Switching: Seamlessly switch between SubAgents while maintaining conversation continuity
import("trpc.group/trpc-go/trpc-agent-go/agent""trpc.group/trpc-go/trpc-agent-go/agent/llmagent""trpc.group/trpc-go/trpc-agent-go/tool""trpc.group/trpc-go/trpc-agent-go/tool/function")// Create SubAgent.mathAgent:=llmagent.New("math-agent",llmagent.WithModel(modelInstance),llmagent.WithDescription("Handles mathematical calculations and numerical problems"),llmagent.WithInstruction("You are a mathematics expert, focusing on mathematical operations and numerical reasoning..."),llmagent.WithTools([]tool.Tool{calculatorTool}),)weatherAgent:=llmagent.New("weather-agent",llmagent.WithModel(modelInstance),llmagent.WithDescription("Provides weather information and suggestions"),llmagent.WithInstruction("You are a weather expert, providing weather analysis and activity suggestions..."),llmagent.WithTools([]tool.Tool{weatherTool}),)// Create coordinator Agent, use WithSubAgents to configure SubAgent.coordinatorAgent:=llmagent.New("coordinator-agent",llmagent.WithModel(modelInstance),llmagent.WithDescription("Coordinator Agent responsible for task delegation"),llmagent.WithInstruction("You are a coordinator, analyzing user requests and delegating to appropriate experts..."),llmagent.WithSubAgents([]agent.Agent{mathAgent,weatherAgent}),)
🔄 Agent Transfer Demo
Available SubAgents: math-agent, weather-agent, research-agent
==================================================
👤 User: Calculate compound interest, principal $5000, annual rate 6%, term 8 years
🎯 Coordinator: I will delegate this task to our mathematics expert for accurate calculation.
🔄 Initiating delegation...
🔄 Transfer event: Transferring control to Agent: math-agent
🧮 Math Expert: I will help you calculate compound interest step by step.
🔧 🧮 Executing tool:
• calculate ({"operation":"power","a":1.06,"b":8})
✅ Tool completed
🔧 🧮 Executing tool:
• calculate ({"operation":"multiply","a":5000,"b":1.593})
✅ Tool completed
Compound Interest Calculation Result:
- Principal: $5,000
- Annual Rate: 6%
- Term: 8 years
- Result: $7,969.24 (interest approximately $2,969.24)
Environment Variable Configuration
All multi-agent examples require the following environment variables: