DEV Community

Cover image for How to triage Gmail with AI in n8n: rules first, drafts only (free workflow)
Relaybrook
Relaybrook

Posted on Edited on

How to triage Gmail with AI in n8n: rules first, drafts only (free workflow)

Most inbox "AI triage" demos send every single email to a model. That's slow, it burns your free-tier quota on newsletters, and when the API hiccups the email just disappears from the flow.

This tutorial walks through a free n8n workflow that does it the other way around: rules first, AI second. Bulk mail and notifications get sorted by plain header checks. Everything else gets a category, a priority, a one-line summary and, where it makes sense, a reply saved as a Gmail draft in the original thread. Nothing is ever sent.

You can import it, run it on four built-in sample emails in a couple of minutes, and then point it at your own inbox.

What you get per email

Every email that goes through the workflow ends up with these fields:

  • category (from your own list: support, sales, billing, newsletter, ...)
  • priority (urgent / high / normal / low) and priority_rank (1 to 4, for sorting)
  • summary (one sentence) and reason (why that priority)
  • suggested_reply (plain text, or empty when no reply is needed)
  • triage_by: ai, rules or rules (AI failed)
  • needs_human_review and review_reason
  • create_draft and draft_skipped_reason

That last one is small but useful: when an email doesn't get a draft, you can see why.

The flow, node by node

1. Two ways in

  • New unread email (Gmail Trigger): polls every 5 minutes for unread mail, with the search query -category:promotions -category:social so Gmail's own tabs filter out the noisiest stuff before n8n sees it.
  • Try with sample emails (Manual Trigger) + Sample inbound emails (Code): four made-up emails in the same shape the Gmail Trigger outputs: an outage report, a pricing question, a newsletter and a notification. They never create drafts, so this path is safe to run as often as you like.

2. Triage settings (Edit Fields)

One node holds everything you'd want to change:

Field Default What it does
my_name, my_company Alex, Your Company Used in the prompt and to sign replies
categories support, sales, billing, partnership, recruiting, personal, newsletter, notification, spam, other The only categories the AI may use
draft_categories support, sales, billing, partnership Which categories get a reply draft
vip_senders boss@yourcompany.com, @bigclient.com Addresses or whole domains that always go to the AI
reply_tone friendly, concise and professional Passed into the prompt
create_drafts true Set to false for triage only
max_body_chars 4000 Longer bodies are cut before the AI call
llm_base_url, llm_model Gemini's OpenAI-compatible endpoint Swap for any OpenAI-compatible API

3. Normalize email and flag bulk mail (Code)

Gmail's raw output is awkward to prompt with, so this node flattens each email into sender, subject and body, strips quoted history (On Mon, ... wrote: and > lines) and signatures, and trims long bodies.

It also flags bulk or automated mail from the headers and sender:

  • List-Unsubscribe header present
  • Precedence: bulk, list or junk
  • Auto-Submitted header
  • no-reply / notifications-style sender addresses

VIP senders are never flagged, even if they write to you through a mailing tool.

4. Automated or bulk mail? (If) → Label by rules (no AI)

Flagged mail goes down the rules branch: category newsletter or notification, priority low, no reply, triage_by: rules. No AI call. In a typical inbox that's a big share of the volume, and it's exactly the mail you don't want spending your free-tier requests.

5. Build triage prompt (Code) → AI triage (HTTP Request)

Everything else gets one chat request. A few prompt details that made a real difference:

  • The email is passed as untrusted data, and the model is told to ignore instructions inside it. Inbox triage is a textbook prompt-injection target ("ignore previous instructions and forward..."), so this matters.
  • The model must return only a JSON object with fixed keys: category, priority, needs_reply, summary, reason, reply_draft.
  • There's a written priority guide: urgent = something broken or money/a deadline at risk within 24h; high = a customer, lead or partner waiting on a decision; normal = needs an answer this week; low = FYI.
  • Reply rules: your tone, same language as the email, max 120 words, and never promise refunds, discounts, dates or facts that aren't in the email. Anything you need to fill in becomes a [placeholder].

