What's the difference?
An AI agent is a loop-driven system that can decide which tool to call next, keep state across interactions, and adapt its behaviour.
An automation is a fixed sequence of steps that runs the same way every time.
In this guide you'll build both a plain n8n workflow that sends a prompt to OpenAI and stores the answer, and a full RAG-enabled AI agent that decides when to fetch documents, when to query the LLM, and when to respond.
By the end you'll see why most teams over-engineer, and you'll have a production-ready example you can ship tomorrow.
Key insight: If your use-case requires conditional tool use, memory, or dynamic goal-setting, you need an AI agent; otherwise a straight automation is cheaper, faster, and easier to maintain.
What you need Tool Plan / Price Role n8n (open-source workflow engine) Community edition (self-hosted, free) - see https://n8n.io/pricing for hosted options Orchestrates both automation and agent pipelines OpenAI API (ChatGPT/GPT-4) Pay-as-you-go - see https://openai.com/api/pricing Generates natural-language responses Pinecone (vector store) Free tier or paid plan - see https://www.pinecone.io/pricing Holds document embeddings for RAG Docker (container runtime) Free Runs n8n locally or in CI Git (version control) Free Stores workflow definitions Estimated build time: ~4 hours for a complete agent (including embedding documents) and ~1 hour for the plain automation.
Step-by-step build
1.
Set up n8n locally What this does: launches a self-hosted n8n instance with basic auth.
After a few seconds open http://localhost:5678 and log in with the credentials above.
2.
Create the plain automation workflow In the n8n UI, click New Workflow.
Add a Webhook node (trigger URL: ).
This receives a JSON payload .
Connect the Webhook to an OpenAI node (provided by n8n).
Model: (or whichever you have access to).
Prompt: .
Add a Set node to format the LLM output: .
End with a Respond node that returns .
Export the workflow JSON so you can version-control it: What this does: the JSON defines a linear pipeline - receive a prompt, send it to the LLM, wrap the response, and return it.
There is no conditional logic or memory; each request is isolated.
3.
Prepare document embeddings for RAG What this does: reads each file, generates an embedding with OpenAI's model, and stores the vector in Pinecone.
The script uses environment variables for API keys - store them securely (e.g., in a file).
4.
Build the AI agent workflow Create a new workflow called RAG Agent.
Add a Webhook node (trigger URL: ).
Input payload: .
Add a Function node named DecideAction.
Its JavaScript decides whether a document lookup is needed: Connect DecideAction to a Switch node that branches on .
Branch "retrieval": a.
Pinecone Search node (n8n has a community Pinecone node; if not, use an HTTP Request node).
Namespace: .
Query vector: compute on-the-fly using OpenAI's embedding endpoint ().
Top K: . b.
Merge node to concatenate retrieved fields. c.
Feed the concatenated context and original question to an OpenAI node (prompt: ) and return the answer.
Branch "direct": a.
Send the original question straight to an OpenAI node (same model, no context).
Close each branch with a Respond node that returns .
Export the workflow; the JSON will be larger because of the conditional logic, but the core principle is the same: the agent retains state () and decides which tool to call next.
5.
Test both endpoints What you should see: the automation returns a single sentence answer; the agent may include relevant excerpts from your indexed docs before the LLM's answer, demonstrating true tool use.
6.
Deploy (optional) If you prefer a managed n8n instance, sign up at https://n8n.io and import the JSON files via the UI.
For production you'll also want to: Enable HTTPS with a reverse proxy (e.g., Nginx).
Store API keys in environment variables (, ).
Set rate limits on the webhook nodes to protect against abuse.
You can now sell these automations as