Implementing Parallelization Patterns
Introduction & Context
Welcome back! You've mastered sequential workflows with prompt chaining and conditional workflows with intelligent routing. Now it's time to unlock dramatic performance improvements by learning parallel processing — executing multiple independent OpenAI API calls simultaneously instead of waiting for each one to complete.
In this lesson, you'll discover how to transform workflows that take minutes into operations that complete in seconds. You'll learn the difference between sequential and parallel execution, master Ruby's built-in Thread class, and build a system that asks multiple questions to GPT at the same time.
What "Synchronous" Really Means
Before we dive into the technical details, let's clarify terms that often confuse beginners: sequential and parallel.
In everyday language, "sequential" means things happen one after another — like waiting in a queue at a bakery, where each customer is served completely before the next one steps forward. In programming, sequential execution means your code runs one operation at a time: it starts a task, waits for it to finish, then starts the next one.
Parallel execution, on the other hand, means multiple tasks run at the same time — like a bakery where several customers are being served simultaneously by different staff members. In Ruby terms, this means launching multiple threads that each handle their own OpenAI API call independently, without waiting for the others.
For OpenAI API workflows:
- Sequential requests: Call
GPTfor question 1, wait for the response, then callGPTfor question 2, wait, then question 3 — total time is the sum of all individual wait times. - Threaded parallel requests: Launch all three
GPTcalls at once usingthreads— total time is roughly equal to the slowest individual response, not the sum of all of them.
This distinction is the key insight of this lesson: when your tasks are independent of each other, running them in parallel with threads can dramatically cut your total execution time.
Sequential Requests vs. Threaded Parallel Requests in Ruby
In Ruby, the standard OpenAI::Client handles requests one at a time when called sequentially — your code waits for GPT to respond before moving on to the next call. This is simple and easy to reason about, but slow when you have multiple independent questions to ask.
Sequential approach — each request waits for the previous one to finish:
If each request takes 3 seconds, the total runtime is 9 seconds.
Threaded parallel approach — all requests fire at the same time:
If each request takes 3 seconds, the total runtime is roughly 3 seconds — because all three are running concurrently. The same OpenAI::Client is used in both cases; it's Ruby threads that enable the parallelism, not a different client type.
In summary:
- Use sequential calls for simple workflows where each step depends on the previous one.
- Use
threadswhen you have multiple independentGPTcalls that can run simultaneously, dramatically improving performance for batch or parallel tasks.
