Skip to content
Innovialabs

How to Build an AI Agent Workflow in n8n

Advanced4 min

In short

In n8n an agent is the AI Agent node with a model, memory and tools attached; give it a clear system prompt, structured output and limits, and keep actions with consequences in regular nodes.

What an n8n agent is made of

An AI agent in n8n is the AI Agent node with sub-nodes connected underneath:

  • Chat Model (required) — the LLM: OpenAI, Anthropic, Google Gemini, a local model via Ollama and others.
  • Memory — conversation history so the agent remembers earlier messages.
  • Tools — things the agent can call on its own: HTTP requests, Google Sheets, a CRM, another n8n workflow.
  • Output Parser — forces the answer into a defined structure (JSON).

The agent receives a message, decides whether it needs a tool, calls it, reads the result and writes a response. The number of such steps is capped by the Max Iterations option.

Step by step: a lead-qualification agent

The task: a website lead arrives in n8n, the agent evaluates it, looks up missing details and decides where it should go.

1. Trigger. For debugging, use the Chat Trigger to talk to the agent right in the editor. In production, use a Webhook from the site form or a Telegram Trigger.

2. AI Agent node. Pass the lead data into the prompt with an expression such as {{ $json.message }}. In System Message, describe the role and rules:

You qualify incoming leads.
Criteria: a budget or timeline is given, the task is clear, a contact is provided.
If data is missing, do not make it up; set status to "need_info".
Answer only in the format defined by the parser.

3. Model. Attach a Chat Model and create credentials with an API key. A fast, inexpensive model is usually enough for classification; keep the temperature low for stable answers.

4. Memory. A single lead needs no memory. For a Telegram conversation, attach Simple Memory with the chat ID as the session key, and in production use Postgres or Redis Chat Memory so history survives restarts.

5. Tools. For example:

  • HTTP Request Tool — look up a company or contact in the CRM by email;
  • Google Sheets Tool — read a price list or list of services;
  • Call n8n Workflow Tool — run a separate workflow with your own logic.

Tool parameters the model should fill in are defined with $fromAI():

{{ $fromAI('email', 'customer email from the lead', 'string') }}

Give every tool a clear name and description: the model uses them to decide when to call it.

6. Structured output. Enable Require Specific Output Format and attach a Structured Output Parser with a schema: status, score, reason, next_step.

7. Actions in regular nodes. After the agent, add a Switch on status: a hot lead is created in the CRM and sent to a manager in Telegram, an incomplete one gets a follow-up email. Write to the CRM with a regular node rather than an agent tool — the action stays deterministic and auditable.

Controlling cost

Agent cost depends on the model, prompt length, memory size and the number of tool calls. What helps:

  • Max Iterations stops the agent from looping.
  • A short memory window sends only the needed part of the history with each request.
  • A response token limit in the model settings.
  • Compact tool output — return the fields you need, not the entire API JSON.
  • A non-AI pre-filter — drop spam and empty leads with an IF node before the agent.
  • Token tracking — usage data appears in the model node's execution; log it to a table and watch the trend.

Errors and reliability

  • Enable Retry On Fail in node settings for temporary model API failures.
  • On Error → Continue (using error output) creates a separate branch for failures: the lead goes to a human instead of being lost.
  • Assign an Error Workflow with an Error Trigger node for alerts.
  • Validate the agent's output: even with a parser, add an IF check on required fields.
  • Least privilege for tools. Lead text can contain instructions aimed at the model, so agent tools should only read data where possible.

FAQ

Yes. The Ollama Chat Model node connects a model running on your own server. Keep in mind that small local models are weaker at calling tools, so test the workflow on real leads.

We can help with this

If you'd rather get the result than figure it all out yourself — this is what we do every day.

Discuss a project

Read next

Have an idea or a task? Tell us about it

We'll reply within one business day and propose a solution. The consultation is free.