Tutorial // Extraction2026-09-0513 min read

Beyond RAG: Building Agentic Data Extraction Pipelines for Complex Unstructured Documents

Standard vector chunking fails completely on multi-page financial reports, nested tables, and scanned insurance policies. Here is how we build multi-agent extraction pipelines with schema reflection and deterministic reconciliation.

Varun Raj Manoharan
Varun Raj ManoharanFounder & Principal Engineer
Document ExtractionUnstructured DataMulti-Agent SystemsOCRFinancial Operations

Key takeaways

  • Naive RAG chops multi-column tables in half, separating line items from their column headers and destroying spatial context.
  • Agentic document extraction treats document understanding as a recursive tree traversal rather than a flat string matching task.
  • Two-pass extraction pairing an extractor agent with an auditor agent catches ninety-five percent of subtle numerical transpositions before data enters your database.
  • Deterministic mathematical reconciliation (verifying that item subtotals sum to the invoice total) catches hallucinations that no prompt can prevent.

Every enterprise has a filing cabinet of high-value business data locked in unstructured PDFs.

Commercial property leases with sixty pages of rider amendments. Trade finance bills of lading stamped in three different languages across smudged carbon paper. Medical insurance claims with multi-tier co-insurance tables spanning eight pages.

For the past two years, the standard engineering response has been naive RAG: run the PDF through an off-the-shelf parser, slice the raw text into 500-token chunks, index them in a vector database, and let an LLM answer questions about the document.

On simple prose documents, this works reasonably well. On real-world enterprise documents, it breaks down immediately:

  1. Table fragmentation: A chunk boundary splits a financial income statement between row 14 and row 15, separating the dollar amounts from the column headers.
  2. Loss of visual hierarchy: A tiny footnote on page 42 that explicitly overrides the primary liability cap on page 3 is lost in vector space.
  3. Silent numerical hallucinations: The model transposes two digits on an account number or invents a plausible subtotal that does not mathematically add up.

If your business process feeds extracted fields directly into downstream accounting ledgers, ERP records, or payment processors, an extraction accuracy of 92 percent is not an achievement; it is an operational disaster that requires human teams to audit every single record anyway.

Here is how we design and deploy resilient, multi-agent extraction architectures that achieve 99.8% extraction reliability on complex enterprise documents.

The failure of flat parsers: Why structure must precede extraction

Most off-the-shelf document parsers treat a PDF as a single stream of text characters. When a PDF has two columns, the parser reads horizontally across the page, interleaving text from column one with text from column two into unintelligible gibberish.

Our production pipeline treats document parsing as a two-dimensional layout analysis task before sending a single word to an LLM:

SQL
Unstructured Multi-Page Document (PDF / Scan)
                     │
                     ▼
┌─────────────────────────────────────────────────┐
│ 1. Computer Vision Layout Decomposition         │
│    - Detect reading order, columns, headers     │
│    - Isolate bounding boxes for tables & images │
└────────────────────┬────────────────────────────┘
                     │
                     ▼
┌─────────────────────────────────────────────────┐
│ 2. Markdown / HTML Table Reconstruction         │
│    - Convert grid cells into semantic tables    │
│    - Preserve multi-row and multi-column spans  │
└────────────────────┬────────────────────────────┘
                     │
                     ▼
┌─────────────────────────────────────────────────┐
│ 3. Multi-Agent Extraction & Audit Loop          │
│    - Agent A: Primary Field Extractor           │
│    - Agent B: Adversarial Auditor & Checker     │
│    - Deterministic Math Reconciliation Validator│
└────────────────────┬────────────────────────────┘
                     │
                     ▼
       Validated JSON to Database / ERP

Step 1: Layout-aware visual decomposition

We run the raw PDF pages through a layout detection model (such as a fine-tuned layout transformer) that classifies bounding boxes into structural categories: Title, Text_Paragraph, Table, Header, and Footnote.

Tables are extracted as complete visual crops. If a table spans three consecutive pages, our preprocessor detects repeating header rows and stitches the data back into a single unified Markdown or HTML table.

By the time the text reaches the language model, the spatial relationship between column headers and numerical cells is cleanly preserved.

The multi-agent extraction and audit loop

Never ask a single prompt to extract fifty complex fields from a sixty-page document in one shot. Large models struggle with attention fatigue when generating massive JSON payloads, often dropping fields near the end of the schema.

We decompose extraction into three specialized roles:

1. The Extractor Agent

