This guide shows you how to build a working agentic AI workflow using n8n (v1.50+) and OpenAI's GPT-4o model. By the end, you'll have an autonomous agent that can research a topic, make branching decisions, call external tools, and deliver a formatted output — all without manual intervention. Expect to spend about 90 minutes from setup to a live test run.
What You'll Build
- A self-directing n8n workflow powered by GPT-4o that loops, reasons, and takes action across multiple steps
- An agent that calls real tools — including a web search node and an HTTP request node — based on its own reasoning output
- A decision loop that retries intelligently when tool calls fail or return unexpected results
- A structured final output (JSON or Markdown) delivered to Slack, email, or a Google Sheet automatically
Prerequisites
- n8n v1.50 or later — self-hosted (Docker) or n8n Cloud. The AI Agent node requires at least v1.45.
- An OpenAI API key with GPT-4o access (tier 1 or above as of July 2026)
- Basic familiarity with n8n's canvas — you should know how to add nodes and connect them
- A free SerpAPI key (or Brave Search API key) for the web search tool
- Optional: a Slack webhook URL or Google Sheets OAuth credential for the output step
Step 1: Understand What Makes a Workflow "Agentic"
A standard automation runs a fixed sequence of steps. An agentic workflow is different — it lets an LLM decide which tool to call next, evaluate the result, and loop until it satisfies a goal.
n8n's AI Agent node (introduced in v1.40, significantly improved in v1.50) implements a ReAct-style loop: Reason → Act → Observe → Repeat. GPT-4o handles the reasoning layer. You supply the tools.
Why does the model choice matter here?
GPT-4o's function-calling accuracy is measurably higher than GPT-3.5 for multi-step tool use — OpenAI's own evals show a roughly 30–40% improvement on complex tool-chaining tasks. For agentic loops with 3+ tool calls, use GPT-4o or GPT-4o-mini (for cost). Do not use GPT-3.5-turbo for this pattern; it fails to close loops reliably.
Step 2: Set Up Your n8n Credentials
Open your n8n instance and navigate to Settings → Credentials. You need three credentials before building the workflow.
- Click Add Credential → search for OpenAI → paste your API key → Save.
- Click Add Credential → search for SerpAPI → paste your key → Save.
- If you want Slack output: Add Credential → Slack → follow the OAuth flow → Save.
Common pitfall: n8n stores credentials encrypted. If you're self-hosting, make sure your N8N_ENCRYPTION_KEY environment variable is set before saving credentials. Without it, credentials are stored in plaintext and may fail after a restart.
Step 3: Create the Workflow and Add a Trigger
Go to Workflows → New. Every agentic workflow needs a trigger. For this guide, use a Manual Trigger so you can test it on demand. In production, swap this for a Schedule Trigger or a Webhook Trigger (for Slack slash commands, form submissions, or API calls).
Add a Set node after the trigger. This passes your starting prompt to the agent. Configure it like this:
{
"topic": "AI automation trends for Australian SMBs in Q3 2026",
"output_format": "Markdown bullet list with 5 findings and source URLs"
}
Using a Set node to define inputs makes your workflow reusable. Change the topic without touching the agent logic.
Step 4: Add the AI Agent Node
Add an AI Agent node. This is the core of the workflow. Configure these fields:
- Chat Model: Select your OpenAI credential → choose
gpt-4o - System Prompt: Paste the prompt below
- Max Iterations: Set to
8(prevents infinite loops; adjust up for complex tasks)
System prompt:
You are a research analyst for a digital agency.
Your job is to research the provided topic thoroughly using the available tools.
Always call the search tool at least twice with different queries.
After gathering results, synthesise your findings into the requested output format.
Do not fabricate sources. If a search returns no useful results, try a different query.
What if the agent exceeds the max iterations limit?
n8n will throw an error and halt the workflow. Add an Error Trigger workflow that sends you a Slack alert with the failed execution ID. Eight iterations covers most research tasks. For document-processing agents, increase this to 12–15.
Step 5: Attach Tool Nodes to the Agent
This is what makes the agent actually useful. n8n's AI Agent node accepts Tool sub-nodes that GPT-4o can call by name.
Tool 1: Web Search (SerpAPI)
Add a SerpAPI node as a tool. Connect it to the AI Agent node via the Tools input port (the bottom port on the Agent node). Set:
- Tool Name:
web_search - Tool Description:
Search the web for current information on any topic. Input: a search query string. - Query:
{{ $fromAI('query') }}
The $fromAI() expression is n8n's native syntax (available since v1.45) that lets the LLM populate a field dynamically. This is how tool-calling works — GPT-4o generates a JSON argument, n8n maps it to the node input.
Tool 2: HTTP Request (for fetching page content)
Add an HTTP Request node as a second tool. Connect it to the same Tools port.
- Tool Name:
fetch_page - Tool Description:
Fetch the text content of a URL. Input: a valid URL string. - URL:
{{ $fromAI('url') }} - Method: GET
- Response Format: Text
Pro tip: Limit the HTTP response to the first 3,000 characters using an Edit Fields node after the HTTP node: {{ $json.data.slice(0, 3000) }}. GPT-4o's context window is large, but trimming responses keeps token costs down by 60–70% on long pages.
Step 6: Add Memory (Optional but Recommended)
By default, n8n's AI Agent node is stateless — it forgets context between workflow executions. For workflows that run repeatedly (like a daily market briefing), connect a Simple Memory node to the Agent's Memory input port.
For cross-session memory, use the Postgres Chat Memory node instead, which stores conversation history in a Postgres table. Set the Session ID to a stable identifier like the user's email or a campaign slug.
When should you skip the memory node?
Skip memory for one-shot tasks like report generation or data transformation. Memory adds latency and token cost. Only add it when the agent needs to reference past runs — for example, a weekly competitor tracking agent that compares this week's findings to last week's.
Step 7: Route the Agent Output
After the AI Agent node, add a Switch node to route output based on success or failure. Check for the presence of the agent's output field:
{
"rules": [
{ "condition": "{{ $json.output !== undefined }}", "route": "success" },
{ "condition": "true", "route": "fallback" }
]
}
On the success route, add your delivery node — Slack, Gmail, or Google Sheets. On the fallback route, log the raw response to a Notion or Airtable node for manual review.
Step 8: Format and Deliver the Output
Add a Slack node on the success route. Set the message body to:




