Dynamic AI Model Router Using OpenRouter - n8n Workflow

Implement smart AI routing with this advanced n8n workflow. Automatically select the best LLM via OpenRouter based on query complexity and type for optimized results. Import this n8n template now.

Workflow Preview

Ready to automate?

Download this n8n workflow template and start using it instantly.

Who is this best for?

Users and organizations seeking to optimize AI costs by routing complex queries only to expensive models, while routing simpler queries to cost-effective ones.
Developers building robust n8n templates who require dynamic LLM selection capabilities.
AI engineers leveraging the diverse model ecosystem provided by OpenRouter.
Anyone looking to implement a sophisticated AI agent structure within an n8n workflow.

Overview

Achieving optimal results from Large Language Models often requires utilizing a model specifically tailored to the task—whether it's coding, complex reasoning, or web search. This sophisticated n8n workflow solves the routing problem by implementing an automated Routing Agent.

Instead of hardcoding a single LLM, this n8n automation analyzes the incoming user query (the intent and complexity) and dynamically selects the best-suited model available via OpenRouter (e.g., GPT-4o Mini for reasoning, Claude 3.7 Sonnet for coding, or Perplexity Sonar for web search).

This dynamic routing dramatically improves the quality of the response, reduces latency, and ensures cost efficiency across your n8n deployments. This powerful design serves as an excellent foundation for other complex n8n templates.

How it Works

This powerful n8n workflow utilizes several LangChain nodes to establish an intelligent decision loop:


  1. Incoming Chat Trigger: The process begins with the When chat message received n8n trigger, which activates the n8n workflow upon receiving a new chat message or query.

  2. Routing Agent Initialization: The query is passed to the Routing Agent n8n node. This agent, configured with an extensive system prompt detailing the strengths of eight different models (e.g., perplexity/sonar, openai/gpt-4o-mini), decides which LLM is most suitable for the task.

  3. LLM Decision Making: The OpenRouter Chat Model provides the reasoning engine for the Routing Agent to make its selection, formulating the decision as a strict JSON object containing the desired target model and the original prompt.

  4. Structured Output Parsing: The Structured Output Parser n8n node strictly enforces the JSON output schema, ensuring the next step receives reliable, structured data (prompt and model).

  5. Dynamic Execution: The structured output then routes to the final AI Agent. Critically, the OpenRouter Chat Model1 n8n node used for execution has its model parameter dynamically set using an n8n expression: ={{ $json.output.model }}. This completes the dynamic routing, and the appropriate LLM executes the prompt, delivering an optimized response.

Installation Guide

To deploy this advanced n8n workflow, follow these steps:


  1. Import the n8n template: Copy the provided JSON code and import it into your n8n instance via the Workflows section. Click 'New', then 'Import from JSON'.

  2. Activate Credentials: The workflow requires credentials for OpenRouter. Click on the credential fields in both OpenRouter Chat Model n8n nodes.

* Create a new OpenRouter credential, providing your API Key.

  1. Configure the Trigger: The When chat message received n8n trigger is a specialized node that requires specific integration (often via n8n's LangChain integration layer). Ensure this n8n trigger is correctly linked to your desired chat front-end or service.

  2. Save and Activate: After configuring the credentials and checking the Routing Agent parameters, save the n8n workflow and set it to 'Active' to start running the automation.

Node Details

This n8n workflow relies on specialized LangChain integration nodes:

When chat message received (n8n trigger):
Function: Starts the n8n workflow upon receiving input, providing the user query that needs routing.
Key Configuration: Acts as the essential entry point, ensuring immediate response capability.

OpenRouter Chat Model (n8n node):
Function: Provides the Large Language Model service (via OpenRouter) that the Routing Agent uses to analyze the query and make a decision.
Key Configuration: Requires an active OpenRouter credential setup.

Routing Agent (n8n node):
Function: Acts as the AI decision layer. Its extensive system message defines the purpose and capabilities of eight different LLMs, ensuring intelligent model selection based on query intent.
Key Configuration: Contains a detailed prompt defining model strengths (e.g., perplexity/sonar for web search, openai/gpt-4o-mini for reasoning) and enforcing a strict JSON output format.

Structured Output Parser (n8n node):
Function: Ensures the output from the Routing Agent is a validated JSON object with prompt and model fields, preventing errors in the subsequent dynamic model selection.
Key Configuration: Uses a manual schema definition to enforce structure.

OpenRouter Chat Model1 (n8n node):
Function: This is the final execution n8n node. It dynamically calls the specific LLM chosen by the Routing Agent to provide the final answer.
* Key Configuration: The model name is set via an n8n expression: ={{ $json.output.model }}, enabling dynamic routing across the OpenRouter ecosystem.

Related n8n Workflows

Free

Nodes: 5 Nodes
Updated: December 26 2025
View all
Created by

Full-stack Web Developer based in Italy specialising in Marketing & AI-powered automations. For business enquiries, send me an email at [email protected] or add me on Linkedin.com/in/davideboizza

Featured*