Gmail Intelligence Engine for RAG Knowledge Bases - n8n Workflow

Use this powerful n8n workflow to automatically parse, analyze, and structure incoming Gmail messages using LLMs (GPT/Mistral). Turn unstructured email data into a Mem0 RAG knowledge base asset. Explore more n8n templates today.

Workflow Preview

Ready to automate?

Download this n8n workflow template and start using it instantly.

Who is this best for?

Sales and Support Teams: Users needing to automatically log detailed, structured customer interaction summaries based on email conversations. Data Engineers: Professionals looking for reliable n8n templates to convert unstructured text (emails) into clean JSON records. Automation Specialists: Anyone deploying complex, multi-stage n8n automation involving Langchain agents, structured parsing, and external memory systems (RAG). Developers using Mem0: Users integrating n8n with Mem0 or other vector databases for persistent memory agents.

Overview

Managing high-volume inboxes requires more than simple archiving; it demands turning communication into intelligence. This n8n workflow provides an autonomous solution, creating an 'always-on' engine that processes, analyzes, and structures every incoming email in real-time. By leveraging a complex n8n agent powered by large language models, the automation extracts the core message, sentiment, red flags, and keywords. The most critical step is storing this cleaned, structured data in a persistent memory layer like Mem0, creating an invaluable, historical intelligence file tied to the sender's email address. This sophisticated approach transforms reactive email management into a proactive intelligence system, demonstrating the power of n8n automation.

How it Works

This advanced n8n workflow operates as an autonomous intelligence engine, ensuring every new email becomes a valuable data asset:


  1. Ingestion via n8n Trigger: The process begins with the Gmail n8n trigger, which constantly monitors the specified inbox. When a new email is detected, the n8n workflow is immediately executed.

  2. Data Preparation: The Set Target Email n8n node performs crucial preliminary work, extracting essential components of the email—including the subject, the plain text body, and the sender's address (headers.from)—to create a clean input payload for the AI analysis.

  3. AI Analysis with Context: The prepared email content is fed into the Langchain Parse_Email Agent. This powerful n8n node, leveraging an OpenAI LLM (e.g., GPT-4), reads the email. Critically, it uses the Window Buffer Memory n8n node, keyed by the email threadId, to recall the last 10 messages in that conversation, providing rich context for its analysis.

  4. Guaranteed Structured Output: The Agent's initial analysis (which includes core message extraction, sentiment classification, and keyword identification) is passed to a quality control mechanism. This involves an Auto-fixing Output Parser linked to a second Parsing LLM (Mistral). If the primary model’s output fails to match the strict JSON schema defined in the Structured Output Parser, the auto-fixer uses the secondary LLM to correct the response, guaranteeing perfect data structure for the final action.

  5. RAG Knowledge Base Update: The perfectly structured JSON data is then sent to Mem0 via a specialized n8n node or an HTTP Request n8n node. The core message, along with the extracted sentiment and keywords, is logged against the unique user ID (the sender's email address). This action builds a persistent, long-term memory record, turning the raw email into a searchable component of a RAG knowledge base. This completes the core n8n workflow.

Installation Guide

To deploy this n8n template, follow these steps:


  1. Import the n8n Workflow: Copy the provided JSON code and import it directly into your n8n instance via the 'Workflows' > 'New' > 'Import from JSON' option.

  2. Configure Gmail Credentials: Click on the FullEmail Gmail n8n trigger node and set up your Gmail OAuth2 credentials, ensuring n8n has permission to monitor your inbox.

  3. Set Up LLM Credentials: Configure the credentials for the llm of your choice (OpenAI API key) and the Parsing LLM (Mistral API key).

  4. Mem0 Integration: Set up the credentials for Mem0.ai. This is crucial for the final data storage nodes (AddParsed email to memory and email to mem0). If using the HTTP Request n8n node, ensure the API key is configured under the 'HTTP Header Auth' credential type as specified in the n8n workflow.

  5. Activate the n8n Workflow: Ensure all credentials are set, and then activate the n8n workflow to begin monitoring incoming emails in real-time.

Node Details

FullEmail (Gmail Trigger n8n node):
Function: Serves as the starting point, initiating the n8n workflow whenever a new email arrives in the monitored Gmail inbox.
Key Configuration: Configured to poll the inbox every minute (everyMinute).

Set Target Email (Set n8n node):
Function: Cleans and structures the raw email payload, extracting key variables (like threadId, subject, and sender address) to simplify data access for the subsequent AI processing steps.

ParseEmail Agent (Langchain Agent n8n node):
Function: The central intelligence unit. It processes the email text based on detailed system instructions to extract the core message, sentiment, and red flags. This is the main n8n node utilizing the LLMs and parsers.
Key Configuration: Uses dynamic inputs, leverages the llm of your choice (OpenAI), and requires specific output parsing via the linked Auto-fixing Parser.

llm of your choice (ChatOpenAi n8n node):
Function: Provides the computational power for the primary agent analysis. The JSON specifies gpt-4.1-nano for this task.

Window Buffer Memory (Langchain Memory n8n node):
Function: Maintains conversational history for the agent. It uses the email threadId to store and recall the last 10 messages, giving the AI essential context.

Structured Output Parser / Auto-fixing Output Parser (Langchain Parser n8n nodes):
Function: This critical parsing chain guarantees the AI's output conforms to a strict JSON structure, making the data highly reliable. The Auto-fixing n8n node uses a secondary LLM (Mistral) to fix any formatting errors from the primary analysis.

Parsing LLM (ChatMistralCloud n8n node):
Function: The fallback LLM used by the Auto-fixing parser to correct output formatting, ensuring robust and reliable data delivery.

email to mem0 (HTTP Request n8n node):
Function: Final action that sends the fully processed and structured data (core message, sentiment, keywords) to the Mem0 RAG knowledge base API, completing the storage process for this n8n workflow.

Related n8n Workflows

Free

Nodes: 11 Nodes
Updated: December 26 2025
View all
Created by

Account Executive by day , Noco builder for fun at night and always a proud dad of Togo the Samoyed.

Featured*