Overview
Multi-agent systems allow you to break down complex tasks into specialized sub-agents, each handling a specific domain or responsibility. This example demonstrates:- Creating specialized sub-agents with focused instructions
- Coordinating agents through a root agent
- Sharing state between agents
- Transferring control between agents
Multi-agent systems are ideal for complex workflows like customer service, order processing, content moderation, or any task that benefits from specialized expertise.
Restaurant Order System Example
This example builds a restaurant ordering system with three specialized agents:- Customer Analyzer: Extracts dietary restrictions, preferences, and budget
- Menu Validator: Checks item availability and suggests alternatives
- Order Finalizer: Confirms and processes the final order
Complete Example
Notice how each agent uses
outputKey to store its results in the shared state. The next agent can reference this data using {outputKey} in its instructions.import { AgentBuilder } from "@iqai/adk";
import { getCustomerAnalyzerAgent } from "./customer-analyzer/agent";
import { getMenuValidatorAgent } from "./menu-validator/agent";
import { getOrderFinalizerAgent } from "./order-finalizer/agent";
export function getRootAgent() {
const customerAnalyzer = getCustomerAnalyzerAgent();
const menuValidator = getMenuValidatorAgent();
const orderFinalizer = getOrderFinalizerAgent();
const initialState = {
customer_preferences: "",
menu_validation: "",
};
return AgentBuilder.create("restaurant_order_system")
.withModel(process.env.LLM_MODEL || "gemini-3-flash-preview")
.withSubAgents([customerAnalyzer, menuValidator, orderFinalizer])
.withQuickSession({ state: initialState })
.build();
}
import { ask } from "../utils";
import { getRootAgent } from "./agents/agent";
async function main() {
const { runner } = await getRootAgent();
const questions = [
"I'd like something vegetarian, not too spicy, around $20. Maybe a salad or pasta?",
"Can I get a burger with fries?",
"What desserts do you have?",
];
for (const question of questions) {
await ask(runner, question);
}
}
main().catch(console.error);
Expected Output
Key Concepts
Agent Coordination
The root agent automatically:- Determines which sub-agent should handle each request
- Transfers control to the appropriate specialist
- Manages shared state between agents
- Returns responses to the user
State Sharing with Output Keys
Sub-agents share data through theoutputKey mechanism:
Folder Structure for Multi-Agent Systems
Each sub-agent can have its own
tools.ts, plugins.ts, and other files. This keeps complex systems organized and maintainable.Advanced Patterns
Sequential vs Parallel Agents
This example shows sequential agent execution (one after another). For parallel execution:Conditional Agent Routing
The root agent intelligently routes requests:Use Cases
Customer Support
Route to specialists: billing, technical, returns
Content Moderation
Analyze content safety, compliance, quality
Data Processing
Extract, validate, transform, and load data
Research Tasks
Gather, verify, synthesize information
Next Steps
Database Sessions
Persist multi-agent conversations across sessions
Memory Systems
Add long-term memory to your agents