Lesson 4 of 6·10 min read

AI Nodes: LLM Integration

n8n offers native AI nodes that enable direct integration with large language models — without writing a single line of code. In this lesson, you'll learn how to integrate OpenAI, Anthropic, and Ollama into your workflows.

Available AI Nodes

NodeFunctionProvider
AI AgentAutonomous agent with tool useOpenAI, Anthropic
Basic LLM ChainSimple LLM call with promptAll providers
Summarization ChainAutomatic text summarizationAll providers
Information ExtractorExtract structured data from textAll providers
Text ClassifierCategorize textAll providers
Sentiment AnalysisDetect sentimentAll providers
EmbeddingsGenerate vector representationsOpenAI, Ollama

Credential Management

Setting Up OpenAI

  1. Navigate to CredentialsNew Credential
  2. Select OpenAI API
  3. Paste your API key
  4. Optional: Organization ID and base URL (for Azure OpenAI)

Setting Up Anthropic

  1. CredentialsNew CredentialAnthropic
  2. Paste API key from the Anthropic Console
  3. Select the desired model (Claude Sonnet 4, Claude Opus 4)

Ollama (Self-Hosted)

For maximum data control: Ollama runs locally without API costs.

# Install Ollama and pull model
ollama pull llama3.1
ollama pull nomic-embed-text

In n8n: Set base URL to http://ollama:11434 (Docker network) or http://host.docker.internal:11434 (local installation).

Practical tip: Never store API keys in workflow data. Use n8n's credential management exclusively — keys are stored encrypted with the N8N_ENCRYPTION_KEY.

Prompt Templates

AI nodes support dynamic prompts with expressions:

You are an experienced analyst. Analyze the following customer message:

Message: {{ $json.message }}
Customer: {{ $json.customer_name }}
Product: {{ $json.product }}

Respond in JSON format with these fields:
- sentiment: "positive", "neutral", or "negative"
- category: Main category of the inquiry
- priority: "high", "medium", or "low"
- summary: Summary in one sentence

Using System Prompts Effectively

  • System prompt: Defines the model's role and behavior
  • User prompt: Contains dynamic data from the workflow
  • Separation enables reusability and easy testing

Structured Output Parsing

Enforcing JSON Output

Use the Output Parser (sub-node) to get the LLM result as structured JSON:

{
  "sentiment": "positive",
  "category": "Product question",
  "priority": "medium",
  "summary": "Customer asks about availability of the premium plan."
}

Schema Definition

Define a JSON schema so n8n automatically validates the response. Malformed responses are caught and can be re-requested.

Best Practices for AI Nodes

  1. Keep temperature low (0.0–0.3) for consistent, factual answers
  2. Limit max tokens — prevents unnecessarily long and expensive responses
  3. Enable retry on failure (rate limits, timeouts)
  4. Enable logging — store prompts and responses for audit and debugging
  5. Model selection: Claude Sonnet for fast tasks, Claude Opus for complex analyses

Practical tip: Test prompts in the provider's playground first before building them into n8n. This way you iterate faster and save API costs during development.