Scenario 1: Research Task Decomposition
Business Context
Building an "AI News Weekly" feature: every Monday morning, automatically generate an AI news digest.
This task requires multiple specialized agents:
- News scraping
- Content analysis
- Email generation
- Trend tracking
The correct approach is one supervisor agent coordinating multiple specialized agents.
Complete Code Example
import { spawn_subagent, agent_send_message } from '@markus/core';
interface WorkerRole {
name: string;
task: string;
allowed_tools?: string[];
}
const workerRoles: WorkerRole[] = [
{ name: 'news-collector', task: 'Fetch latest AI news', allowed_tools: ['web_fetch'] },
{ name: 'summary-generator', task: 'Generate summaries', allowed_tools: [] },
{ name: 'email-generator', task: 'Generate email content', allowed_tools: [] },
{ name: 'trend-analyzer', task: 'Analyze trends', allowed_tools: [] }
];
async function runWeeklyNewsletter() {
const subagentIds: string[] = [];
// Spawn specialized agents
for (const role of workerRoles) {
const agentId = await spawn_subagent({
task: role.task,
allowed_tools: role.allowed_tools
});
subagentIds.push(agentId);
}
// Distribute tasks
for (const [i, role] of workerRoles.entries()) {
await agent_send_message({
agent_id: subagentIds[i],
message: { type: 'task_delegate', content: role.task }
});
}
// Wait for results and aggregate
const results = await waitForWorkers(subagentIds);
return aggregateNewsletter(results);
}
Scenario 2: Code Review Pipeline
Parallel reviews with three reviewer types: code quality, security, and documentation.
async function reviewPullRequest(prUrl: string) {
const reviewerTypes = ['code_quality', 'security', 'documentation'];
const reviewerIds = [];
// Spawn reviewer pool
for (const type of reviewerTypes) {
const agentId = await spawn_subagent({
task: `Review for ${type}`
});
reviewerIds.push(agentId);
}
// Distribute parallel reviews
const results = await Promise.all(
reviewerIds.map(id => agent_send_message({
agent_id: id,
message: { type: 'task_delegate', content: `Review PR: ${prUrl}` },
wait_for_reply: true
}))
);
return { prUrl, reviews: results };
}
Scenario 3: Cross-Team Resource Coordination
async function requestGPUResources(teamId: string, gpuCount: number) {
const response = await agent_send_message({
agent_id: 'gpu-pool-manager',
message: {
type: 'resource_request',
content: { resource_type: 'gpu', quantity: gpuCount, team_id: teamId }
},
wait_for_reply: true
});
return { allocated: response?.allocated ?? 0, status: response?.status };
}
Scenario 4: Long-Running Process Tracking
async function trackTask(correlationId: string, maxWaitMinutes = 120) {
while (Date.now() - startTime < maxWaitMinutes * 60 * 1000) {
const response = await agent_send_message({
agent_id: 'worker-pool',
message: { type: 'status_sync', correlation_id: correlationId },
wait_for_reply: true
});
if (response?.status === 'completed') return response;
await new Promise(r => setTimeout(r, 30000)); // 30s polling
}
}
Best Practices Summary
- Use spawn_subagent for on-demand, short-lived agents
- Use task_delegate for structured task distribution with priority and deadlines
- Use status_sync for progress tracking and timeout handling
- Always implement correlation IDs for tracking across failures
Conclusion
These four patterns are production-ready and can be adapted to your specific use cases. The A2A protocol isn't just theory — it's the foundation of real multi-agent systems. Start experimenting today!
Series Index
- s01-s04: Previous articles in the A2A Protocol Series
- s05: This article — Real-World Cases: Four Real Collaboration Scenarios