How to Automate Document Processing with an LLM

Document processing — extracting structured data from PDFs, Word files, invoices, contracts, and forms — is one of the highest-value and most reliable applications of LLMs for business automation. A document processing pipeline takes an unstructured document as input and returns structured data (JSON, CSV, database records) as output, replacing hours of manual data entry with seconds of automated extraction. This guide covers the practical approaches, which tools handle different document types best, and how to build a reliable extraction pipeline.

What LLMs Add to Document Processing

Traditional document data extraction relied on template matching and OCR with rule-based parsing: define the exact position of each field on an invoice, extract the text at that position. This works for high-volume, fixed-format documents from a single supplier but breaks immediately when the layout changes. LLMs understand document content semantically — they can find the invoice total regardless of whether it appears at the bottom left or the bottom right, whether it is labelled “Total”, “Amount Due”, or “Grand Total”. This flexibility means a single LLM-based extractor handles documents from hundreds of suppliers with different layouts, compared to a traditional system that requires a separate template for each.

The Extraction Pipeline

A document extraction pipeline has four stages. First, text extraction: convert the raw document to text. PDFs with embedded text extract cleanly with PyMuPDF or pdfplumber. Scanned PDFs and images need OCR first — Tesseract (free, local) or a cloud OCR service. Word documents extract via python-docx. Second, chunking: for long documents, chunk the text into sections small enough to fit in the LLM’s context window. For invoices and forms (typically under 2,000 tokens), the full document fits in one pass. For long contracts, chunk by section and extract from each chunk separately. Third, LLM extraction: send the document text with a structured extraction prompt to the LLM and request a JSON response with the specific fields you need. Fourth, validation: check that required fields are present and values match expected formats (dates are valid dates, amounts are parseable numbers). Route documents that fail validation to a human review queue.

Figure 1 — Document processing pipeline stages

1. Extract PDF/OCR/docx → plain text 2. Chunk Split if needed fit context window 3. LLM Extract Prompt → JSON structured fields 4. Validate Check fields route failures

Prompting for Structured Extraction

The extraction prompt is the most important variable. Structure it in three parts. First, the task definition: “Extract the following fields from the invoice text below. Return a JSON object with exactly these keys.” Second, the field list with type and description for each: “invoice_number (string): the unique invoice identifier; invoice_date (string, ISO 8601 format): the date on the invoice; total_amount (number): the total amount due including all taxes; vendor_name (string): the name of the company issuing the invoice.” Third, handling instructions for missing fields: “If a field is not present in the document, set its value to null. Return only the JSON object, no other text.” This prompt structure minimises hallucination (the model cannot invent field names), ensures consistent output format, and handles missing data gracefully.

Tools for Document Processing Pipelines

Several tool combinations work well depending on your environment. For a Python script: pdfplumber or PyMuPDF for text extraction, the Ollama Python library for LLM calls, and Pydantic for output validation. Pydantic models define the expected schema and raise clear errors when the LLM returns a field with the wrong type. For a no-code pipeline: n8n with an HTTP Request node to fetch the document, a PDF Extract node for text extraction, and an Ollama LLM node for extraction. Dify’s Workflow app type supports file inputs natively and has built-in document parsing nodes. For high-volume production: docling (IBM’s open-source document parser) handles complex PDFs including tables and multi-column layouts better than simpler text extractors, and its structured output integrates cleanly with LLM extraction prompts.

Handling Tables and Complex Layouts

Tables in PDFs are the hardest extraction case. Simple text extraction flattens table rows into space-separated strings that lose the column relationships. pdfplumber’s extract_table() method preserves row and column structure as a Python list of lists, which you can convert to a markdown table string before passing to the LLM — markdown table format is well-understood by modern LLMs and produces much more accurate extraction than flattened text. For scanned documents with tables, docling’s table detection or Surya’s table recognition model extract table structure from images before the LLM sees the content. Expect to spend more engineering effort on table extraction than on simple text fields — it is where the complexity of real-world document processing primarily lives.