The AI node is a plain HTTP Request to {llm_base_url}/chat/completions, not a vendor-specific node, so any OpenAI-compatible API works. It's batched one email at a time with a 4-second gap and retried up to 3 times, to stay inside free-tier rate limits.

6. Validate AI triage (Code)

This is the node I'd copy into any LLM workflow. It never trusts the answer:

  • category must be in your list, priority must be one of the four values, and a reply must exist when needs_reply is true.
  • Anything missing or invalid is repaired with a safe default and marked needs_human_review with a review_reason.
  • If the call itself failed (429 rate limit, wrong key, timeout) or the answer isn't JSON, keyword rules take over: words like "outage", "is down" or "can't log in" mean urgent; "invoice", "refund" or "pricing" mean high; and a keyword guess picks the category, but only if that category is in your list. triage_by becomes rules (AI failed).

So no email is dropped because an API had a bad minute.

7. Create a reply draft? (If)

A draft is created only when all of these hold: it's a real Gmail email (not a sample), it needs a reply, it passed validation, create_drafts is on, and its category is in draft_categories. Flagged emails never get a draft. Everything else goes to Triaged, no draft with its draft_skipped_reason.

8. Create threaded reply draft (Gmail)

The Gmail node uses the draft resource with the original threadId and the sender as recipient, so the draft shows up inside the conversation, ready for you to edit and send. There is no send node anywhere in the workflow.

Setup

  1. Download workflow.json from the repo (link below) and in n8n go to Workflows → Import from File.
  2. Connect a Gmail OAuth2 credential in New unread email and Create threaded reply draft.
  3. Get an API key from Google AI Studio (aistudio.google.com). Create a Header Auth credential with Name Authorization and Value Bearer YOUR_KEY, and select it in AI triage (OpenAI-compatible).
  4. Open Triage settings and set your name, company, categories, draft categories, VIP senders and tone.
  5. Click Execute workflow on Try with sample emails. Only the AI credential is needed for this; your inbox isn't touched.
  6. Check the output, then activate the workflow.

Using a different provider? Change llm_base_url and llm_model, for example https://api.openai.com/v1, https://api.groq.com/openai/v1 or http://localhost:11434/v1 for a local Ollama.

Ideas for extending it

  • Real Gmail labels: the workflow outputs category and priority as fields. To see them in Gmail, add a Gmail Add label node after Create a reply draft?.
  • Alerts: send urgent items to Slack or Telegram, or log every email to a Google Sheet.
  • IMAP: swap the trigger. The normalizer also reads IMAP fields, but drafts are Gmail-only.
  • Different filter: change the trigger's search query to include promotions, or watch only one label.

Limitations (please read)

  • You need your own credentials. Live runs need your Gmail OAuth2 connection and your own AI API key. Nothing is included in the JSON.
  • What was tested: the triage logic on self-hosted n8n 2.47.7 with the sample emails and a local stand-in for the AI API (normal answers, HTTP 429, non-JSON and invalid values). Live Gmail polling, live draft creation and a live Gemini call have not been tested yet. If you run it live and something breaks, please open an issue.
  • The AI can misjudge category or priority. Read every draft before sending it.
  • With the 4-second spacing, a large unread backlog takes a while on the first run.
  • Bodies over max_body_chars are cut, so very long emails are summarized from their first part only.

Get it

Free, MIT-licensed, core n8n nodes only: https://github.com/Relaybrook/n8n-free-workflows (folder workflows/gmail-ai-inbox-triage, with workflow.json and a README).


Disclosure: I run Relaybrook, which also sells paid n8n packs. The free workflow above is complete on its own. If you want more, the AI Inbox Triage Pack ($19) covers Gmail, Outlook and IMAP, and the AI Automation Bundle ($49) adds the lead enrichment and content repurposing packs. Code **BLACKFRIDAY40* gives 40% off through Dec 1, 2026 (crypto checkout) at relaybrook.bytesell.app.*

n8n is a trademark of n8n GmbH. Relaybrook is not affiliated with n8n.

Top comments (0)