AI Invoice Processing Pipeline Case Study

An AI-powered accounts-payable pipeline. It watches Gmail for invoices and receipts, extracts structured financial data with GPT-OSS 120B via Groq, validates totals and duplicates against deterministic business rules, logs clean records to a Google Sheets ledger, and alerts the team on Slack the moment something needs review.

Service
Automation
Industry
Fintech
Category
AI Automation

Built with: Groq / GPT-OSS, n8n, Google Sheets, Slack.

Overview

The Problem

  • Finance teams waste hours opening invoice PDFs, copying fields into spreadsheets, and double-checking math by hand
  • Incoming invoices arrive in email with no consistent format, vendors, dates, and line items vary across every document
  • Duplicate invoices slip through when the same bill is re-sent, forwarded, or logged twice under a different filename
  • Errors in subtotals, tax, and totals often go unnoticed until reconciliation, or until someone catches them manually

The Solution

  • Gmail is monitored automatically for messages with invoice, receipt, or bill attachments, no manual forwarding required
  • PDF attachments are parsed to text and passed to GPT-OSS 120B (Groq), which returns strict JSON: vendor, dates, currency, totals, and line items
  • A validation engine checks required fields, math accuracy, line-item sums, and duplicate invoice numbers against the ledger
  • Valid invoices are appended to Google Sheets; invalid or suspicious ones trigger a structured Slack alert with vendor, invoice #, total, and issue list

Solution Workflow

  1. Watch
  2. Extract
  3. Parse
  4. Check
  5. Validate
  6. Route

Full Technical Breakdown

1. Gmail Intake & Attachment Filtering

A Gmail Trigger polls the inbox every minute for Message Received events matching has:attachment (invoice OR receipt OR bill). An IF node confirms the message contains attachments before processing, so only relevant financial documents enter the pipeline, not every email in the inbox.

2. PDF Text Extraction

The Extract from PDF node converts the attachment into raw text for downstream AI parsing. This turns vendor-specific PDF layouts, tables, headers, footers, into a consistent text payload regardless of invoice design.

3. AI Structured Field Extraction (GPT-OSS 120B)

A Basic LLM Chain running openai/gpt-oss-120b via Groq extracts invoice fields into a fixed JSON schema: vendor, invoice number, invoice date, due date, currency, subtotal, tax, total, and line items (description, quantity, unit price, amount). A Structured Output Parser enforces the schema. The prompt requires only valid JSON, no markdown, no commentary, and explicitly forbids inventing missing data (null/0 when absent).

4. Duplicate Detection

Before validation completes, a Google Sheets lookup checks whether the extracted invoice number already exists in the ledger. This prevents double-logging when the same invoice is re-sent, forwarded, or processed twice from different email threads.

5. Business Rule Validation Engine

A Code node merges extracted fields with duplicate-check results and runs four deterministic rules: required fields (vendor, invoice number, and total must be present), total math (subtotal + tax must equal total within $0.01 tolerance), duplicate guard (invoice number must not already exist in the sheet), and line-item integrity (sum of line-item amounts must match subtotal within $0.01 tolerance). Each run outputs is_valid, a detailed issues array, and a processed_at timestamp for auditability.

6. Routing, Ledger Logging & Slack Alerting

A Valid? IF node splits the flow: valid invoices are appended to a clean Google Sheets ledger; invalid invoices trigger a Slack alert with structured context, vendor, invoice number, total, and a bulleted issue list, so finance can review duplicates, math mismatches, or missing fields without opening the workflow.

Results

Key Features

  • Intelligent Gmail monitoring: Polls Gmail on a recurring schedule with a targeted search filter, only messages with invoice, receipt, or bill attachments enter the pipeline, reducing noise and wasted runs.
  • PDF-to-structured-data extraction: Converts unstructured PDF invoices into a normalized JSON object with vendor details, dates, currency, totals, and itemized line items, ready for spreadsheet logging or downstream accounting.
  • Schema-constrained AI parsing: Uses a structured output parser with strict JSON rules and anti-hallucination guardrails, the model returns exactly the fields defined in the schema, nothing invented.
  • Deterministic validation layer: AI extraction is followed by code-based business rules, required fields, subtotal + tax = total, line-item sum checks, and duplicate detection, so bad data never silently enters the ledger.
  • Duplicate invoice prevention: Cross-references every invoice number against the existing Google Sheets ledger before append, protecting against double-payment risk and duplicate bookkeeping entries.
  • Actionable Slack alerts: Invalid invoices surface in Slack with vendor, invoice #, total, and precise issue details, finance sees exactly what failed and why, without digging through email or n8n execution logs.

Before

Manually opening PDF attachments, copying fields into spreadsheets, checking math by hand, missing duplicates until reconciliation, and no structured log of why an invoice was rejected or flagged.

After

Invoices are detected automatically from Gmail, parsed into structured JSON by GPT-OSS 120B, validated against business rules, logged cleanly to Sheets when valid, and flagged on Slack with precise issue details when not, consistent, auditable, hands-free intake.

What This Proves

This system demonstrates that finance operations can combine document extraction, constrained LLM parsing, and deterministic validation into one reliable pipeline, automating invoice intake while keeping humans in the loop only when data integrity actually requires it.

Tech Stack

AI Layer

  • Groq (GPT-OSS 120B) — Structured invoice field extraction from PDF text
  • Structured Output Parser — Schema-enforced JSON response
  • n8n — Workflow orchestration engine

Data Layer

  • Gmail API — Inbox monitoring and attachment intake
  • PDF / File Extraction — Invoice text extraction from attachments
  • Google Sheets — Duplicate lookup and clean invoice ledger
  • Custom JS Validation Engine — Required-field checks, math validation, duplicate guard

Communication Layer

  • Slack API — Invalid-invoice alerts with vendor, invoice #, total, and issue list
  • Google Sheets API — Automated ledger append for valid records

Scalability & Flexibility

  • LLM Provider Flexibility: Currently runs on Groq GPT-OSS 120B; can swap to Llama 3.3 70B, GPT-4o, Claude, or Gemini for higher-accuracy extraction on complex multi-page or non-English invoices.
  • Intake Channel Expansion: Gmail trigger can extend to Outlook, Google Drive folder watch, or a shared AP inbox webhook without redesigning extraction or validation logic.
  • Multi-Attachment Handling: Built to process PDF attachments per message; can loop over multiple files per email (invoice + receipt) with per-file validation and logging.
  • Approval Workflow Optional: Valid invoices can route through a Slack approve/reject step before final ledger append for high-value or new-vendor bills.
  • Accounting System Sync: Structured JSON output can push directly into QuickBooks, Xero, or NetSuite via API using the same validated payload.
  • Vendor-Specific Rules: Validation engine can add per-vendor tolerance rules, expected currency checks, or PO-number matching without changing the core extraction flow.
  • Audit Trail & Historical Logging: processed_at, issues, and raw extraction can be logged to a separate audit sheet or database for compliance and reconciliation reporting.
  • Threshold Alerting: Can add logic to flag invoices above a dollar threshold, overdue due dates, or unknown vendors for priority review before payment.

Gallery

  • Full n8n invoice processing workflow
  • Slack finance-alerts channel showing flagged invoices with issue details
About Nexoryn Nexoryn Services Nexoryn Portfolio Client Reviews Contact Nexoryn Privacy Policy Terms of Service Cookie Policy