How to Connect n8n to OpenAI: The Complete Guide to AI Workflows (2025)

Connecting n8n to OpenAI turns your automations from "move data from A to B" into "understand, decide, and act". Classify emails, summarize documents, extract structured data, write replies — all inside a workflow. Here's how to set it up properly, and how to keep the bill under control.

Step 1: Get your OpenAI API key

Go to platform.openai.com/api-keys, create a new secret key, and copy it. Set a monthly usage limit under Billing → Limits before you do anything else — this is the single most important step for avoiding surprises.

Step 2: Add the credential in n8n

In n8n, create a new OpenAI credential and paste your key. This credential is shared across all OpenAI-related nodes: the OpenAI node, the AI Agent node, and the embeddings nodes.

Step 3: Choose your approach

n8n gives you two levels of abstraction:

  • OpenAI node — direct calls for a single task: "summarize this", "classify that". Simple and predictable.
  • AI Agent node — a reasoning loop with access to tools (HTTP requests, other nodes, memory). Use it when the task requires multiple steps or decisions.

Ready to automate?

Cut your AI bill by up to 60% — get the n8n LLM Router that sends each task to the cheapest model that can handle it.

Get the Template on Gumroad →

The cost trap (and how to avoid it)

The #1 mistake: routing every task to GPT-4o. Most workflow tasks — classification, extraction, short summaries — run perfectly on a smaller, cheaper model. A practical routing strategy:

TaskRecommended modelWhy
Classification / routingGPT-4o-mini10-20x cheaper, plenty accurate
Data extraction (JSON)GPT-4o-miniStructured output works well
Long-form writingGPT-4oQuality justifies cost
Complex reasoningGPT-4o / o-seriesMulti-step logic

Getting reliable JSON output

For extraction workflows, always request structured output. Set the response format to JSON and give the model an exact schema in the system prompt. In n8n, add a Code node after the AI call to validate the JSON before using it downstream — LLMs occasionally drift.

Prompt engineering that survives production

  • Be explicit about format: "Return only valid JSON with keys x, y, z. No prose."
  • Give examples: one or two examples in the prompt beat paragraphs of instructions.
  • Constrain the model: "If the answer isn't in the provided text, return null" prevents hallucination.

Handling errors and retries

OpenAI APIs occasionally return 429 (rate limit) or 500 errors. In the node settings, enable "Retry on Fail" with 3 attempts and a delay. Add an error branch that alerts you on Slack if all retries fail, so a silent API outage doesn't break your automation unnoticed.

Where to start

Pick one repetitive text task — triaging inbound email, summarizing meeting notes, tagging support tickets — and automate just that. Once it's reliable, expand. AI workflows compound: each one you build makes the next one easier.


Ready to automate? Get this template on Gumroad

This ready-to-import n8n workflow saves you hours of setup. Import it, connect your accounts, and go live.

Get the Template on Gumroad →