Gmail Trigger , Email AI agent

Hey, i’m trying to set a AI agent that will take care of my Customer service emails for me. Everything is going great but one small problem is that when the customer answer back to me in the thread then the Gmail trigger snippets with the new email content also contains along with the customer message my previous replies which can cause error with the LLM…

anyone knows any ways to EXCLUDE the previous replie from the snippet and only include the body of the new message in the thread ?

thanks a lot

Hello @TonyKZ,
welcome here!
you can implement a strategy in your n8n workflow to extract only the latest message content before passing it to your AI agent.

  1. set up the Gmail Trigger node to monitor incoming emails. Issue with Gmail Trigger Node: Unable to Access Full Previous Conversation Context
  2. after triggering, use the Gmail node with the “Get Message” operation to retrieve the full content of the latest email. Ensure that the “Simplify” option is turned off to access the raw email data.
  3. add a Function node to process the email body and remove quoted previous messages. Here’s a sample code snippet you can use (for example):
const body = $json.body;

const cleaned = body
  .split(/(?:On\s.*?wrote:|From:\s)/)[0]  // basic email quote pattern
  .replace(/^\s*>.*/gm, '')               // remove '>' quoted lines
  .trim();

return [{ json: { cleanedBody: cleaned } }];

This script attempts to split the email body at common reply indicators and removes lines that are quoted from previous messages.
4. use the output from the Function node as the input for your AI agent, ensuring it only processes the new message content.