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
- n8n running on your computer or server — see Part 2.
- A Gmail credential in n8n — follow How to create Google OAuth credentials for Gmail.
- A free Gemini API key — follow How to get a free Gemini API key.
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).

Step 1: Add the Gmail Trigger
- Create a new workflow and click Add first step.
- In “What triggers this workflow?”, search for Gmail and pick it.

- Credential: choose your Gmail account (create it here if you haven’t).
- Poll Times: Every Minute. Event: Message Received.
- Simplify: turn it off, so the full email body arrives in the
textfield for the AI.

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)
- Click the + on the right of the Gmail Trigger. The “What happens next?” list opens — these are actions.
- Open AI and pick AI Agent.

- Source for Prompt: Define below. Prompt:
{{ $json.text }}— the email text from the trigger. - Require Specific Output Format: on.
- 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

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.

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.

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.

Step 6: Test and publish

- Send a test email to your Gmail from another address.
- Open the Gmail Trigger and click Fetch Test Event, then Execute workflow.
- Check the AI Agent’s output, then open the email thread in Gmail — the reply should be there.
- 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.
