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'
2
3const openaiRequest: MozaikRequest = { model: 'gpt-5' }
4const anthropicRequest: MozaikRequest = { model: 'claude-sonnet-4.5' }
5
6const openaiAgent = new MozaikAgent(openaiRequest)
7const anthropicAgent = new MozaikAgent(anthropicRequest)
8
9const task = 'What are the key differences between TypeScript and JavaScript?'
10
11// Run both agents in parallel
12const [openaiResponse, anthropicResponse] = await Promise.all([
13 openaiAgent.act(task),
14 anthropicAgent.act(task)
15])
16
17console.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'
2
3async function getBestResponse(task: string) {
4 const models = ['gpt-5', 'claude-sonnet-4.5', 'gpt-5-mini']
5
6 // Create agents for each model
7 const agents = models.map(model => new MozaikAgent({ model }))
8
9 // Run all in parallel
10 const responses = await Promise.all(
11 agents.map(agent => agent.act(task))
12 )
13
14 // Use another agent to judge the responses
15 const judge = new MozaikAgent({ model: 'claude-opus-4.5' })
16 const verdict = await judge.act(`
17 Here are responses to the question: "${task}"
18
19 Response 1: ${responses[0]}
20 Response 2: ${responses[1]}
21 Response 3: ${responses[2]}
22
23 Which response is best and why?
24 `)
25
26 return verdict
27}

Batch Processing

Process multiple items concurrently with controlled parallelism:

parallel-batch.ts
1import { MozaikAgent, MozaikRequest } from '@mozaik-ai/core'
2
3async function processBatch(items: string[], concurrency: number = 5) {
4 const results: string[] = []
5 const agent = new MozaikAgent({ model: 'gpt-5-mini' })
6
7 // Process in batches
8 for (let i = 0; i < items.length; i += concurrency) {
9 const batch = items.slice(i, i + concurrency)
10
11 const batchResults = await Promise.all(
12 batch.map(item => agent.act(`Summarize: ${item}`))
13 )
14
15 results.push(...batchResults)
16 }
17
18 return results
19}
20
21// Process 100 items, 5 at a time
22const 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])
6
7// Filter successful results
8const successful = results
9 .filter((r): r is PromiseFulfilledResult<string> => r.status === 'fulfilled')
10 .map(r => r.value)
11
12// Log failures
13results
14 .filter((r): r is PromiseRejectedResult => r.status === 'rejected')
15 .forEach(r => console.error('Failed:', r.reason))

Next Steps