Technical
The Agent Loop Pattern I Use for Document Processing
Document processing is the boring AI use case that actually makes money. Most clients I work with have a pile of PDFs they need summarized, classified, or extracted. I use the same agent loop pattern for every one of these projects. Here is the structure and why it beats a single giant prompt every time.
The Loop Shape
- Classify the document type
- Route to a specialized extractor prompt
- Validate output against a schema
- If validation fails, retry with the failure as context
- Emit structured data to the next pipeline stage
Each step is small, testable, and replaceable independently. When accuracy drops in step 2, you do not have to touch step 1 or step 3.
The Classifier Is Cheap
Small, fast model. One shot. A label. This is the cheapest step and it unlocks everything downstream. When the classifier is wrong, the whole pipeline is wrong, so it gets the most aggressive testing.
The Extractor Is Specialized
Never reuse one generic extractor across document types. Write a focused prompt per type. The accuracy gain is huge and the cost difference is negligible once you have the classifier routing properly. Generic extractors try to be everything to every document and end up being mediocre for all of them.
def process(doc):
doc_type = classify(doc)
extractor = EXTRACTORS[doc_type]
for attempt in range(3):
result = extractor(doc)
if schema.validate(result):
return result
raise ValueError('could not extract')Why the Retry Matters
First-pass failures drop by half when you feed the validation error back into the prompt. That single retry step is the highest leverage part of the whole pipeline. Most implementations skip it because it feels like cheating. It is not cheating. It is how reliable systems work.
What to Log
Every decision. Every retry. Every schema failure. This is the data that makes your pipeline better next month. Without it, you are guessing. With it, you can pull the ten worst-performing document types and fix them specifically rather than tuning the whole system.
The Business Case
Clients pay for reliability, not cleverness. A boring pipeline that handles 99 percent of documents well is worth more than a clever pipeline that handles 80 percent of documents brilliantly. The loop pattern leans into reliable and boring, which is exactly what clients want for this kind of work.
RELATED READING
The Consulting Shift I Am Making In Year Two
After a year of writing and building, my consulting practice is changing shape. Shorter engagements. Sharper outcomes.
ReadThe Frontend Shift: Shipping Less JavaScript In Year Two
A year ago I reached for Next.js for everything. This year I often reach for nothing.
ReadThe Serverless Lesson I Would Write On A Sticky Note
After a year of shipping serverless projects, one rule explains most of the wins and all of the losses.
Read