AI Engineering
Optimizing Large-Scale Processing with Celery & OpenAI API
How I split long transcriptions into chunks and processed them in parallel with Celery chords, taking processing from around 30 seconds to under 10.
In the world of transcription and AI-based services, handling large amounts of data efficiently is a key challenge. When working on a project that required processing large conversations using the OpenAI API, I ran into several bottlenecks — one of the most significant being the inefficiencies that arose when attempting to pass entire transcriptions to the API at once. It led to high token usage, slower response times, and compromised accuracy.
In this article, I'll walk you through how I approached this challenge and how Celery and the OpenAI API helped me build a robust and scalable solution.
The Problem: Overwhelmed by Large Conversations
When dealing with long conversations, feeding the entire transcription at once to the OpenAI API didn't give the best results. The transcription was often too large to process in a single request, leading to:
- High token usage: The OpenAI API is token-based, and passing huge chunks of text meant unnecessary cost and inefficiency.
- Slower responses: Processing large data in a single API call created delays, which impacted the overall performance.
- Reduced accuracy: OpenAI has context limits, and trying to pass a lengthy conversation as one single prompt led to truncated responses or a loss of context.
Clearly, this wasn't the optimal approach.
The Solution: Chunking & Parallel Processing
After some brainstorming, we decided that the best solution would be to chunk the transcription into smaller parts and process them in parallel. This would allow us to break down the problem into smaller, manageable tasks and process them efficiently. However, there was a new challenge on the horizon:
How do we gather these chunks back together in the right order once they've been processed?
Enter Celery: Asynchronous Task Management
To solve the parallel processing challenge, we decided to use Celery — a powerful asynchronous task queue/job queue based on distributed message passing.
Why Celery?
Celery is a great choice for handling parallel processing in Python. It allows you to distribute tasks across multiple workers, which helps scale your application. We also needed to ensure that tasks could run asynchronously, without blocking the main process, so Celery became the obvious solution.
The Core Workflow
To implement this, we chunked the transcription into smaller pieces and used Celery's chord to process them in parallel. A chord in Celery is a powerful construct that allows you to execute a set of tasks asynchronously and then trigger a final callback task once all the tasks in the chord are completed.
Here's the code snippet that showcases how we set it up:
chord(
(process_chunk_cleanup.s(chunk, content, i)
for i, chunk in enumerate(chunks)),
gather_chunks.s(conversation.id)
).apply_async()Breaking it Down
process_chunk_cleanup.s(): This task takes each chunk of the conversation and processes it (e.g., passes it to the OpenAI API, cleans up the response, etc.).gather_chunks.s(): This is the final task that gathers all processed chunks and ensures they are put back together in the correct order.
Why Not group?
You may wonder why we didn't use Celery's group on its own. A group runs tasks in parallel, but something still has to wait for all of them to finish and then assemble the output. A chord is a group plus a callback: Celery runs the callback once every task in the group has completed and passes it the results in the same order the tasks were defined. That gave us parallel processing and a single, reliable place to put the chunks back together.
One requirement to know about: chords need a result backend, because Celery has to track when every task in the group has finished.
The Results: Speed, Accuracy, and Efficiency
By using Celery chords for parallel processing, we achieved the following:
- Faster processing: Each chunk was processed simultaneously, reducing overall time. A conversation of about 20 to 25 minutes used to take around 30 seconds to process. It came down to under 10 seconds.
- Improved accuracy: We could maintain more context by processing smaller chunks, and the final output was more coherent.
- Cost-efficiency: With smaller API calls and reduced token usage, we kept costs manageable.
This approach not only solved the original problem but also made the entire pipeline more scalable and reliable.
Key Takeaways
- Chunking and parallelism: Splitting large tasks into smaller chunks and processing them in parallel is a great way to handle large-scale API calls.
- Celery chords: For tasks that need to be processed in parallel with a final callback, Celery's chord is a powerful tool that simplifies complex workflows.
- Ensure correct sequencing: It's not just about parallel execution — it's about bringing the results back together in the correct order.
Final Thoughts
This approach of chunking and parallel processing is just one way to handle large-scale AI tasks efficiently. Whether you're working with transcription, image processing, or any other large dataset, breaking tasks down into smaller parts and processing them concurrently can drastically improve performance and cost-effectiveness.
Have you faced similar challenges in your own product? Tell me about it.
Originally published on Medium on 18 February 2025.
Have a product to build or a system to fix?
Tell me about your project, a technical problem you are stuck on, or an ongoing engineering need. I will reply with how I can help and what a sensible first step looks like.
Or email hello@sarmadkhalique.com