profile-pic
Bilal TufailWeb Designer

Available for Amazing Projects!

AI AgentsMay 12, 20265 min read

How to Build an AI Agent Workflow in n8n That Actually Remembers Context (2026)

How to Build an AI Agent Workflow in n8n That Actually Remembers Context (2026)

Most n8n tutorials show you how to send a message to GPT-4 and get a reply. That’s not an agent. That’s a chatbot with extra steps. Here’s how to build something that actually works.

Most automation workflows are dumb by design. They follow a path: if this happens, do that. No context. No memory. No ability to adapt.

That works fine for simple tasks like sending a Slack notification or updating a spreadsheet row. But if you want to build something that can handle multi-step decisions, remember what happened last time, and take action based on context — you need an AI agent, not a workflow.

In 2026, n8n has made this genuinely achievable without writing a full backend. The AI agent node, combined with memory and tool support, lets you build workflows that behave more like a teammate than a script.

This article walks you through building one from scratch.

What Makes an AI Agent Different From a Regular LLM Node

Before building, you need to understand the distinction.

A regular LLM node in n8n does one thing: takes an input, sends it to a model like GPT-4 or Claude, and returns the output. Stateless. No memory of what came before. Every call starts fresh.

An AI agent node is different. It has three things a plain LLM node doesn’t:

  1. Memory — it can store and recall past interactions
  2. Tools — it can call external systems (search, database, APIs) during reasoning
  3. Decision loop — it decides which tools to use and in what order, rather than following a fixed path

Think of it this way: the LLM node is a calculator. The agent node is a junior analyst who knows how to use Excel, browse the internet, and remember what you told them yesterday.

What We’re Building

A lead qualification agent that:

  • Receives a new lead from a form submission
  • Looks up the company using a search tool
  • Checks if the lead has contacted you before using memory
  • Scores the lead and writes a summary
  • Routes the lead to the right Slack channel based on score

This is a real use case. You can adapt it for customer support, onboarding, or internal ops.

Prerequisites

  • n8n instance (self-hosted or cloud, version 1.x or 2.x)
  • OpenAI API key (or Anthropic/Google — the setup is the same)
  • A form tool like Typeform or a webhook trigger
  • Basic familiarity with n8n’s canvas

Step 1: Set Up the Trigger

Start with a Webhook node as your trigger. This fires every time a new lead submits your form.

In your webhook node:

  • Set the HTTP method to POST
  • Copy the webhook URL and point your form to it

Your incoming payload should look something like this:

{
"name": "Sarah Chen",
"email": "sarah@growthco.io",
"company": "GrowthCo",
"message": "We're looking for help automating our onboarding process for 200+ clients."
}

You can test this by sending a manual POST request using a tool like Postman or Hoppscotch.

Step 2: Add the AI Agent Node

This is the core of the workflow.

Add an AI Agent node and connect it to your webhook trigger.

Inside the agent node, you’ll configure three things:

2a. The System Prompt

This is where you define the agent’s role and behavior. Be specific. Vague prompts produce vague results.

You are a lead qualification assistant for a B2B automation agency.

When you receive a new lead, you must:
1. Use the web search tool to find basic information about their company
2. Check memory to see if this person or company has reached out before
3. Score the lead from 1–10 based on company size, message intent, and fit
4. Write a short 3-sentence summary of the lead for the sales team
5. Return a JSON object with: name, company, score, summary, and is_returning_lead

Always be concise. Never make up information. If you can’t find data, say so.

2b. The Model

Select your preferred model. For this use case, GPT-4o or Claude 3.5 Sonnet works well. Both are available as LLM nodes you can attach to the agent.

Connect an OpenAI Chat Model node (or Anthropic equivalent) to the agent’s model input.

Set temperature to 0.3 — you want consistent, structured output, not creative variation.

2c. Memory

Connect a Window Buffer Memory node to the agent’s memory input.

Configure it with:

  • Session ID: use {{ $json.email }} — this ties memory to the individual lead’s email address
  • Context window: 10 messages is enough for this use case

This means if Sarah from GrowthCo fills out your form again in three months, the agent will remember the previous interaction and flag her as a returning lead.

Step 3: Attach Tools

Tools are what make the agent actually useful. Without them, it can only reason on the input data you give it. With tools, it can go get more information.

Tool 1: Web Search

Add a SerpAPI node (or any search tool node) and connect it to the agent’s tools input.

Configure it to accept a search query as input. The agent will use this to look up the company when it decides it needs more context.

You don’t need to tell the agent when to search. That’s the point. It decides.

Tool 2: Google Sheets Lookup

Add a Google Sheets node in read mode, connected as a second tool.

Point it to a sheet that logs past leads (name, email, company, date).

The agent can call this tool to check if the lead exists before flagging them as new or returning.

Here’s how to configure the Sheets node so the agent can use it properly:

  • Operation: Read rows
  • Filter by: Email column equals {{ $fromAI('email') }}

The $fromAI() syntax lets the agent pass values directly into the tool at runtime. This is a key feature in n8n’s 2026 tool integration — the agent decides what to query, not you.

