Automatically classifying and routing incoming documents with AI in n8n (Drive, Dropbox)
Published 21 July 2026 · 5 min read
A shared "To process" folder that receives invoices, contracts, resumes and ID documents in a jumble is a classic in almost every small business. Someone drops the file, nobody renames it, and three weeks later you're digging by hand to find "the contract client X signed back in June." A n8n pipeline that reads each document on arrival, identifies what it is, and files it in the right place with a consistent name eliminates this filing work — without changing anyone's upload habits.
Why automate classification instead of sorting by hand
Manual document sorting suffers from three structural flaws:
- It depends on individual discipline: as long as everyone renames and files their documents correctly, the folder stays clean. It only takes one person in a hurry for the system to degrade;
- It doesn't scale: ten documents a week sort themselves without effort, two hundred become a project of their own;
- It leaves no usable trail: without a log of who filed what and when, retrieving the history of a specific document remains a manual search through the file tree.
An LLM plugged in after a storage trigger solves all three at once: it reads the content (not just the filename), classifies according to a schema defined once, and logs every decision.
Step 1 — Detect the arrival of a new document
n8n's Google Drive Trigger node watches a specific folder (it's essential to specify the folder ID rather than leave it on "My Drive" entirely, or the workflow fires on every existing file in your account). Set to a one-minute interval, it detects file creations and updates in that folder. For use on a shared team Drive, remember to enable the "Use Shared Drive" option on both the credential and the node — it's the most common oversight, and it shows up as 404 errors on files that are otherwise perfectly visible in the interface.
For a team that already centralizes files on Dropbox, the Dropbox Trigger node covers the same need with equivalent polling logic on a watched folder — the rest of the pipeline (extraction, classification, filing) stays identical regardless of the source storage.
Step 2 — Extract the content and classify with AI
Once the file is detected, the Extract from File node retrieves the text for native PDFs and Office documents. For a scan or a photo of a document, a multimodal (vision) LLM processes the image directly with no separate OCR step — the same approach detailed in our article on extracting invoice data from PDFs with AI.
The extracted content then flows into a Basic LLM Chain paired with a Structured Output Parser, with a schema that forces a usable answer rather than free text:
{
"category": "invoice | contract | resume | id_document | purchase_order | other",
"confidence": 0.0,
"suggested_name": "2026-07-invoice-vendor-x.pdf",
"summary": "One-sentence summary of the document's content"
}
The system prompt should explicitly enumerate the possible categories — exactly as with email classification in the Inbox AI Pack — to stop the model from inventing near-identical but non-matching labels ("invoice" one day, "bill" the next), which would be impossible to aggregate later in a Switch node or a report.
This principle of automatic document classification isn't new: as far back as 2014, a study by Kang, Kumar et al. (ICPR 2014) showed that a convolutional neural network could classify document images with performance clearly superior to the rule-based, hand-engineered feature approaches of the time. The difference today: a general-purpose LLM reaches a comparable level zero-shot, with no dedicated training corpus or specialized vision pipeline to maintain — which puts this kind of automation within reach of a small business in an afternoon rather than a data team over several weeks.
Step 3 — Route and rename automatically
The structured category field feeds a Switch node that routes the document to one of its branches. On each branch, a Google Drive node (Move operation) moves the file into the matching subfolder (/Invoices, /Contracts, /HR/Resumes, /KYC…), and an Update operation applies the model's suggested name — typically YYYY-MM-category-identifier.ext, a format that naturally sorts by date in any file browser with no extra effort.
For sensitive documents (ID documents, contracts), it's worth routing them to a restricted-access folder rather than a broadly shared one — the Switch can easily have a dedicated output that also applies a permission change via the Drive API.
Step 4 — Log decisions and handle ambiguous cases
Every filing decision gets written to a Supabase table (classified_documents: original name, category, confidence, final path, timestamp) via the Supabase node. It's the same audit-trail logic described in our n8n-Supabase connection guide, and it plugs directly into the architecture of the Compliance & Audit Pack for organizations that need to demonstrate full document traceability.
The confidence field acts as a safeguard: below a defined threshold (0.7, say), an IF node diverts the document to a Slack notification instead of automatic filing — exactly the pattern described in our article on human approval with the Wait node and Slack buttons. An unreadable scan or a hybrid document (an invoice that also contains contract terms) ends up in front of a human reviewer instead of being silently misfiled.
Pitfalls to avoid
- Processing a bulk import with no pacing: if you point this pipeline at a folder already holding several hundred files, the trigger will send all of them to the model's API at once. A Loop Over Items with a Wait between batches avoids 429 errors — see our article on rate limits of AI APIs in n8n;
- Ignoring duplicates: two uploads of the same file (renamed slightly differently) produce two separate entries. Checking by content hash rather than filename before classifying reduces this risk;
- Letting errors disappear silently: a timeout on extraction or the LLM call should never make an important document vanish without notice — a dedicated Error Workflow, as described in our n8n error-handling guide, covers this case.
How much does it cost
With a cost-efficient LLM (GPT-4o mini or equivalent), classifying a document of a few pages costs on the order of a few cents in tokens, vision OCR on a scan included. For a flow of a hundred documents a month, the bill stays in the range of a few euros — easily offset by the manual sorting time saved, not to mention the reliability of finding a properly filed document instead of one lost in a catch-all folder.
Conclusion
A document classification pipeline comes down to four building blocks: detection on arrival (Google Drive or Dropbox Trigger), AI extraction and classification with structured output, automatic filing and renaming, and logging with a confidence safeguard for ambiguous cases. It's exactly the document-ingestion architecture of the RAG Assistant Pack, reusable as-is: if your classified documents then need to be queryable ("find me the notice-period clause in contract X"), the RAG pipeline picks up right where this one leaves off.
FAQ
Frequently asked questions
Does this pipeline work with Dropbox instead of Google Drive?
Yes: n8n offers a Dropbox Trigger node with the same polling logic on a watched folder. Only the operation names change (move, rename); the AI classification step stays identical regardless of the storage backend.
What happens if the document is a poor-quality scan?
The model then returns a low confidence score or empty fields. That's the signal to route it to human review instead of forcing an automatic classification: a document left pending is better than a mislabeled contract you can no longer find.
How long before a new file gets classified?
With a Google Drive Trigger set to a one-minute interval, the typical delay between dropping a file and its final filing is a matter of tens of seconds, AI processing included.
Can new document categories be added later?
Yes, without touching the architecture: just extend the structured output schema's enum and add a matching branch on the Switch node. It's the same customization principle used for email sorting in the Inbox AI Pack.
Bundle FlowKit Complet
€269