Custom AI Agent Builder using LangChain and Gemini - n8n Workflow

Learn how to deploy a custom, stateful AI chat agent within n8n. This n8n workflow uses the specialized LangChain n8n node for prompt engineering and conversational memory management with the Gemini LLM.

Workflow Preview

Ready to automate?

Download this n8n workflow template and start using it instantly.

Who is this best for?


  • Technical users and developers familiar with Python or JavaScript logic.

  • Automation specialists looking to deploy custom, highly specialized AI personas.

  • Teams seeking advanced n8n templates for building conversational AI agents.

  • Users who need stateful conversation history managed directly within an n8n workflow using LangChain.

Overview

This sophisticated n8n workflow demonstrates how to build and deploy a complex, role-playing AI agent using the power of LangChain components directly within n8n. Unlike standard LLM calls, this automation leverages a custom code n8n node to define specific personality traits, conversational rules (e.g., response language, tone, and constraints), and utilizes robust memory management.

By combining the When chat message received n8n trigger with specialized LangChain memory and LLM nodes, this n8n workflow creates a truly persistent and engaging conversational experience. This approach provides maximum flexibility for prompt engineering and is one of the most advanced n8n templates for AI agent creation, allowing users to fine-tune every aspect of the agent’s responses while keeping conversation context intact.

How it Works

The entire process is initiated by the dedicated n8n trigger for chat applications, creating a persistent session:


  1. Initiation (The n8n Trigger): The When chat message received n8n trigger starts the execution loop whenever a user sends a message to the deployed agent. It also serves as the final response handler.

  2. Language Model Selection: The flow connects to the Google Gemini Chat Model n8n node, providing the computational backbone for generating responses.

  3. Memory Retrieval: The Store conversation history n8n node (a LangChain Buffer Window Memory) is accessed to retrieve the full context of the ongoing conversation history, ensuring the agent remembers past interactions.

  4. Custom Execution & Prompt Engineering: The core logic resides in the Construct & Execute LLM Prompt n8n node. This custom code executes a LangChain ConversationChain using the provided model and memory.

  5. Prompt Logic: The custom template within this n8n node defines the persona (a witty, aloof girlfriend named "Bunny"), strict rules (respond exclusively in Chinese, never ask questions), and ensures the crucial {chat_history} and {input} variables are preserved for LangChain to function correctly.

  6. Response: The output generated by the LangChain chain is passed back via the main output connection, which is automatically returned to the user through the initial n8n trigger.

Installation Guide

To deploy this specialized n8n workflow, follow these steps:


  1. Import the n8n template: Copy the provided JSON data and paste it into your n8n canvas via the 'New' -> 'Import from JSON' option.

  2. Configure Credentials: Set up your Google Gemini API credentials. You will need to create a new credential entry for Google Gemini (PaLM) API and input your key. This credential is required by the Google Gemini Chat Model n8n node.

  3. Activate and Test: Enable the workflow and click the 'Chat' button on the When chat message received n8n trigger node to begin interacting with your custom AI agent. Alternatively, use the provided webhook URL once the n8n workflow is activated.

Node Details

This n8n workflow relies heavily on the specialized LangChain n8n node suite for building the AI agent:

When chat message received (n8n trigger):
Function: Serves as the interactive webhook endpoint and the n8n trigger for the chat interface. It manages the user session and delivers the final response.
Key Configuration: Set to respond using the lastNode output, meaning the result from the Construct & Execute LLM Prompt n8n node will be returned.

Google Gemini Chat Model (LangChain LLM n8n node):
Function: Provides the large language model engine (Gemini) that powers the conversational ability of the agent.
Key Configuration: Uses the models/gemini-2.0-flash-exp model, configured with a temperature of 0.
7.

Store conversation history (LangChain Memory n8n node):
Function: Implements a sliding window memory buffer for the LangChain chain. This is vital for the agent to maintain context and state throughout the conversation.
Key Configuration: Uses the Buffer Window Memory type to efficiently store conversation history.

Construct & Execute LLM Prompt (LangChain Code n8n node):
Function: This critical n8n node uses custom JavaScript to instantiate and execute a LangChain ConversationChain. It handles complex prompt injection and structure.
Key Configuration: Contains the entire prompt template defining the agent's complex personality and behavioral constraints, ensuring adherence to the strict role-playing requirements defined in this n8n workflow.

Related n8n Workflows

Free

Nodes: 5 Nodes
Updated: December 26 2025
View all
Created by
shepard
shepard

Featured*