AI Agents in n8n
The AI Agent node gives an LLM autonomous reasoning and tool access within a workflow.
System Message vs. User Message
| Field | Content | Changes per run? |
|---|---|---|
| System message | Role definition, behavioral rules, output format, constraints | No — static |
| User message | The actual input/query to process | Yes — 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 }}
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:
- List applicable rules for the current situation
- Check if all required information has been collected
- Verify planned actions comply with constraints
- 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 Case | Recommended Model | Why |
|---|---|---|
| Testing / prototyping | GPT-4o Mini, Gemini Flash | Fast, cheap |
| Content & code generation | Claude (Opus 4.6, Sonnet 4.6) | Best at structured output |
| Heavy research / analysis | O-series, Opus 4.6 | Extended reasoning |
| Large context (100K+) | Gemini 3 Pro (1M tokens) | Largest context window |
Agent Memory
| Strategy | How | Best For |
|---|---|---|
| Window memory | Keep last N messages | Simple chatbots |
| Vector memory | Embed conversations, retrieve relevant | Long-running agents |
| Database memory | Store structured data (Postgres) | Multi-session agents |
| External memory (Zep) | Dedicated memory service | Production 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
1. Prompt Chaining
Sequential LLM calls where each step feeds the next. Best for multi-step processing where each step is deterministic.
2. Routing
Agent or classifier decides which path to take. Best for inputs requiring different handling by type/complexity.
3. Parallelization
Run independent tasks simultaneously, merge results. Best for tasks with independent sub-components.
4. Evaluator-Optimizer
Agent evaluates its own output and iterates until quality threshold is met. Best for content generation.
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)