Module 3 of 6 — Advanced

AI Integration in n8n – Connect OpenAI, Gemini & Claude to Build Intelligent Automation Pipelines

Integrate the world's most powerful AI models — OpenAI GPT-4o, Google Gemini, and Anthropic Claude — directly into your n8n workflows to build truly intelligent automations.

16 min read Jan 15, 2026 5 Steps Advanced Level
Step-by-Step Instructions
1
Step 1 — Set Up AI Credentials in n8n

n8n has built-in nodes for OpenAI and can call Gemini and Claude via HTTP Request nodes. Set up your credentials first.

OpenAI: Go to Credentials → New → OpenAI. Paste your OpenAI API key. This unlocks the native "OpenAI" node which has built-in support for chat completions, embeddings, DALL-E image generation, and Whisper transcription.

Google Gemini: Create a credential of type "Header Auth". Name: x-goog-api-key. Value: your Google AI Studio API key. Use this credential with an HTTP Request node pointing to the Gemini API.

Anthropic Claude: Create a "Header Auth" credential. Name: x-api-key. Value: your Anthropic API key. Also add a second static header anthropic-version: 2023-06-01 in the HTTP Request node headers. You will call https://api.anthropic.com/v1/messages.

2
Step 2 — Build an OpenAI Content Generation Node

Add an OpenAI node to your workflow. Select "Message a Model" as the operation. Set model to gpt-4o (or gpt-4o-mini for cost efficiency). In the Messages section, add your system prompt and user message.

Use expressions to inject dynamic data into your prompts:

System: You are a professional blog writer specializing in technology.
Always write in a clear, engaging tone. Output only the article body.

User: Write a 600-word blog post about: {{ $node["Get Topic"].json.topic }}
Target audience: {{ $node["Get Topic"].json.audience }}
Keywords to include: {{ $node["Get Topic"].json.keywords.join(", ") }}

Set Max Tokens to 1500-2000 for articles. Set Temperature to 0.7 for creative content, 0.2 for factual/analytical content.

3
Step 3 — Integrate Google Gemini via HTTP Request

Add an HTTP Request node. Configure it as follows:

Method: POST
URL: https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-pro:generateContent
Auth: Header Auth (your Gemini credential)
Body Type: JSON
Body:
{
  "contents": [
    {
      "parts": [
        {
          "text": "{{ $json.prompt }}"
        }
      ]
    }
  ],
  "generationConfig": {
    "temperature": 0.8,
    "maxOutputTokens": 2048
  }
}

After this node, add a Set node to extract the text from Gemini's nested response structure: {{ $json.candidates[0].content.parts[0].text }}

4
Step 4 — Integrate Claude via HTTP Request

Add an HTTP Request node for Claude:

Method: POST
URL: https://api.anthropic.com/v1/messages
Headers:
  x-api-key: (your Anthropic credential)
  anthropic-version: 2023-06-01
  content-type: application/json
Body:
{
  "model": "claude-sonnet-4-6",
  "max_tokens": 2048,
  "system": "{{ $node["Config"].json.system_prompt }}",
  "messages": [
    {
      "role": "user",
      "content": "{{ $node["Input"].json.user_message }}"
    }
  ]
}

Extract Claude's response with: {{ $json.content[0].text }}

When to use which model: GPT-4o excels at structured outputs and function calling. Gemini 1.5 Pro handles very long documents (1M token context). Claude excels at nuanced writing, analysis, and following complex instructions precisely.

5
Step 5 — Build an AI Agent with Memory

An AI agent in n8n maintains conversation history across turns. Store the conversation history in a Code node variable or in a MySQL table.

Pattern for a persistent AI agent:

// Code Node: Build conversation history
const history = $node["Get History"].json.messages || [];
const newMessage = { role: "user", content: $json.user_input };
const updatedHistory = [...history, newMessage];

return [{
  messages_for_api: updatedHistory,
  messages_for_storage: updatedHistory
}];

Send messages_for_api to the OpenAI/Claude node as the messages array. After receiving the response, append the assistant's reply to the history and save back to MySQL. This creates a multi-turn AI agent that remembers the full conversation context.

Sample Workflow Diagram
Real-World Automation Example
Real-World Example: Automated Multi-AI Content Factory

A digital marketing agency produces 50 SEO articles per week. Using n8n with three AI models working in parallel, they reduced content creation time by 80% while maintaining quality through AI-powered review.

1
Airtable holds the content calendar with topics, target keywords, and audience profiles.
2
n8n triggers daily at 6 AM, fetches 5 pending topics.
3
GPT-4o writes the article body with correct tone and structure.
4
Gemini generates SEO title, meta description, and keyword variants.
5
Claude reviews both outputs for factual accuracy and brand tone, scoring 1-10.
6
Articles scoring 8+ auto-publish to WordPress via REST API. Lower scores route to a Slack review queue.
7
Airtable record updated to "published" or "needs review" — full audit trail maintained.
Frequently Asked Questions
Which AI model should I use in n8n — GPT-4o, Gemini, or Claude?
It depends on the task. GPT-4o is best for structured JSON outputs, function calling, and code generation. Gemini 1.5 Pro handles the longest documents and is excellent for summarizing large PDFs. Claude is best for nuanced writing, following complex multi-step instructions, and analysis tasks requiring careful reasoning. For content pipelines, using two or three models in series (generate → review) consistently produces better results than any single model alone.
How do I avoid high AI API costs in my n8n workflows?
Use gpt-4o-mini or gemini-1.5-flash for simple extraction tasks and gpt-4o or claude-sonnet only for complex generation. Add a Code node before AI calls to check if the task actually needs AI (skip the call if the data is already in the right format). Cache AI outputs in a database and check for duplicates before making new calls. Batch multiple small prompts into a single API call when possible.
Can I build a chatbot using n8n AI nodes?
Yes. The pattern: Webhook receives user message → Code node retrieves conversation history from MySQL → OpenAI/Claude generates response → Code node appends assistant reply to history → MySQL updates history → Webhook responds with AI reply. You can also use n8n's AI Agent node (available in newer versions) which handles the memory loop automatically using LangChain under the hood.