How to Connect an AI Model to Your n8n Workflow
To connect an AI model to n8n, add an AI node (OpenAI or Anthropic), enter your API key as a credential, write a prompt in the node, and map your workflow data into it. The model's output appears in the node result and is available to downstream nodes. This lets you build automations that classify, summarise, translate, or generate text as part of a larger workflow.
You can connect AI models like OpenAI GPT or Anthropic Claude directly to n8n workflows. This guide shows how to classify incoming emails with AI and send an automatic reply.
What You Need
To connect an AI model to n8n, you need two things: an n8n instance (cloud or self-hosted) and an API key from your chosen AI provider. For OpenAI, sign up at platform.openai.com. For Claude, sign up at console.anthropic.com. Both offer trial credits for new accounts. Store your API key in n8n’s credential manager, not directly in workflow nodes, so it is reusable and does not appear in execution logs. For background on what the Claude API does, read What is the Claude API.
Step 1: Create a Workflow and Add a Trigger
Open n8n and create a new workflow. For this example, use a Gmail trigger: “When email received”. Configure it to watch your inbox and set a polling interval. If you prefer to trigger the workflow via an incoming HTTP call instead, see How to Set Up a Webhook in n8n. Run a test to confirm the trigger fires and that you can see the email content in the node output.
Step 2: Add an Anthropic or OpenAI Node
Click the ”+” button and search for “Anthropic” or “OpenAI”. Add the node. In the credentials field, select your stored API key. Choose the model you want to use (Sonnet is a good balance of cost and capability for classification tasks). Set the operation to “Message a model”.
Step 3: Write Your Prompt
In the “User message” field, write your prompt. Combine a fixed instruction with dynamic data from the email trigger. Switch the field to expression mode so n8n interpolates the values. Example:
Classify this customer email into one of these categories: Billing, Technical Support, General Enquiry.
Reply with only the category name, nothing else.
Email subject: {{ $json.subject }}
Email body: {{ $json.text }}
The AI returns a single category name that you use in the next step.
Step 4: Route the Workflow Based on the AI Response
Add a Switch node after the AI node. Set the condition to match the AI’s output text. Create a case for each category (Billing, Technical Support, General Enquiry). Connect each output to a different branch: for example, Billing routes to a task creation node, Technical Support routes to a ticket system, and General Enquiry routes to a draft-reply node.
Step 5: Send the Automated Response
At the end of each branch, add a Gmail node to send a reply. Use a fixed template per category, or add a second AI node to generate a personalised draft based on the email content. Test the full flow by sending a sample email and verifying the output at each step. Once everything works, activate the workflow.
Our tip: Always include a fallback branch for unexpected AI output. If the model returns something other than your defined categories (it occasionally will), route that case to a manual review queue rather than letting it fall through silently.
Frequently asked questions
Which AI models can I use in n8n?
n8n has built-in nodes for OpenAI (GPT-3.5, GPT-4, GPT-4o), Anthropic Claude (Haiku, Sonnet, Opus), Hugging Face, and Google Gemini, among others. For any model without a dedicated node, you can use the HTTP Request node to call the API directly. The AI Agent node in n8n is a more advanced option that lets the model call other nodes as tools and iterate towards a goal.
Do I need an API key to use AI in n8n?
Yes. You need an API key from the AI provider you want to use. For OpenAI, create an account at platform.openai.com and generate a key. For Claude, create an account at console.anthropic.com. In n8n, you add the key as a credential, and any node of that type can then use it. The key is stored securely in n8n's credential storage and is not visible in workflow logs.
How do I write a good prompt for an AI node in n8n?
In a workflow context, prompts usually combine a fixed instruction with variable data from previous nodes. For example: 'Classify the following customer email into one of these categories: Billing, Technical Support, Sales. Reply with only the category name. Email: {{ $json.emailBody }}'. The key is to be specific about the output format, since the response feeds into downstream nodes. Asking for a single word or a JSON object is easier to parse than a free-form paragraph.
Can n8n use AI to decide which path a workflow takes?
Yes. Have the AI node return a simple value (a category, yes/no, a priority level) and then connect an If or Switch node that routes the workflow based on that value. For example: classify an email as urgent or standard, then route urgent emails to one branch and standard emails to another. This pattern is reliable as long as you instruct the model to return a consistent, predictable format.