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.
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.
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 }}
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.
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.
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.