n8n + OpenAI: The Integration Playbook
Connect OpenAI to n8n the right way — chat, embeddings, structured outputs, function calling, and cost controls.
Key takeaways
- Use Structured Output mode with a JSON schema — never hand-parse text.
- Pin model versions in production (gpt-4o-2024-11-20) not aliases.
- Track tokens to Postgres for cost visibility.
- Use the cheap model for routing, the expensive one for the actual task.
The OpenAI node in n8n is more powerful than most users realize. Beyond chat completion, it covers embeddings, audio, image generation, structured outputs with JSON schema, and function calling. This playbook covers the patterns that matter in production.
Chat completion done right
Always set a system message that defines role and forbidden actions. Set temperature explicitly (0.2 for extraction, 0.7 for creative). Use Structured Output with a Zod-style JSON schema so the output is guaranteed-parseable.
Function calling vs Agents
Function calling is one-shot: the model picks a function and you execute it. Agents loop until done. For deterministic flows (classify then route) use function calling. For exploratory workflows (research, multi-step Q&A) use the Agent node.
Cost controls
Log usage.prompt_tokens + usage.completion_tokens to a Postgres table every call with workflow_id and model. A weekly digest workflow surfaces runaway prompts before the invoice does.
Fallback patterns
Wrap OpenAI calls in retry-with-backoff. On hard failure, fall back to Anthropic via a Switch node so your workflow degrades gracefully when OpenAI has an outage.
Frequently asked questions
- Should I use OpenAI directly or via the OpenRouter node?
- OpenAI direct for production-critical paths. OpenRouter when you want easy model swapping during prototyping.
- How do I prevent the model from hallucinating?
- Constrain with structured outputs, give it tools to look things up, and force citations in the system prompt.