Language Models (LLMs)
Relevant source files
The following files were used as context for generating this wiki page:
- packages/@n8n/workflow-sdk/src/prompts/best-practices/guides/knowledge-base.ts
- packages/@n8n/ai-workflow-builder.ee/src/prompts/agents/discovery.prompt.ts
- packages/@n8n/ai-workflow-builder.ee/src/code-builder/prompts/index.ts
- packages/cli/src/modules/agents/builder/agents-builder-prompts.ts
- packages/cli/src/modules/mcp/tools/workflow-builder/mcp-instructions.ts
- packages/quality/testing/playwright/expectations/workflow-builder/1768236115215-unknown-host-POST-_v1_messages-343a818e.json
Language Models (LLMs) function as the cognitive engine within n8n's AI ecosystem. They enable workflows to perform complex reasoning, text analysis, and content generation. Within the n8n architecture, LLMs are integrated through specialized provider nodes (e.g., OpenAI, Anthropic, Google Gemini) that connect to AI Agents and Chains. These models process unstructured data, interact with external tools, and support Retrieval-Augmented Generation (RAG) by interfacing with vector databases and memory components.
Sources: packages/@n8n/ai-workflow-builder.ee/src/prompts/agents/discovery.prompt.ts:48-61, packages/cli/src/modules/mcp/tools/workflow-builder/mcp-instructions.ts:103-115
AI Agent and Model Architecture
The n8n AI framework uses a modular architecture where the AI Agent node acts as an orchestrator. This node requires a Chat Model sub-node to provide the underlying intelligence. LLMs are categorized by provider and specific API capabilities, such as embeddings or image generation.
Model Categories and Usage
| Category | Node/Model Type | Use Case |
|---|---|---|
| Reasoning | AI Agent + Chat Model | Text analysis, summarization, and multi-step reasoning. |
| Embeddings | Embeddings Node | Generating vector representations for semantic search. |
| Multimodal | OpenAI/Gemini Node | DALL-E (Images), Whisper (Audio), or Sora (Video). |
| Specialized | Text Classifier | Simple categorization with fixed categories. |
Sources: packages/@n8n/ai-workflow-builder.ee/src/prompts/agents/discovery.prompt.ts:48-58, packages/@n8n/ai-workflow-builder.ee/src/prompts/agents/discovery.prompt.ts:145-155
Multi-Agent Systems
Multi-agent systems utilize the AI Agent Tool, which contains an embedded AI Agent. This allows a primary agent to call sub-agents for specialized tasks like research or writing. Each Agent Tool requires its own Chat Model connection.
This diagram shows the relationship between a main AI Agent and its sub-agents via tools, highlighting that each reasoning unit requires an LLM connection.
Sources: packages/@n8n/ai-workflow-builder.ee/src/prompts/agents/discovery.prompt.ts:63-75, packages/@n8n/ai-workflow-builder.ee/src/code-builder/prompts/index.ts:303-315
Model Implementation and Configuration
LLMs are configured using specific parameters that control their behavior and output format. Key configuration elements include temperature, system prompts, and structured output parsing.
Core Configuration Parameters
- Temperature: Controls randomness. Use 0-0.3 for factual accuracy (e.g., Q&A) and 0.7-1.0 for creative tasks.
- System Prompt: Defines the model's persona and constraints (e.g., "Answer using only the provided knowledge base").
- Structured Output Parser: A specialized sub-node connected to the AI Agent to ensure the LLM returns valid JSON.
Sources: packages/@n8n/workflow-sdk/src/prompts/best-practices/guides/knowledge-base.ts:31-36, packages/@n8n/ai-workflow-builder.ee/src/prompts/agents/discovery.prompt.ts:59-61
Data Flow for Retrieval-Augmented Generation (RAG)
The RAG pattern extends LLM capabilities by providing external context from a vector store.
The RAG flow illustrates how the LLM (via the Chat Model) processes a user query in combination with context retrieved from a vector database.
Sources: packages/@n8n/workflow-sdk/src/prompts/best-practices/guides/knowledge-base.ts:6-14, packages/@n8n/ai-workflow-builder.ee/src/prompts/agents/discovery.prompt.ts:241-245
Best Practices and Performance
To ensure reliable LLM performance, n8n enforces several best practices regarding model consistency and resource management.
Technical Constraints
- Consistent Embeddings: You must use the same embedding model for both document indexing and user queries to maintain semantic search accuracy.
- Token Limits: Monitor the combined length of query, context, and response to avoid exceeding model limits (e.g., GPT-4 32k tokens).
- Execution Mode: Run large indexing or complex LLM operations in production mode to prevent the workflow editor from freezing.
Sources: packages/@n8n/workflow-sdk/src/prompts/best-practices/guides/knowledge-base.ts:114-121, packages/@n8n/workflow-sdk/src/prompts/best-practices/guides/knowledge-base.ts:153-157
Implementation Guidelines
| Practice | Recommendation | Rationale |
|---|---|---|
| Temperature | 0 to 0.3 | Minimizes hallucinations in factual Q&A. |
| Tool Names | Specific (e.g., "HR Policy Search") | Prevents the agent from misusing tools. |
| Memory | Window Buffer Memory | Maintains conversation history without exceeding token limits. |
| Output Parsing | Structured Output Parser | Ensures programmatic access to LLM results without manual regex. |
Sources: packages/@n8n/workflow-sdk/src/prompts/best-practices/guides/knowledge-base.ts:35, packages/@n8n/workflow-sdk/src/prompts/best-practices/guides/knowledge-base.ts:173-177, packages/@n8n/ai-workflow-builder.ee/src/prompts/agents/discovery.prompt.ts:59-61
Integration with External Tools
LLMs in n8n can interact with external services via Tool nodes. These are distinct from regular nodes because the LLM decides autonomously when and if to invoke them based on the provided reasoning.
- Research Tools: SerpAPI or Perplexity Tool for web search.
- Communication Tools: Slack or Gmail Tool for messaging.
- Utility Tools: Calculator Tool for mathematical operations.
Sources: packages/@n8n/ai-workflow-builder.ee/src/prompts/agents/discovery.prompt.ts:227-238, packages/quality/testing/playwright/expectations/workflow-builder/1768236115215-unknown-host-POST-_v1_messages-343a818e.json:204-207