An AI Virtual Accounting Department for Financial Document Processing

A 77-node n8n workflow that uses Gemini OCR to extract invoices, receipts, and bank statements into Google Sheets — with auto-categorization and a natural-language financial-analysis agent.

77

workflow nodes
Deployment:

Self-Hosted

THE BRIEF

What the system needed to do.

This project is a 77-node n8n workflow that automates financial document processing as an AI "virtual accounting department." It handles three document types, each with a dedicated flow: invoices uploaded via chat, and expense receipts and bank statements dropped into monitored Google Drive folders. Google Gemini performs OCR to extract structured fields, an OpenRouter LLM auto-categorizes expenses into 17 categories, and a custom parser splits multi-row bank statements. All data is stored in Google Sheets, files are auto-renamed, and a natural-language financial-analysis agent can query invoices, expenses, and transactions on demand — turning manual bookkeeping into a hands-off, queryable system.

The scope

Automatically read invoices, receipts, and bank statements with AI OCR, extract and categorize the data, and enable natural-language financial analysis.

DESIGN CHOICES

What makes the build interesting.

Accountants & finance teams

Role-based, multi-document processing

Gemini OCR extraction with smart categorization

Natural-language financial analysis agent

THE ARCHITECTURE

Follow the flow.

Sourced architecture diagram of the AI virtual accounting department workflows
  1. Ingest — invoices are uploaded via a chat interface; expense receipts and bank statements are dropped into monitored Google Drive folders (checked every minute).
  2. Store & upload — the document is saved to Drive and uploaded to Google Gemini's File API.
  3. AI OCR extraction — Gemini reads the PDF and returns the relevant structured fields for that document type.
  4. Parse & transform — JSON is cleaned; invoice and bank-statement line items are split into individual rows via code.
  5. Categorize (expenses) — an LLM assigns each expense to one of 17 predefined categories.
  6. Record — structured data is written to the appropriate Google Sheet (invoices, expenses, or transactions).
  7. Organise (invoices) — the file is renamed {Vendor} - {Invoice Number} and a confirmation is sent.
  8. Analyse on demand — an AI agent queries all three sheets to answer natural-language financial questions.

Tools and platforms

  • n8n — workflow orchestration (77 nodes, self-hosted)
  • Google Gemini (File API) — OCR and structured data extraction from PDFs
  • OpenRouter (GPT-4.1) — context-aware expense categorization and the analysis agent's model
  • Google Drive — document storage and folder monitoring (invoices, receipts, statements)
  • Google Sheets — system of record (invoice, expense, and transaction sheets) and analysis data source
  • n8n Chat Trigger — invoice upload interface
  • n8n LangChain Agent & Tools — natural-language financial analysis over the sheets
  • Structured Output Parser & Code nodes — schema enforcement and multi-row JSON parsing

What the design enables.

  • Eliminates manual data entry — invoices arrive by chat and receipts and statements by folder drop, becoming structured spreadsheet data automatically, with no keying by hand.
  • Clean, categorised, analysis-ready records — consistent extraction, automatic expense categorization, line-item splitting, and standardised file naming keep the books tidy and queryable.
  • Insight on demand — the financial-analysis agent turns stored data back into answers, removing the need to manually dig through spreadsheets for reporting.

The difficult parts.

  • One parser can't fit every document type. Invoices, receipts, and statements differ structurally. We built separate flows per document type, each with its own trigger, tuned Gemini extraction prompt, and destination sheet, so every document is handled correctly.
  • Bank statements contain many transactions per file. A single extraction returns multiple rows in varied shapes. We added a custom JavaScript parser that cleans the AI output and converts it into individual transaction objects for the spreadsheet.
  • AI responses don't always return clean JSON. Malformed output can break automation. We strip markdown fences, parse defensively with error handling, and continue on non-critical failures (such as a failed rename) so processing never stalls.

Build notes and context.

The brief: replace manual bookkeeping data entry with an AI back office.

The client wanted to automate the most tedious part of finance operations: getting data out of financial PDFs and into a usable, queryable form. The goal was a system that ingests invoices, expense receipts, and bank statements, uses AI OCR to extract the right fields from each, categorizes and stores everything in spreadsheets, and then lets a user interrogate all of it in plain English. We delivered this as a 77-node n8n workflow organised as a "virtual accounting department" — four AI roles, each owning one financial function, all writing to a shared Google Sheets backbone.

Role 1 — Invoice processing. Users upload invoice PDFs through a chat interface. The file is saved to a Google Drive "Invoices" folder, then uploaded to Google Gemini's File API, where Gemini performs OCR and extracts structured data: vendor name, invoice number, invoice and due dates, total and VAT amounts, and itemised line items. The AI's response is parsed and cleaned (stripping markdown fences and gracefully handling malformed JSON), the line items are split into individual spreadsheet rows for item-level tracking, and the data is written to an invoice-records sheet. The original file is then automatically renamed to a consistent {Vendor Name} - {Invoice Number} format, and a confirmation message is sent back to the user.

Role 2 — Expense receipt processing. A Google Drive "Expense Receipts" folder is monitored every minute for new uploads. Each new receipt PDF is sent to Gemini, which extracts merchant name, transaction date, total and tax amounts, payment method, and any line items. The extracted expense then passes to an LLM (GPT-4.1 via OpenRouter) that performs context-aware categorization, assigning it to one of 17 predefined expense categories, before the structured, categorised record is logged to an expenses sheet. This turns a pile of receipts into clean, analysis-ready, categorised data with no human touch.

Role 3 — Bank statement processing. A "Bank Statements" folder is similarly watched. New statement PDFs are downloaded and passed to Gemini, which extracts multiple transactions per document — transaction date, description/payee, debit and credit amounts, currency, and running balance. Because a statement contains many rows, a custom JavaScript parser cleans the AI output (handling several possible response shapes) and converts it into individual transaction objects, which are written to a bank-transactions sheet ready for reconciliation.

Role 4 — Financial analysis agent. Sitting on top of the three datasets is an AI agent acting as a finance analyst. It has read access to all three Google Sheets — invoices, expenses, and transactions — as tools, and answers natural-language queries such as "what are the last ten bank transactions?", "what's the total value of invoices for December?", or "list all unpaid invoices." This is the payoff that turns recorded data back into usable insight without manual spreadsheet work.

Why this design works. Splitting the system by document type — each with its own trigger, extraction prompt, and destination sheet — is what makes it reliable: each "role" is tuned for the structure of its documents rather than forcing one generic parser to handle everything. Gemini's File API does the heavy lifting of reading varied real-world layouts, while a dedicated categorization model and a custom multi-row parser handle the parts where raw OCR isn't enough. Throughout, the workflow cleans AI output defensively and continues on non-critical errors (like a failed rename) so a single hiccup doesn't halt processing. Google Sheets acts as both the system of record and the data source the analysis agent queries, keeping everything in one transparent, auditable place. The result is a genuine AI back office: invoices arrive by chat and receipts and statements by folder drop, and within moments they become structured, categorised, queryable financial data — with a CFO-style agent ready to answer questions on demand.

Have a related process in mind?

Tell me what goes in, what should come out and where the current process gets stuck.