Designing a Durable AI Document Processing System
Consider an enterprise application that allows employees to upload invoices for automated processing. The system uses an LLM to extract invoice numbers, supplier information, dates, line items, and payment amounts. It then validates the extracted information against business rules and stores the verified results in a database.
A single invoice may require several AI calls and take several minutes to process. During execution, an LLM provider may time out, a worker may crash, or the application may receive the same invoice submission multiple times. Restarting the entire process after every failure would waste model tokens and processing capacity. More importantly, repeating completed operations could create duplicate invoice records.
The challenge is to design a workflow that can execute each processing stage independently, preserve completed work, and resume unfinished operations without producing duplicate business results.
1. Designing the Workflow Execution Architecture
Start by separating the document submission process from its actual execution. When an employee uploads an invoice, the API should validate the request, store the original document in object storage, and create a workflow execution record in a database. Instead of processing the document within the HTTP request, the application schedules the work for background execution.
A message queue distributes processing tasks to available workers.