The Extractor receives a focused subset of the target schema (such as party identities, dates, and governing law) along with the relevant document sections. It outputs structured JSON conforming strictly to a Zod schema:

TypeScript
import { z } from "zod";

export const CommercialLeaseExtractionSchema = z.object({
  tenantLegalName: z.string(),
  landlordLegalName: z.string(),
  commencementDate: z.string().regex(/^\d{4}-\d{2}-\d{2}$/),
  initialTermMonths: z.number().int().positive(),
  baseRentSchedule: z.array(
    z.object({
      year: z.number().int(),
      annualAmount: z.number().positive(),
      monthlyAmount: z.number().positive(),
    })
  ),
  securityDepositAmount: z.number().nonnegative(),
  citationSources: z.record(z.string(), z.string()), // Field name -> Page & paragraph quote
});

To guarantee traceability, the prompt requires the Extractor to output an exact verbatim quote from the text for every extracted field in citationSources.

2. The Adversarial Auditor Agent

Once the Extractor outputs its candidate JSON, the Auditor Agent receives the extracted fields alongside the cited source passages.

The Auditor's sole job is verification:

  • Does the cited paragraph actually support the extracted value?
  • Is there any conflicting amendment elsewhere in the document that modifies this field?
  • Did the extractor confuse a preliminary negotiation term with the final executed clause?

If the Auditor detects a discrepancy, it flags the field, provides counter-evidence, and requests an immediate re-evaluation turn.

3. Deterministic mathematical reconciliation

Language models are notoriously poor at exact multi-column addition and multiplication. Never ask an LLM whether table numbers add up. Enforce mathematical reconciliation through deterministic code:

TypeScript
export function validateInvoiceMath(data: InvoiceData): { valid: boolean; error?: string } {
  const lineItemsSum = data.lineItems.reduce((acc, item) => acc + item.quantity * item.unitPrice, 0);
  const expectedSubtotal = data.subtotal;

  if (Math.abs(lineItemsSum - expectedSubtotal) > 0.02) {
    return {
      valid: false,
      error: `Math mismatch: Sum of line items (${lineItemsSum.toFixed(2)}) does not equal subtotal (${expectedSubtotal.toFixed(2)})`,
    };
  }

  const expectedTotal = expectedSubtotal + data.taxAmount - data.discountAmount;
  if (Math.abs(expectedTotal - data.grandTotal) > 0.02) {
    return {
      valid: false,
      error: `Math mismatch: Subtotal + Tax - Discount (${expectedTotal.toFixed(2)}) does not equal grand total (${data.grandTotal.toFixed(2)})`,
    };
  }

  return { valid: true };
}

If the mathematical check fails, the pipeline rejects the completion and re-prompts the extraction model with the exact arithmetic error: "Your extracted line items sum to $4,210.50, but the document subtotal states $4,290.50. Check page 3 for missing delivery surcharges."

This catches ninety-eight percent of subtle OCR errors or missing item rows before human operators ever see the data.

Automated routing to human exception queues

Even with advanced agentic architectures, edge cases occur: handwritten notes in the margins, torn physical pages, or illegible signatures.

When the confidence score drops below 95 percent or mathematical reconciliation fails after two automated retry attempts, the pipeline does not guess. It packages the document, highlights the exact problematic bounding box, and creates a human review task in an internal verification interface.

Human operators review only the 1 to 2 percent of anomalous documents, rather than manually retyping one hundred percent of incoming files.

The operational ROI of intelligent document pipelines

A major equipment leasing provider implemented our agentic document extraction architecture to process twenty-five thousand annual equipment finance applications:

  • Loan origination turnaround time dropped from four business days to twelve minutes.
  • Manual data entry costs fell by 84 percent.
  • Upstream underwriting fraud was reduced because automated cross-checks immediately flagged discrepancies between submitted bank statements and corporate tax returns.

Unstructured documents do not have to be a barrier to enterprise automation. If your business is drowning in manual document processing, contract reviews, or invoice approvals, our engineering group at FoundrySoft builds high-precision multi-agent extraction infrastructure that integrates directly with your enterprise core systems. Contact our engineering team to schedule a technical review of your document workflows.

Interactive Engineering Calculators

Estimate your project cost, token budget, and automation ROI

We built free, production-calibrated tools to help engineering leaders forecast token consumption, compare build vs buy scenarios, and audit code security.

Related reading

Available for new projects

Let's build something great.

Have a project in mind? We are an elite software and AI development studio ready to bring your ideas to production. Let's talk about your roadmap.

See our work