đ€« Effortlessly Switch Between AI Models Like GPT-4, Claude & More â No Code Required
So, youâve got a fancy AI model (or ten), and youâre wondering how to make it work with n8n? Maybe you want your AI chatbot to switch between different models on the fly, or youâre just curious about how to build a supercharged AI agent. Either way, youâre in the right place!
Today, weâre going to build an LLM Router Agent in n8n that lets you use any model dynamically - GPT-4, Claude, Grok, or even a fine-tuned custom model. No coding required! Letâs get started. đ
â Step 1: Whatâs an LLM Router Agent (and Why Do You Need One)?
Before we start clicking around in n8n, letâs answer the big question: What is an LLM Router Agent?
Think of it like a smart AI traffic controller. Instead of sending every query to a single model, an LLM Router:
Chooses the best AI model based on the request (e.g., GPT-4 for creative tasks, Claude for reasoning, or Mistral for speed).
Saves money by using cheaper models for simple queries and premium ones for complex tasks.
Enhances flexibility, allowing you to integrate multiple AI providers effortlessly.
Imagine asking, âSummarize this articleâ, and the system decides to use GPT-4-Turbo because itâs great at summarization. Then, when you ask, âGive me a Python scriptâ, it switches to a model specialized in coding. Neat, right? đ
â Step 2: Setting Up n8n and Creating a Workflow
First, open up n8n and create a new workflow. This will serve as the brain of your AI agent.
Start a new workflow in n8n.
Add a Chat Trigger node â This captures the userâs message. When you are in n8n, you can hit chat, and then test it out just as if you were using ChatGPT, provided you have this connected to an AI Agent or any LLM.
This will serve as the entry point for user input.
â Step 3: Add an AI Agent (Chat Model) Node
Once youâve set up a Trigger, you have limitless possibilities for what comes next - you can integrate with other apps, process data, or even run custom code.
But, as I said at the beginning, today weâll integrate all AI models into one. This is where an LLM comes in.
Start by adding an AI Chat Model node and connect it to the Chat Trigger so it can process user input. There are three key connections to understand:
Chat model: Acts as the AI Agentâs brain, handling the conversation.
Memory: Enables the AI to retain context from past interactions, making responses more intelligent.
Tool (optional): Functions as add-ons, allowing the AI to access additional context or resources when needed.
Next, generate API credentials from your selected AI provider - here, weâll use OpenRouter Chat Model. It helps you to connect all the other models.
Double-click on the OpenRouter Chat Model node and link your OpenRouter account by adding an API key, which you can obtain from OpenRouterâs website.
Paste the API key into n8nâs credential configuration.
Now you can access almost any model on OpenRouter. You can view all the models on OpenRouterâs homepage.
In the model settings, you can choose which GPT module you want to use. Iâll choose openai/03-mini as an example, cause itâs cost-effective and fast.
Also, you can rename nodes by right-clicking on a node and renaming it.
Next is adding memory, as I said above âMemory helps enable the AI to retain context from past interactionsâ. Here, all you need is Simple Memory Node.
Learn How to Make AI Work For You!
Transform your AI skills with the AI Fire Academy Premium Plan â FREE for 14 days! Gain instant access to 500+ AI workflows, advanced tutorials, exclusive case studies, and unbeatable discounts. No risks, cancel anytime.
â Step 4: Connecting Multiple AI Models
Now comes the fun part - integrating different AI models! Hereâs how:
Add multiple AI Chat Model nodes â Each one will represent a different AI provider.
By adding multiple chat models, you can access their strengths, their weaknesses and help it decide on which model to use based on your query.
Ok, now open AI Agent Nodeâs settings. Then click on Add Option and choose System Message.
Then open Expression mode and enter your prompt.
Iâll give you my basic prompt template describing everything as I go. You could change the prompt based on how many models you add in. Also, in this article, Iâll use Perplexity (for a live web search), ChatGPT (highly advanced reasoning), and Claude (for coding) as an example.
(In the Model part to have a model ID, you need to open OpenRouter, search your model then copy the ID below the name and paste it into your prompt).
You are a routing agent.
Your job is to take in user queries and decide which model is best fit for each use case the user may have. You will have 3 models you can select form for the user query:
1.) "perplexity/sonar"
2.) "openai/o3-mini-high"
3.) "anthropic/claude-3.5-sonnet"
Each model has its strengths and can give better answers for specific user questions and commands. Let's dive into the strengths of each model so you can decide which is best for the user question:
Model:
perplexity/sonar
Strength:
â Built-in search.
â Features citations and the ability to customize sources.
â Can search the web for live data.
openai/o3-mini-high
Strength:
- 03-mini is a cost-efficient language model optimized for advanced reasoning tasks.
- Excels in science and mathematics.
- This model is best when careful, well thought responses are needed regarding problems with multiple variables or connections.
anthropic/claude-3.5-sonnet
Strengths:
- Coding: Scores ~49% on SWE-Bench Verified, higher than the last best score, and without any fancy prompt scaffolding
- Data science: Augments human data science expertise; navigates unstructured data while using multiple tools for insights
- Visual processing: Excels at interpreting charts, graphs, and images, accurately transcribing text to derive insights beyond just the text alone.
- Agentic tasks: exceptional tool use, making it great at agentic tasks (i.e., complex, multi-step problem solving tasks that require engaging with other systems)
You are to pass the user query and decide on a model using structured JSON object format like this:
JSON Object Structure:
{
"userQuery": "user query here",
"model": "selected model here"
}
Example 1:
{
"userQuery": "Search the web and find cool dog breeds.",
"model": "perplexity/sonar"
}
Example 2:
{
"userQuery": "Create me a business plan in order to create the next Google.",
"model": "openai/o3-mini-high"
}
Example 3:
{
"userQuery": "Give code to create the game Tetris with Python",
"model": "anthropic/claude-3.5-sonnet"
}Donât forget to turn on Require Specific Output Format.
After that, go back to your prompt then copy JSON Object Structure. Then click on output parser in AI Agent Nodeâs setting and select Structured Output Parser.
Ok, now weâll paste it into JSON Example.
The last thing in this step is to test your project.
*Warning: I highly recommend you add below 10 models. Because if you add more, itâll be hard for the AI Agent to completely decide which model is the best for you.
â Step 5: Setting Up the Routing Logic
After selecting the model we want, we need to process our queries, right?
First, we add a new AI Agent Node to pass the query and map the chat model.
Instead of using Connected Chat Trigger Node, switch it to Define below to control our source for the prompt.
In Text, switch it to Expression mode and put userQuery in it.
Now, we need to map another OpenRouter chat model, then we can just use the exact same OpenRouter account unless you want to change it for limit use purposes.
Change model based on the first AI Agent decided when we give a query. All we need to do is put model in the Input to OpenRouter Model Expression.
Next, we add Simple Memory Node. We switch to Define below, drag sessionId into Key chat.
Now, test it out.
Your AI agent is now intelligent enough to answer for each query! đ
đ Final Thoughts: You Just Built a Multi-Model AI Agent!
Congratulations! đ Youâve just built a dynamic AI router that intelligently picks the best LLM for any situation. Hereâs what your agent can now do:
â Switch between AI models on demand
â Optimize costs by using the right model for the job
â Handle a variety of tasks more effectively
â Work without a single line of code!
The best part? You can keep adding models as new AI systems come out. Now go forth and automate - your AI-powered future awaits! đ
If you are interested in other topics and how AI is transforming different aspects of our lives, or even in making money using AI with more detailed, step-by-step guidance, you can find our other articles here:
Make AI Work for You: Create Daily Motivational Quotes Effortlessly
The Power of AI Automation: It Can Find and Create Viral Reels for You in Minutes!
The Simple Guide to Setting Up Automated Calls for Customer Success
*indicates a premium content, if any