How to Build AI Intent Routing in n8n
February 2, 2025 ยท 9 min read
The #1 question on the n8n community forum about AI agents: "How do I get my AI agent to follow system prompts and route to the right action?" (106 views and climbing).
The answer isn't better prompting. It's intent routing โ classifying what the user wants first, then sending them down the right path. Here's how to build it in n8n.
๐ฅ Free Template
Download the complete workflow: ai-intent-router.json โ import directly into n8n.
Why Intent Routing Beats "One Big Prompt"
Most people try to build AI chatbots by cramming everything into one system prompt: "You are a helpful assistant that handles support, sales, feedback, and general questions."
This fails because:
- The AI gets confused โ with too many responsibilities, it picks the wrong action
- You can't customize responses โ support needs a ticket, sales needs a CRM update, feedback needs a database entry
- It's expensive โ you send the entire context to the AI every time, even for simple routing
- Debugging is impossible โ when something goes wrong, you don't know which "mode" the AI was in
Intent routing fixes all of this. One small, fast AI call classifies the intent. Then deterministic n8n logic routes to specialized handlers. Each handler has its own focused prompt and actions.
The Architecture
Webhook โ AI Classifier โ Switch Node โ Handlers
โโ Support Handler
โโ Sales Handler
โโ Feedback Handler
โโ General Handler
Five nodes. That's it. The key insight: the classifier is tiny. It only needs to output one word (SUPPORT, SALES, FEEDBACK, or GENERAL). This means:
- You can use a cheap, fast model (Claude Haiku or GPT-4o-mini)
- Set
max_tokens: 10โ classification takes milliseconds - The expensive model only runs in the handler that needs it
Step 1: The Classifier Prompt
This is the most important part. Your classifier prompt must be:
- Exhaustive โ every possible intent is listed
- Mutually exclusive โ no overlap between categories
- Constrained โ output is ONLY the label, nothing else
// System prompt for the classifier
You are an intent classifier. Given a user message,
classify it into EXACTLY ONE of these intents:
1. SUPPORT - Technical help, bug reports, how-to
2. SALES - Pricing, features, purchase intent
3. FEEDBACK - Suggestions, complaints, praise
4. GENERAL - Greetings, off-topic, unclear
Respond with ONLY the intent label. Nothing else.
The constraint "respond with ONLY the label" is critical. Without it, the AI will add explanations, which breaks your Switch node.
Step 2: The Code Node (Safety Net)
AI models don't always follow instructions perfectly. Add a Code node after the classifier to validate and normalize the output:
// Extract and validate intent
const content = response.choices[0].message.content;
const intent = content.trim().toUpperCase();
const valid = ['SUPPORT', 'SALES', 'FEEDBACK', 'GENERAL'];
const classified = valid.includes(intent) ? intent : 'GENERAL';
If the AI returns anything unexpected, it falls through to GENERAL. Fail safe, not fail open.
Step 3: The Switch Node
n8n's Switch node is perfect for intent routing. Set up one output per intent, with a fallback for anything unmatched:
- Output 0:
intent === "SUPPORT"โ Support Handler - Output 1:
intent === "SALES"โ Sales Handler - Output 2:
intent === "FEEDBACK"โ Feedback Handler - Fallback: โ General Handler
Step 4: Specialized Handlers
This is where intent routing really shines. Each handler can:
- Support: Create a Jira/Linear ticket, search knowledge base, send to Slack #support
- Sales: Update CRM, send to sales team, trigger nurture sequence
- Feedback: Save to database, tag sentiment, notify product team
- General: Use a conversational AI with a focused "helpful assistant" prompt
Each handler gets its own system prompt, its own tools, its own logic. No confusion. No prompt bloat. Clean separation of concerns.
Advanced: Adding Confidence Scores
Want to handle ambiguous messages? Modify the classifier to return a confidence level:
// Modified classifier prompt
Respond with the intent label and confidence (HIGH/LOW):
Format: INTENT|CONFIDENCE
Example: SUPPORT|HIGH
Then in your Code node, route LOW confidence messages to a human review queue instead of auto-handling them.
The JSON Body Gotcha
If you're calling the AI API via HTTP Request node, remember: set Specify Body to "String", not "JSON". n8n validates JSON before evaluating expressions, so {{ $json.message }} fails as invalid JSON. This trips up almost everyone.
When to Use This Pattern
- โ Customer support chatbots with multiple categories
- โ Slack/Discord bots that handle different commands
- โ Email triage (route to right department)
- โ Any system where user input could mean multiple different actions
- โ Single-purpose tools (just use one prompt)
- โ Free-form conversation (use a proper AI agent instead)
Get the Template
Download the complete AI Intent Router workflow and import it directly into n8n. Customize the intents, handlers, and AI model to fit your use case.
Download Template โBrowse All Templates