LangChain Nodes
Relevant source files
The following files were used as context for generating this wiki page:- packages/@n8n/nodes-langchain/nodes/agents/Agent/Agent.node.ts
- packages/@n8n/nodes-langchain/nodes/llms/LMChatOpenAi/LmChatOpenAi.node.ts
- packages/@n8n/nodes-langchain/nodes/vector_store/VectorStorePinecone/VectorStorePinecone.node.ts
- packages/@n8n/nodes-langchain/nodes/chains/ChainRetrievalQA/ChainRetrievalQa.node.ts
- packages/@n8n/nodes-langchain/package.json
- packages/@n8n/nodes-langchain/nodes/code/Code.node.ts
- packages/@n8n/ai-workflow-builder.ee/evaluations/chains/evaluators/connections-evaluator.ts
- packages/@n8n/ai-workflow-builder.ee/src/tools/prompts/main-agent.prompt.ts
The n8n-nodes-langchain package provides a suite of nodes that integrate the LangChain framework into n8n, enabling the creation of complex AI-powered workflows. These nodes cover a wide spectrum of AI functionalities, including agents, chains, language models, document loaders, vector stores, and more, allowing users to build sophisticated applications like Retrieval-Augmented Generation (RAG) systems and autonomous agents directly within the n8n canvas.
A key architectural concept for LangChain nodes is the distinction between data flow and capability provision. This is managed through a specific connection model that uses both main and ai_* connection types. Understanding this model is crucial for correctly wiring nodes and building functional AI workflows.
Sources: packages/@n8n/nodes-langchain/README.md, packages/@n8n/ai-workflow-builder.ee/evaluations/chains/evaluators/connections-evaluator.ts:4-8
Architecture and Connection Model
LangChain nodes utilize a dual-connection system to manage both the flow of data and the provision of AI capabilities.
Sources: packages/@n8n/ai-workflow-builder.ee/evaluations/chains/evaluators/connections-evaluator.ts:10-12
Connection Types
There are two primary categories of connections:
| Type | Description |
|---|---|
main | Carries run-time data between workflow nodes. This represents the primary execution path, flowing from a node that produces data to a node that consumes it. |
ai_* | Provides capabilities from a "sub-node" to a "parent" node. The sub-node is always the source of the connection, and the parent is the target. These connections augment a node with abilities like language models, tools, or memory. |
The following diagram illustrates the two connection types. The main connection passes data through the workflow, while ai_* connections provide capabilities to the AI Agent node.
Node Archetypes
Hybrid Nodes
These nodes can appear on both the main data path and the ai_* capability network. A common example is a Vector Store, which might receive data via a main connection to be inserted, while also connecting to an AI Agent via an ai_tool connection to be used for retrieval.
Sources: packages/@n8n/ai-workflow-builder.ee/evaluations/chains/evaluators/connections-evaluator.ts:27-32
Capability-Only Sub-nodes
These nodes exist purely to provide ai_* capabilities and are not part of the main data flow. They should not have main inputs or outputs. Examples include:
- Document Loaders
- Text Splitters
- Embeddings
- Language Models (LLMs)
- Output Parsers
- Tool nodes
Sources: packages/@n8n/ai-workflow-builder.ee/evaluations/chains/evaluators/connections-evaluator.ts:34-38
Node Categories
The @n8n/n8n-nodes-langchain package organizes nodes into several functional categories. The full list of nodes is defined in the package's package.json file.
| Category | Description | Example Nodes |
|---|---|---|
| Agents | Orchestrate tasks using LLMs and Tools. | Agent, AgentTool, OpenAiAssistant |
| Chains | Combine LLMs with other components for specific tasks. | ChainRetrievalQa, ChainSummarization, ChainLlm |
| Document Loaders | Load data from various sources as documents. | DocumentDefaultDataLoader, DocumentGithubLoader |
| Embeddings | Create vector representations of text. | EmbeddingsOpenAI, EmbeddingsCohere |
| LLMs | Interface with Large Language Models. | LmChatOpenAi, LmChatAnthropic, LmChatOllama |
| Memory | Add state to chains and agents. | MemoryBufferWindow, MemoryMotorhead, MemoryPostgresChat |
| Output Parsers | Structure the output of LLMs. | OutputParserStructured, OutputParserItemList |
| Retrievers | Fetch documents to pass to an LLM. | RetrieverVectorStore, RetrieverMultiQuery |
| Text Splitters | Split long texts into smaller chunks. | TextSplitterRecursiveCharacterTextSplitter, TextSplitterTokenSplitter |
| Tools | Functions that agents can use to interact with the world. | ToolCalculator, ToolHttpRequest, ToolVectorStore |
| Vector Stores | Store and search over vector embeddings. | VectorStorePinecone, VectorStoreMilvus, VectorStoreInMemory |
| Triggers | Start workflows based on chat interactions. | ManualChatTrigger, ChatTrigger |
Sources: packages/@n8n/nodes-langchain/package.json:42-120, packages/@n8n/nodes-langchain/nodes/chains/ChainSummarization/ChainSummarization.node.ts:12-15
Common Workflow Patterns
Retrieval-Augmented Generation (RAG)
The RAG pattern allows an LLM to answer questions using information from a private knowledge base. This is a critical pattern in LangChain workflows.
The data flow for a RAG workflow is as follows:
- A data source (e.g., HTTP Request, Read Binary File) provides documents.
- This data is passed to a
Vector Storenode via amainconnection for insertion. - The
Vector Storeis augmented with capabilities from other nodes:- A
Document Loaderconnects viaai_documentto process the incoming data. - An
Embeddingsnode connects viaai_embeddingto generate embeddings for the documents. - A
Text Splittercan connect to theDocument Loaderviaai_textSplitterto chunk documents.
- A
- The
Vector Store(in tool mode) then connects to anAI Agentvia anai_toolconnection, making the knowledge base available to the agent.
The following diagram shows a typical RAG workflow for populating a vector store.
Agent Ecosystem
An agent uses an LLM to decide which actions to take. These actions are provided by Tool nodes.
- Language Model: An LLM node (e.g.,
LmChatOpenAi) connects to theAI Agentvia theai_languageModelinput. - Tools: One or more Tool nodes (e.g.,
ToolCalculator,ToolVectorStore) connect to theAI Agentvia theai_toolinput. - Memory: A Memory node (e.g.,
MemoryBufferWindow) connects to theAI Agentvia theai_memoryinput to provide conversation history.
LangChain Code Node
A unique and powerful node is the LangChain Code node (@n8n/n8n-nodes-langchain.code), which allows for the execution of arbitrary JavaScript code within a sandboxed environment with access to LangChain libraries.
Features
- Dynamic I/O: Inputs and outputs can be dynamically configured by the user, allowing the node to accept and provide various
mainorai_*connection types. - Sandboxed Execution: Code is run in a
JavaScriptSandbox, providing a secure execution context. - LangChain Access: The sandbox's module resolver is configured to allow importing
langchainand@langchain/*modules. - Dual Execution Modes:
- Execute: Runs code for the main data flow, used when a
Maininput and output are present. - Supply Data: Runs code to provide data for non-
Mainoutputs (i.e., to act as a capability provider).
- Execute: Runs code for the main data flow, used when a
Sources: packages/@n8n/nodes-langchain/nodes/code/Code.node.ts:10-13, 44-59, 114-142
Default Code Snippets
The node comes with default code to guide users.
Default Execute Code:
This snippet demonstrates creating a simple chain with a prompt and an LLM from an input connection.
// packages/@n8n/nodes-langchain/nodes/code/Code.node.ts:38-49
const { PromptTemplate } = require('@langchain/core/prompts');
const query = 'Tell me a joke';
const prompt = PromptTemplate.fromTemplate(query);
// If you are allowing more than one language model input connection (-1 or
// anything greater than 1), getInputConnectionData returns an array, so you
// will have to change the code below it to deal with that. For example, use
// llm[0] in the chain definition
const llm = await this.getInputConnectionData('ai_languageModel', 0);
let chain = prompt.pipe(llm);
const output = await chain.invoke();
return [ {json: { output } } ];
Default Supply Data Code:
This snippet shows how to create and return a LangChain tool, in this case, a Wikipedia tool.
// packages/@n8n/nodes-langchain/nodes/code/Code.node.ts:51-52
const { WikipediaQueryRun } = require( '@langchain/community/tools/wikipedia_query_run');
return new WikipediaQueryRun();
Sources: packages/@n8n/nodes-langchain/nodes/code/Code.node.ts:38-52
Summary
The LangChain nodes for n8n provide a powerful toolkit for building AI-driven automations. By leveraging a specialized connection model that separates data flow (main) from capability provisioning (ai_*), users can construct complex systems like RAG pipelines and autonomous agents. The extensive list of nodes, from agents and chains to vector stores and tools, offers modular components for various AI tasks. The LangChain Code node further extends this flexibility, enabling custom logic and direct interaction with the LangChain.js library, making the possibilities for AI workflow automation nearly limitless.