Back to AI Recipes

Parallelization

Workflow

Workflow

Run multiple LLM calls in parallel to reduce total latency.

Summary

The Parallelization pattern distributes tasks across multiple LLM calls that can run simultaneously. This approach is particularly effective for processing multiple independent items or when different aspects of a task can be handled concurrently, significantly reducing overall processing time.

How it works

  1. Task Decomposition: Break the problem into independent subtasks
  2. Concurrent Execution: Dispatch all subtasks to LLM instances simultaneously
  3. Result Aggregation: Collect and merge outputs from all calls
  4. Synthesis: Combine results into final unified output

Key trade-offs

  • Latency: Reduced for batch workloads, increased for single items
  • Cost: Higher per-request but potentially lower total time
  • Consistency: May vary across parallel calls
  • Complexity: Requires result aggregation logic

Use cases

  • Batch processing of multiple documents or data points simultaneously
  • Multi-aspect analysis where different perspectives can be evaluated in parallel
  • Concurrent generation of multiple variations or alternatives
  • Distributed content processing for large-scale data analysis
Code View

Parallelization Implementation

// Parallelization recipe using OpenAI
// Install: bun add openai

import OpenAI from "openai";

async function main() {
  const input = "Add your prompt here.";
  const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
  const system = "You are a senior AI engineer and technical writer. Apply the agent recipe to structure the reasoning and produce a useful result. Recipe: Parallelization. Description: Run multiple LLM calls in parallel to reduce total latency. Focus: Workflow Provide actionable, implementation-ready guidance.";
  const user = `Request: ${input}`;

  const openaiResponse = await openai.chat.completions.create({
    model: "gpt-4o-mini",
    messages: [
      { role: "system", content: system },
      { role: "user", content: user },
    ],
  });

  const openaiText = openaiResponse.choices[0]?.message?.content?.trim() ?? "";

  console.log(openaiText);
}

main().catch((error) => {
  console.error(error);
  process.exitCode = 1;
});