Step 4: Parse the Agent’s Output

After the agent runs, it returns a string. You need to convert it into structured data.

Add a Code node after the agent node with this JavaScript:

const raw = $input.first().json.output;

// Strip markdown code fences if present
const cleaned = raw.replace(/```json|```/g, '').trim();

let parsed;
try {
  parsed = JSON.parse(cleaned);
} catch (e) {
  // If parsing fails, return raw output for debugging
  return [{ json: { error: 'Failed to parse agent output', raw } }];
}

return [{ json: parsed }];

This gives you a clean JSON object to work with in the next steps:

{
  "name": "Sarah Chen",
  "company": "GrowthCo",
  "score": 8,
  "summary": "Sarah leads operations at a mid-sized growth agency handling 200+ clients. Her message shows clear intent around onboarding automation. Company size and use case align well with our core offering.",
  "is_returning_lead": false
}

Step 5: Route Based on Score

Now add an IF node to split the workflow based on lead score.

  • Condition{{ $json.score }} is greater than or equal to 7
  • True branch: High-priority lead
  • False branch: Low-priority lead

High-Priority Path

Connect a Slack node set to post in #hot-leads:

New High-Priority Lead

Name: {{ $json.name }}
Company: 
{{ $json.company }}
Score: 
{{ $json.score }}/10
Returning: 
{{ $json.is_returning_lead ? ‘Yes’ : ‘No’ }}
Summary: 
{{ $json.summary }}

Low-Priority Path

Connect a Google Sheets node in append mode to log the lead to a nurture list for later follow-up.

Step 6: Add Error Handling

Agents can fail. The model might time out, the search tool might return nothing useful, or the JSON might not parse correctly.

Add an Error Trigger node at the workflow level.

Connect it to a Slack node that posts to #automation-errors:

Workflow failed: Lead Qualification Agent
Error: {{ $json.error.message }}
Lead email: {{ $json.email }}
Timestamp: {{ $now.toISO() }}

This is your human-in-the-loop fallback. When the agent fails, a human gets notified and can handle it manually.

Full Workflow Map

Here’s the complete node sequence:

Webhook Trigger
    |
    v
AI Agent Node
    |-- [Tool] SerpAPI Web Search
    |-- [Tool] Google Sheets Lookup
    |-- [Memory] Window Buffer Memory (keyed by email)
    |-- [Model] GPT-4o / Claude 3.5 Sonnet
    |
    v
Code Node (parse JSON output)
    |
    v
IF Node (score >= 7?)
    |
    |-- TRUE --> Slack (#hot-leads)
    |
    └-- FALSE --> Google Sheets (nurture list)

Error Trigger --> Slack (#automation-errors)

What the Memory Actually Does

This is worth pausing on because it’s not magic.

The Window Buffer Memory node stores the conversation history for a given session ID. In this case, the session ID is the lead’s email address.

When the agent runs for Sarah the first time, it stores the interaction:

Session: sarah@growthco.io
Turn 1 - User: [lead data]
Turn 1 - Agent: [output JSON]

When Sarah submits again three months later, the agent loads this history before reasoning. It sees the previous interaction and can factor that in.

This is why the session ID matters. If you used a random UUID each time, every run would start with no context. By tying it to the email, you get persistent per-contact memory across workflow runs.

Common Mistakes to Avoid

Using temperature 1.0 for structured output You want the agent to be consistent, not creative. Keep temperature at 0.2 to 0.4 when you expect JSON back.

Not validating the agent’s output The agent will sometimes return malformed JSON, especially if the model hits a tricky input. Always wrap your parse step in a try/catch.

Attaching too many tools More tools don’t mean better results. They mean more decision overhead for the model. Start with two tools. Add more only when you have a specific reason.

Forgetting the system prompt is your contract The system prompt is the only reliable way to constrain the agent’s behavior. If you want it to always return a score, say so explicitly. If you don’t want it to speculate, say that too.

Where to Take This Next

This workflow is a starting point. Once it’s running and stable, you can extend it in a few directions:

  • Add a vector store (Pinecone or Qdrant) to give the agent access to your product documentation, so it can match the lead’s use case to specific services you offer
  • Replace the Slack output with an email draft in Gmail, ready for a human to review and send
  • Add a second agent that handles the follow-up sequence after the first contact
  • Log all agent decisions to a database for observability and review

n8n’s 2026 observability features (OpenTelemetry support, execution logs) make this easier to monitor at scale than it was even a year ago.

Final Thought

The difference between a workflow that processes data and a workflow that acts on it is context. Memory is what gives an AI agent context over time.

Most people building with n8n stop at the LLM node. They get a single response and move on. But the agent node with memory is where the real leverage is — especially for anything that involves ongoing relationships with leads, customers, or teammates.

Build it once, tune the system prompt, and you have something that gets more useful the more it runs.

Say Hello

waving-hand-small

Let's Connect

Let's create something unique together! Here's how you can reach out to me!

© 2025 Bilal Tufail • All rights reserved

Developed with ❤️ by Bilal Tufail