Auto-Reply to Gmail with AI in n8n: Your First Workflow (Part 3)

Build an n8n workflow that replies to new Gmail messages with AI: Gmail Trigger, AI Agent with free Gemini, and Reply — step by step with real screenshots.

This is Part 3 of the AI Automation Mastery with n8n series. In Part 2 we got n8n running. Now we build a real AI workflow: every time a new email lands in Gmail, n8n reads it, Google Gemini writes a reply, and n8n sends that reply back in the same thread — automatically. Along the way you’ll learn n8n’s most important idea: trigger nodes and action nodes.

In short

  • Gmail Trigger (the trigger) checks your inbox every minute.
  • AI Agent (an action) uses Gemini to write the reply, shaped by a Structured Output Parser.
  • Reply to a message (an action) sends the AI’s text back in the same thread.
  • You need two credentials: a Gmail (Google OAuth) credential and a free Gemini API key.

What you need

Two kinds of nodes: trigger and action

Every step in an n8n workflow is a node, and nodes come in two kinds:

  • Trigger node — starts the workflow and decides when it runs: a button click, a schedule, a webhook, a form — or, here, a new Gmail message.
  • Action node — does the work after that: here the AI Agent writes the reply and Reply to a message sends it.

The rule: a workflow always starts with a trigger, then one or more actions follow. Some nodes, like the AI Agent, also take sub-nodes — here the Gemini model (the “brain”) and an output parser (the answer format).

The workflow: Gmail Trigger → AI Agent (with Gemini + output parser) → Reply
The workflow: Gmail Trigger → AI Agent (with Gemini + output parser) → Reply

Step 1: Add the Gmail Trigger

  1. Create a new workflow and click Add first step.
  2. In “What triggers this workflow?”, search for Gmail and pick it.
Search “Gmail” in the trigger list
Search “Gmail” in the trigger list
  1. Credential: choose your Gmail account (create it here if you haven’t).
  2. Poll Times: Every Minute. Event: Message Received.
  3. Simplify: turn it off, so the full email body arrives in the text field for the AI.
Gmail Trigger: your account, every minute, Simplify off
Gmail Trigger: your account, every minute, Simplify off

Tip: to avoid replying to newsletters and no-reply senders, open Filters → Add Filter → Search and use a Gmail search such as:

-category:promotions -category:social -from:noreply

Step 2: Add the AI Agent (action)

  1. Click the + on the right of the Gmail Trigger. The “What happens next?” list opens — these are actions.
  2. Open AI and pick AI Agent.
After the trigger, + opens the action list — pick AI → AI Agent
After the trigger, + opens the action list — pick AI → AI Agent
  1. Source for Prompt: Define below. Prompt: {{ $json.text }} — the email text from the trigger.
  2. Require Specific Output Format: on.
  3. Options → System Message: tell the AI how to reply, for example:
Reply to this email in a professional, friendly way.
Don't add any instructions or notes — only the reply text.

Regards,
Your Name
AI Agent: prompt = email text, output format on, system message
AI Agent: prompt = email text, output format on, system message

Step 3: Connect the Gemini Chat Model

Under the AI Agent, click + on Chat Model and pick Google Gemini Chat Model. Create a credential with your free Gemini API key and choose a model such as Gemini 2.5 Flash — fast, and available on the free tier.

Gemini Chat Model: your API key credential and Gemini 2.5 Flash
Gemini Chat Model: your API key credential and Gemini 2.5 Flash

Step 4: Add the Structured Output Parser

Click + on Output Parser and pick Structured Output Parser. Schema Type: Generate From JSON Example, and paste:

{
	"subject": "",
	"message": "",
	"reason": ""
}

Now the AI always answers in the same shape: the reply text is in output.message, and reason notes why it replied that way — handy when you check runs later.

Structured Output Parser: subject, message and reason
Structured Output Parser: subject, message and reason

Step 5: Reply to a message (action)

Click + after the AI Agent, search Gmail and pick Reply to a message:

  • Credential: the same Gmail account.
  • Resource: Message, Operation: Reply.
  • Message ID: {{ $('Gmail Trigger').item.json.id }} — reply in the same thread as the incoming email.
  • Email Type: Text, Message: {{ $json.output.message }} — the AI-written reply.
Reply to a message: same email ID, AI-written text
Reply to a message: same email ID, AI-written text

Step 6: Test and publish

The finished workflow: one trigger, two actions, two sub-nodes
The finished workflow: one trigger, two actions, two sub-nodes
  1. Send a test email to your Gmail from another address.
  2. Open the Gmail Trigger and click Fetch Test Event, then Execute workflow.
  3. Check the AI Agent’s output, then open the email thread in Gmail — the reply should be there.
  4. Happy with it? Click Publish. From now on n8n checks every minute and replies on its own.

Be careful: once published, it replies to every email the trigger picks up. Use the search filter from step 1, test with your own addresses first, and keep important conversations for a human.

Frequently asked questions

Is this Gmail auto-reply free?

Yes for small use: n8n self-hosted is free, Gmail API access is free, and the Gemini API has a free tier with rate limits. Heavy volumes may need a paid Gemini plan.

Why turn Simplify off in the Gmail Trigger?

With Simplify on, n8n returns only key fields. Turning it off gives the full email body in the text field, which the AI needs to write a good reply.

Will it reply to newsletters and spam?

It replies to whatever the trigger fetches. Add a Search filter such as -category:promotions -category:social -from:noreply to skip most automated mail.

Can I use OpenAI instead of Gemini?

Yes. Swap the Google Gemini Chat Model sub-node for OpenAI or any other chat model; the rest of the workflow stays the same.

Why does the reply land in the same thread?

The Reply to a message node uses the incoming email’s ID from the Gmail Trigger, so Gmail threads it with the original message.

What’s next?

Set up the two credentials this workflow needs: Google OAuth for Gmail and a free Gemini API key. Then try another real workflow: auto-posting to a Facebook Page with n8n.

Need a Website or System for Your Business?

Tell us what you need — we'll build, host and support it for you.

Book Free Consultation