AI Email Labeling in Gmail with OpenAI
· By AI Compare
Let AI sort your inbox into Sales, Support, Invoices and more, with a ready-to-use prompt.
What you will build
An automation that reads new Gmail messages, asks an AI model which category each one belongs to, and applies the matching Gmail label: for example Sales, Support, Invoices, Newsletters, Personal, Needs reply. It runs every few minutes and keeps your inbox sorted without filters you have to maintain.
This follows the idea of n8n's "Basic automatic Gmail email labelling with OpenAI and Gmail API" template.
Why AI instead of Gmail filters
Gmail filters match exact words and senders. AI understands meaning: "Can you send me a quote for 50 units?" goes to Sales even without the word "quote" in the subject. The trade-off is a small cost per email and occasional mistakes, which you can reduce with clear label descriptions.
Workflow structure in n8n
| # | Node | Purpose |
|---|---|---|
| 1 | Gmail Trigger (poll every 5–15 min) | New emails in the inbox |
| 2 | Gmail: Get labels | Fetch label IDs (or hard-code them) |
| 3 | AI Agent or Basic LLM Chain + OpenAI Chat Model | Classify the email |
| 4 | Structured Output Parser | Force a reply like {"labels": ["Sales"]} |
| 5 | Gmail: Add Labels | Apply the label IDs to the message |
The classification prompt
Classify this email into one or more of these labels:
- Sales: questions about buying, pricing, quotes, demos
- Support: problems from existing customers
- Invoices: bills, receipts, payment confirmations
- Newsletters: marketing emails and subscriptions
- Needs reply: a person is waiting for my answer
Return JSON: {"labels": [...]}. Use only these labels.
From: {{ $json.from }}
Subject: {{ $json.subject }}
Body: {{ $json.text.slice(0, 3000) }}
Truncating the body keeps costs low. A small, cheap model is usually enough for classification.
The same idea in Zapier and Make
- Zapier: Gmail New Email → AI by Zapier or ChatGPT "classify text" step → Paths per label → Gmail Add Label. Each email uses several tasks.
- Make: Gmail Watch Emails → OpenAI "Create a completion" with JSON output → Router → Gmail Add a Label. About 3–4 credits per email plus AI tokens.
- n8n: one execution per poll, however many emails it processes (or free self-hosted), plus AI tokens.
Tips
- Create the labels in Gmail first.
- Start with 5–7 labels; more categories mean more mistakes.
- Add a Text Classifier node instead of an agent if you want n8n's built-in classification with one output per category.
- Log decisions to a sheet for a week and refine label descriptions where the model is wrong.
- Do not let the AI archive or delete emails until you trust its labels.
Related
- Gmail and Outlook automation
- Run local LLMs with Ollama to classify without sending email to an external API
