This workflow automates analyzing Gmail threads and drafting AI-powered replies with the new model Anthropic Sonnet 4.5.
This workflow automates the process of analyzing incoming emails and generating context-aware draft replies by examining the entire email thread.
This workflow automates the process of analyzing incoming emails and generating context-aware draft replies by examining the entire email thread.
Trigger & Initial Filtering: The workflow is automatically triggered every minute by the Gmail Trigger node, which detects new emails. For each new email, it immediately performs a crucial first step: it uses an AI Email Classifier to analyze the email snippet. The AI determines if the email is a legitimate message that warrants a reply (categorized as "ok") or if it's spam, a newsletter, or an advertisement. This prevents the system from generating replies for unwanted emails.
Context Aggregation: If an email is classified as "ok," the workflow fetches the entire conversation thread from Gmail using the threadId
. A Code Node then processes all the messages in the thread, structuring them into a consistent format that the AI can easily understand.
AI-Powered Draft Generation: The structured conversation history is passed to the Replying email Agent with Sonnet 4.5. This agent, powered by a language model, analyzes the entire thread to understand the context and the latest inquiry. It then drafts a relevant and coherent HTML email reply. The system prompt instructs the AI not to invent information and to use placeholders for any missing details.
Draft Creation: The final step takes the AI-generated reply and the original email's metadata (subject, recipient, threadId) and uses them to create a new draft email in Gmail. This draft is automatically placed in the correct email thread, ready for the user to review and send.
To implement this automated email reply system, you need to configure the following:
Configure Gmail & OpenAI Credentials: Ensure the following credentials are set up in your n8n instance:
Gmail OAuth2 Credentials: The workflow uses the same Gmail account for the trigger, fetching threads, and creating drafts. Configure this in the "Gmail Trigger," "Get a thread," and "Create a draft" nodes.
OpenAI API Credentials: Required for both the "Email Classifier". Provide your API key in the respective OpenAI Chat Model nodes.
Anthropic API Credentials: Required for the main "Replying email Agent." Provide your API key in the respective Antrhopic Chat Model nodes.
Review AI Classification & Prompting:
Contact me for consulting and support or add me on Linkedin.