Skip to content
All posts

AI document extraction for startups: turning PDFs and scans into data you can use

How to build an AI document extraction pipeline for invoices, contracts and forms: getting clean text, extracting fields, validating them and keeping a person in the loop where it matters.

Shubham Somani 4 min read

A lot of businesses still run on documents: invoices, purchase orders, contracts, applications, delivery notes. Someone opens each one and types the important parts into another system. It’s slow, it’s error-prone, and it’s one of the most practical places to use AI.

LLMs are good at reading messy documents. The hard part is everything around the model: getting clean text in, checking what comes out, and deciding what happens when it’s wrong. Here’s how we’d build AI document extraction that holds up in production.

Start with the documents you actually get

Before writing any code, collect a real sample.

  • Gather documents from many senders, not just your three biggest customers. Layouts vary more than you expect.
  • Include the ugly ones: phone photos, faxed scans, multi-page files, handwritten notes and documents in other languages.
  • Write down the fields you need and what each one feeds into. “Invoice total” matters more if it triggers a payment.
  • Note who types this in today, how long it takes and what mistakes they catch. That is your baseline.

Get clean text and layout first

The model can only extract what it can read.

  • Use the text layer when a PDF has one, and OCR when it’s a scan or a photo.
  • Keep the layout where you can. Tables, columns and page positions often carry meaning, such as which number belongs to which line item.
  • Store the original file and the extracted text together, so you can reprocess later when your pipeline improves.

Many models can read images directly. That can help with tricky layouts, but test it on your own documents rather than assuming it’s better.

Extract into a strict schema

Ask for exactly the fields you need, in a fixed structure.

  • Use clear field names and types, with null allowed when a value isn’t present.
  • Ask for a short source quote or page number for important fields, so a reviewer can check them quickly.
  • Let code do the maths. Extract line items and amounts, then calculate totals yourself and compare them with the stated total.

We go deeper on schemas, validation and retries in getting reliable JSON from an LLM.

Validate against the real world

A field can be well-formed and still wrong.

  • Cross-check with your own data: does this supplier exist, does this PO number match an open order, is this customer ID real?
  • Check the arithmetic: line items add up to the subtotal, tax looks plausible, dates are in a sensible range.
  • Flag duplicates before they become a double payment.

Keep a person in the loop where it matters

Don’t aim for full automation on day one.

  • Route documents to review when validation fails, a required field is missing or the document type is unfamiliar.
  • Show the reviewer the original next to the extracted fields, with the source highlighted, so checking takes seconds.
  • Require approval for anything that moves money or changes a customer’s account, at least until you’ve measured accuracy for a long time.
  • Save every correction. Corrections are your best test cases and tell you which fields need work.

Measure accuracy per field

“It works most of the time” isn’t a number you can manage.

  • Build a labelled test set from your real sample, with the correct value for each field.
  • Measure accuracy field by field and by document type. A good overall score can hide a field that’s often wrong.
  • Re-run the set on every prompt, model or pipeline change. See test your LLM feature before users do.

Handle the data with care

Documents often contain personal and financial details. Decide where files are stored, who can see them, what goes into logs and how long you keep them. Our post on keeping customer data safe in LLM features covers the basics.

The short checklist

  • Collect a real, messy sample and record today’s manual baseline
  • Get clean text and layout before extracting
  • Extract into a strict schema with source references
  • Validate against your own data and the arithmetic
  • Send failures and money-moving documents to a person
  • Measure accuracy per field and keep every correction

If your team is retyping documents into another system and you want that automated properly, see our fixed-scope offers or book a 30-minute call.