Building Reliable Extraction at Scale

Production document processing needs two things that simple prototypes lack: confidence scoring and human review routing. After extracting fields, add a second LLM call that scores the extraction confidence (0–100) based on how clearly each field was stated in the document. Route low-confidence extractions to a review queue rather than writing them directly to the database. Track extraction accuracy over time by periodically sampling outputs and having humans verify them. This feedback loop identifies which document types or field types have systematic extraction errors, which you fix by improving the prompt or adding specific handling for that document type. A 95% accurate extraction that routes the remaining 5% to human review is vastly more useful than a 95% accurate extraction that silently writes incorrect values to your database.

Frequently Asked Questions

Which LLM is best for document extraction?
GPT-4o-mini from OpenAI is the best value cloud option for structured extraction — fast, cheap, and excellent at following JSON output instructions. For local inference, Qwen2.5 7B handles most extraction tasks reliably. For complex documents with ambiguous layouts, Qwen2.5 14B or 32B produces noticeably better results.
How do I handle multi-page documents?
For documents where the relevant data spans multiple pages (a contract summary on page 1 and key terms on pages 3-5), extract all pages to text and pass the full document if it fits in the context window (most modern models handle 32k+ tokens). For very long documents, extract page by page and merge the results, using a final LLM call to resolve any conflicts between pages.
Can local LLMs process scanned invoices?
Yes, with an OCR step. Run Tesseract or EasyOCR on the scanned image to produce text, then pass the text to Ollama. For better accuracy on poor-quality scans, docling with GPU acceleration produces cleaner text from complex layouts than basic Tesseract. The OCR step runs locally alongside Ollama with no external dependencies.

Last updated: October 2026

End-to-End Example: Invoice Processing with n8n and Ollama

A complete self-hosted invoice processing pipeline using n8n and Ollama: a Watch Files node monitors a folder for new PDFs (or a Gmail trigger watches for email attachments). A PDF Extract node converts the PDF to text. An Ollama LLM node receives the text with an extraction prompt requesting vendor name, invoice number, invoice date, line items (as an array), subtotal, tax, and total as a JSON object. A Code node validates the JSON structure and checks that numeric fields are parseable. A Spreadsheet File node appends the extracted data to a Google Sheet or CSV. A conditional branch routes low-confidence extractions (detected by a second quick LLM call asking to rate extraction confidence 1-5) to a Slack notification for human review. The whole workflow runs on your own hardware, processes invoices from any supplier without templates, and costs nothing per invoice beyond electricity. For a small business processing 50-200 invoices per month, this replaces meaningful manual data entry work and the cost of a dedicated invoice processing SaaS subscription.

Document Processing for Contracts and Legal Documents

Contract extraction is one of the most valuable document processing use cases and one where local inference is particularly important, as contract text is highly confidential. A contract extraction prompt identifies key dates (effective date, expiry date, renewal date), parties, obligations, payment terms, and termination clauses. The challenge with long contracts is that relevant clauses are spread across many pages. Use a two-pass approach: first pass extracts the table of contents and identifies which sections contain which types of information; second pass extracts specific fields from the relevant sections only, keeping each LLM call focused and within context limits. For clause summarisation rather than field extraction, a RAG approach works better: embed the full contract in a local vector store and query it conversationally for specific clause types. Both approaches keep confidential contract text on your infrastructure with no external API calls.

Planning Cost and Throughput

Before processing a large backlog, estimate the cost. Count the average tokens per page, multiply by the number of pages, and add the expected output size. Run a pilot on a few hundred documents to get real numbers instead of guessing. Use a smaller, cheaper model for simple, clean documents and reserve a stronger model for the complex ones, with a router deciding which applies. Batch non-urgent work to take advantage of discounted batch pricing where your provider offers it, and cache repeated instructions so you are not paying to resend the same long prompt for every page.

Leave a Comment