Parallel Execution
Run multiple AI agents concurrently using standard JavaScript/TypeScript Promise.all to compare or combine responses.
Overview
Mozaik agents are designed to work seamlessly with JavaScript's native concurrency patterns. Run multiple agents in parallel to speed up execution, compare outputs from different models, or implement voting systems.
Basic Parallel Execution
Use Promise.all to run multiple agents simultaneously:
parallel-basic.ts
1import { MozaikAgent, MozaikRequest } from '@mozaik-ai/core'23const openaiRequest: MozaikRequest = { model: 'gpt-5' }4const anthropicRequest: MozaikRequest = { model: 'claude-sonnet-4.5' }56const openaiAgent = new MozaikAgent(openaiRequest)7const anthropicAgent = new MozaikAgent(anthropicRequest)89const task = 'What are the key differences between TypeScript and JavaScript?'1011// Run both agents in parallel12const [openaiResponse, anthropicResponse] = await Promise.all([13 openaiAgent.act(task),14 anthropicAgent.act(task)15])1617console.log('OpenAI:', openaiResponse)18console.log('Anthropic:', anthropicResponse)
Comparing Results
Use parallel execution to get multiple perspectives and choose the best one:
parallel-compare.ts
1import { MozaikAgent, MozaikRequest } from '@mozaik-ai/core'23async function getBestResponse(task: string) {4 const models = ['gpt-5', 'claude-sonnet-4.5', 'gpt-5-mini']56 // Create agents for each model7 const agents = models.map(model => new MozaikAgent({ model }))89 // Run all in parallel10 const responses = await Promise.all(11 agents.map(agent => agent.act(task))12 )1314 // Use another agent to judge the responses15 const judge = new MozaikAgent({ model: 'claude-opus-4.5' })16 const verdict = await judge.act(`17 Here are responses to the question: "${task}"1819 Response 1: ${responses[0]}20 Response 2: ${responses[1]}21 Response 3: ${responses[2]}2223 Which response is best and why?24 `)2526 return verdict27}
Batch Processing
Process multiple items concurrently with controlled parallelism:
parallel-batch.ts
1import { MozaikAgent, MozaikRequest } from '@mozaik-ai/core'23async function processBatch(items: string[], concurrency: number = 5) {4 const results: string[] = []5 const agent = new MozaikAgent({ model: 'gpt-5-mini' })67 // Process in batches8 for (let i = 0; i < items.length; i += concurrency) {9 const batch = items.slice(i, i + concurrency)1011 const batchResults = await Promise.all(12 batch.map(item => agent.act(`Summarize: ${item}`))13 )1415 results.push(...batchResults)16 }1718 return results19}2021// Process 100 items, 5 at a time22const items = ['article1...', 'article2...', /* ... */]23const summaries = await processBatch(items, 5)
Tip
Control concurrency to avoid rate limits and manage costs. Most providers have request-per-minute limits.
Error Handling
Use Promise.allSettled when you need results even if some fail:
parallel-errors.ts
1const results = await Promise.allSettled([2 agent1.act(task),3 agent2.act(task),4 agent3.act(task)5])67// Filter successful results8const successful = results9 .filter((r): r is PromiseFulfilledResult<string> => r.status === 'fulfilled')10 .map(r => r.value)1112// Log failures13results14 .filter((r): r is PromiseRejectedResult => r.status === 'rejected')15 .forEach(r => console.error('Failed:', r.reason))