AvnishYadav
WorkProjectsBlogsNewsletterSupportAbout
Work With Me

Avnish Yadav

Engineer. Automate. Build. Scale.

ยฉ 2026 Avnish Yadav. All rights reserved.

The Automation Update

AI agents, automation, and micro-SaaS. Weekly.

Explore

  • Home
  • Projects
  • Blogs
  • Newsletter Archive
  • About
  • Contact
  • Support

Legal

  • Privacy Policy

Connect

LinkedInGitHubInstagramYouTube
Building an AI Inbox Triage Agent with n8n and OpenAI
2026-02-21

Building an AI Inbox Triage Agent with n8n and OpenAI

8 min readTutorialsEngineeringAutomateautomationn8nAI agentsOpenAIproductivitygmail

A technical walkthrough on building a Human-in-the-Loop email automation system using n8n, Gmail, and OpenAI. Transform chaos into structured data.

If you are a developer or a founder, your inbox is likely the biggest leak in your productivity pipeline. It creates a false sense of work; reading an email feels like doing something, but itโ€™s usually just context switching.

The problem isn't the volume of email; it's the lack of structure. An inbox is an unstructured stream of data containing invoices, client fires, newsletters, and spam, all presented with equal visual weight.

In this tutorial, we are going to treat email for what it actually is: raw data that needs normalization.

We will build an automation agent using n8n and OpenAI that performs three specific functions:

  1. Triage: Classifies incoming mail (Urgent, Newsletter, Invoice, FYI).
  2. Summarize: Extracts the core intent and action items.
  3. Route: Moves the data where it belongs (Task Manager, Slack, or Archive).

This is a Human-in-the-Loop (HITL) system. We aren't automating replies (yet); we are automating the cognitive load of decision-making.

The Architecture

We will use a self-hosted or cloud version of n8n. The logic flow is linear but powerful:

  • Trigger: Gmail (Polling for unread messages).
  • Processing: OpenAI (GPT-4o-mini for speed/cost balance) to analyze the body text.
  • Logic: A Switch node to route based on classification.
  • Action:
    • Apply a Gmail Label (e.g., "Processed_AI").
    • Create a task in Notion/Linear/Todoist (if actionable).
    • Send a digest to Slack (if informational).

Step 1: The Gmail Trigger

First, configure the n8n Gmail node. While webhooks are faster, polling is easier to set up for a personal inbox and less prone to rate-limiting issues during testing.

Configuration:

  • Resource: Message
  • Operation: GetAll
  • Return All: False (Limit to 10 for batching).
  • Filters:
    • q: is:unread -label:Processed_AI

Note: The negative label filter is crucial. It prevents the automation from looping over the same emails indefinitely.

Step 2: The AI Classifier (The Brain)

This is where the engineering happens. We pass the email subject and body to an OpenAI Chat Model node.

We don't just want a summary; we want structured JSON output. This allows the subsequent n8n nodes to programmatically handle the data.

Model: GPT-4o-mini (Cheap, fast, capable enough for classification).
Type: JSON Mode (Ensure you enforce valid JSON in the prompt).

The System Prompt

Copy this prompt into the system message of your AI node:

You are an executive assistant automation. 
Analyze the incoming email and output valid JSON with the following keys:

1. "category": One of ["Client_Request", "Urgent_Bug", "Newsletter", "Invoice", "Spam", "Other"].
2. "sentiment": "Positive", "Neutral", or "Negative".
3. "summary": A 1-sentence summary of the email.
4. "action_items": An array of strings listing specific tasks requested, if any.
5. "priority": 1 (High) to 3 (Low).

If the email is marketing or spam, set priority to 3 and action_items to empty.

The User Prompt:

Subject: {{ $json.subject }}
Body: {{ $json.snippet }} / {{ $json.body }}

Step 3: Parsing and Routing

Once the LLM returns the JSON, use an Edit Fields node to parse the content if it comes back as a string, though n8n's latest OpenAI node often handles parsing automatically.

Next, set up a Switch Node based on the category key.

  • Route 1 (Client_Request / Urgent_Bug): These are tasks. They go to the Project Management system.
  • Route 2 (Newsletter / Invoice): These are reference materials. Label them and archive.
  • Route 3 (Spam): Mark as read and trash.

Step 4: The Action (Task Creation)

Let's focus on Route 1. If a client emails about a bug, we want that in our task tracker (e.g., Notion or Linear), not buried in Gmail.

Connect a Notion Node to the first output of the Switch.

Mapping the fields:

  • Database: Your "Inbox" or "Tasks" database.
  • Title: {{ $json.category }}: {{ $json.subject }}
  • Content/Body:
    Summary: {{ $json.summary }}
    
    Action Items:
    {{ $json.action_items.join('\n') }}
    
    Link: https://mail.google.com/mail/u/0/#inbox/{{ $node.Gmail.json.id }}
  • Status: "To Do"
  • Priority: Map the AI's priority score.

Crucial Step: Always include the deep link back to the original email thread. You will eventually need to reply, and you don't want to hunt for the thread.

Step 5: Closing the Loop (Labeling)

If we don't change the state of the email in Gmail, our trigger will pick it up again (unless you rely solely on the "Unread" status, which is risky if you accidentally mark it unread manually).

Add a final Gmail Node at the end of every route.

  • Operation: Add Label
  • Label: Processed_AI
  • Optional: Mark as Read (I prefer to keep actionable emails unread but labeled, while newsletters get marked read automatically).

Human-in-the-Loop Considerations

Why not automate the reply?

I advise against automating replies directly from the triage agent for two reasons:

  1. Hallucinations: Even GPT-4 can misinterpret tone. You do not want an AI telling your biggest client "We will fix this immediately" if you are actually out of office or the request is out of scope.
  2. Security: If prompt injection occurs via an incoming email, you don't want the AI outputting internal data in a reply.

The goal here is triage. We are preparing the data so the human (you) can execute rapidly.

Testing and Refinement

When you deploy this, run it manually first. Watch the execution log in n8n.

Common edge cases to handle:

  • Long threads: If the email is a Re: Re: Re: chain, the context window might get filled with historical clutter. Use a text-processing node to truncate the body or only feed the latest reply to the AI.
  • HTML formatting: Gmail sends raw HTML. Use n8nโ€™s HTML to Markdown node before sending the body to OpenAI to save tokens and improve accuracy.

The Result

Once active, your workflow looks like this:

  1. Email lands.
  2. n8n wakes up, reads it.
  3. AI determines it's a server alert.
  4. AI creates a High Priority ticket in Linear with the error logs summarized.
  5. AI archives the email from the inbox.

You didn't check your email, but the work is already queued. That is the difference between being busy and being productive.

Share

Comments

Loading comments...

Add a comment

By posting a comment, youโ€™ll be subscribed to the newsletter. You can unsubscribe anytime.

0/2000