This page covers AI Agent nodes, tools, memory, RAG, and design patterns. See also: n8n Overview, Item Flow, Self-Hosting.

AI Agents in n8n

The AI Agent node gives an LLM autonomous reasoning and tool access within a workflow.

System Message vs. User Message

FieldContentChanges per run?
System messageRole definition, behavioral rules, output format, constraintsNo — static
User messageThe actual input/query to processYes — dynamic

System message example:

You are a customer support agent. You will receive an email that you need
to categorize and respond to. Rules:
- Always be professional and empathetic
- Escalate billing issues to the billing team
- Output: JSON with {category, priority, response}

User message: {{ $json.emailBody }}

Keep all static instructions in the system message. Put only the dynamic input in the user message. This lets you swap input sources (email, Telegram, voice, form) without touching the agent logic.

Tools

Tools are nodes that the AI agent can decide to call autonomously:

AI Agent
├── Tool: HTTP Request (search API)
├── Tool: Google Sheets (lookup customer)
├── Tool: Code (calculate discount)
└── Tool: Send Email (respond to customer)

The agent reasons about which tools to use and in what order. Use $fromAI("paramName", "description") in tool node fields to let the agent supply parameter values dynamically.

Think Tool

Gives the agent a reasoning scratchpad before acting. The agent pauses to:

  1. List applicable rules for the current situation
  2. Check if all required information has been collected
  3. Verify planned actions comply with constraints
  4. Iterate over tool results for correctness

Most effective with Claude models. Add it when the agent struggles with complex multi-step tasks.

Model Selection

Use CaseRecommended ModelWhy
Testing / prototypingGPT-4o Mini, Gemini FlashFast, cheap
Content & code generationClaude (Opus 4.6, Sonnet 4.6)Best at structured output
Heavy research / analysisO-series, Opus 4.6Extended reasoning
Large context (100K+)Gemini 3 Pro (1M tokens)Largest context window

Agent Memory

StrategyHowBest For
Window memoryKeep last N messagesSimple chatbots
Vector memoryEmbed conversations, retrieve relevantLong-running agents
Database memoryStore structured data (Postgres)Multi-session agents
External memory (Zep)Dedicated memory serviceProduction at scale

RAG — Retrieval-Augmented Generation

User query → Embed query → Vector search → Top-K results → LLM generates answer

Simple but limited. Fails on structured data (tables, spreadsheets) and when the answer spans multiple documents.

The agent reasons about the best retrieval strategy instead of blindly vector-searching:

User query → AI Agent decides:
  ├── Vector search (unstructured docs)
  ├── SQL query (structured/tabular data)
  ├── Full document read (long-form analysis)
  └── Combination of the above

The agent can try vector search first, realize results are insufficient, then fall back to SQL or full-document analysis.

A production pattern that keeps the knowledge base in sync:

File uploaded/updated  Detect change  Generate embeddings  Upsert to vector DB
File deleted  Detect deletion  Remove embeddings from vector DB

Components: source monitoring, embedding generation, metadata tracking, sync strategy.

Result: Agents always search current, accurate data.

4 Agentic Design Patterns

Prompt Chaining

Extract entities → Classify intent → Generate response → Format output

Routing

Input → Classifier → Route A: Simple response
                    → Route B: Complex analysis
                    → Route C: Human escalation

Parallelization

Input → Fork ─┬─ Task A (research)
               ├─ Task B (analysis)
               └─ Task C (draft)
         Merge → Final output

Evaluator-Optimizer

Generate → Evaluate → Pass? → Output
                    → Fail? → Regenerate (with feedback)

Tracking Agent Actions

Log every tool call, decision, and outcome to a database or Google Sheet:

  • What action was taken
  • Which tools were called
  • Token usage per call
  • Result quality (success/failure)