n8n-nodes-advanced-ai-agent
v0.1.1
Published
Advanced AI Agent node for n8n with output validation (code checkers + AI validator), automatic retries with error feedback, required-tool-call enforcement, and structured JSON output via a formatter model
Maintainers
Readme
n8n-nodes-advanced-ai-agent
An Advanced AI Agent community node for n8n — a tools agent with a built-in quality-control loop:
- ✅ Code Validation — run your own JavaScript checks on the agent's answer and its tool calls. On failure the agent is automatically retried with your error message as feedback.
- 🤖 AI Validation — a second (or the same) chat model reviews the answer against a detailed, editable rubric. It sees the client message, the final answer, every tool call (name, arguments, result), and the agent's reasoning — so it can catch things like "you confirmed the order but never called the
create_ordertool". Rejections trigger a retry with the validator's feedback. - 📦 Structured Output — a formatter model converts the final validated answer into JSON matching an example you provide, with its own retry loop on parse/structure errors.
- 🎛️ All three features are independent toggles. Extra model inputs (Validator / Formatter) only appear on the canvas when enabled.
How the retry loop works
input ──► Agent run ──► code check ──► AI validator ──► pass ──► formatter ──► output
▲ │ │
└── feedback ◄─┴───── fail ────┘ (up to "Validation Retries" times)On every failed check the agent is re-invoked with the conversation so far plus the failed answer and the feedback ("Your previous answer was rejected... you must call the create_order tool..."). Failed attempts are never saved to connected Memory — only the original input and the final answer are.
Installation
Self-hosted n8n → Settings → Community Nodes → Install → n8n-nodes-advanced-ai-agent.
Or manually: npm install n8n-nodes-advanced-ai-agent in your n8n installation's ~/.n8n/nodes directory.
Connections
| Input | Type | When |
|---|---|---|
| Chat Model | ai_languageModel | always (required) |
| Memory | ai_memory | optional |
| Tool | ai_tool | optional, multiple |
| Validator Model | ai_languageModel | when AI Validation is on and Use Main Model as Validator is off |
| Formatter Model | ai_languageModel | when Structured Output is on and Use Main Model as Formatter is off |
Code Validation contract
Your JavaScript gets these variables:
| Variable | Type | Meaning |
|---|---|---|
| $input | string | the user message |
| $output | string | the agent's final answer |
| $toolCalls | array | [{ tool, args, result }, ...] — every tool the agent called |
| $reasoning | string | the agent's intermediate reasoning |
| $json | object | the incoming item's JSON |
Return true (or nothing) to pass. Return an error-message string (or throw) to fail — that message is sent to the agent as retry feedback, so make it actionable:
if (/order|buy/i.test($input) && !$toolCalls.some((c) => c.tool === 'create_order')) {
return 'The client asked to place an order but you never called the create_order tool. ' +
'Call create_order with the requested items, then confirm the order to the client.';
}
return true;Output
{
"output": "final answer text",
"validationPassed": true,
"attempts": 2,
"validationLog": [{ "attempt": 1, "source": "ai", "feedback": "..." }],
"structuredOutput": { "reply": "...", "orderCreated": true },
"toolCalls": [],
"reasoning": ""
}structuredOutput appears when the toggle is on; toolCalls/reasoning when Include Trace in Output is on. With On Validation Failure → Continue, exhausted retries emit the last answer with validationPassed: false instead of failing the node.
Notes
- Requires self-hosted n8n (community nodes with external dependencies are not available on n8n Cloud).
- The agent's "reasoning" is what LangChain captures between tool calls; providers' hidden native reasoning tokens are not always exposed by chat-model sub-nodes. Design validator rules to lean primarily on
toolCalls+output.
