Skip to main content

Dynamic AI model router for query optimization with OpenRouter

Workflow preview

Workflow preview
100%
Dynamic AI model router for query optimization with OpenRouter preview
Open on n8n.io

Important notice

This workflow is provided as-is. Please review and test before using in production.

1. Workflow Overview

The Agent Decisioner is a dynamic, AI powered routing system that automatically selects the most appropriate large language model (LLM) to respond to a user's query based on the query’s content and...

Best for

  • Engineering automation workflows
  • AI Chatbot automation workflows
  • intermediate n8n builders looking for reusable templates

Tools used

@n8n/n8n-nodes-langchain.chattrigger, @n8n/n8n-nodes-langchain.lmchatopenrouter, @n8n/n8n-nodes-langchain.outputparserstructured, n8n-nodes-base.stickynote, @n8n/n8n-nodes-langchain.agent, @n8n/n8n-nodes-langchain.outputparserautofixing, @n8n/n8n-nodes-langchain.lmchatopenai

Source and attribution

This workflow is cataloged by N8N Workflows and links back to its original n8n.io source page by Davide.

Original n8n.io source

1.1 Workflow description

Title
Dynamic AI model router for query optimization with OpenRouter
Workflow name
Dynamic AI model router for query optimization with OpenRouter

The Agent Decisioner is a dynamic, AI-powered routing system that automatically selects the most appropriate large language model (LLM) to respond to a user's query based on the query’s content and purpose.

This workflow ensures dynamic, optimized AI responses by intelligently routing queries to the best-suited model.


Advantages

  • 🔁 Automatic Model Routing: Automatically selects the best model for the job, improving efficiency and relevance of responses.

  • 🎯 Optimized Use of Resources: Avoids overuse of expensive models like GPT-4 by routing simpler queries to lightweight models.

  • 📚 Model-Aware Reasoning: Uses detailed metadata about model capabilities (e.g., reasoning, coding, web search) for intelligent selection.

  • 📥 Modular and Extendable: Easy to integrate with other tools or expand by adding more models or custom decision logic.

  • 👨‍💻 Ideal for RAG and Multi-Agent Systems: Can serve as the brain behind more complex agent frameworks or Retrieval-Augmented Generation pipelines.


How It Works

  1. Chat Trigger: The workflow starts when a user sends a message, triggering the Routing Agent.
  2. Model Selection: The AI Agent analyzes the query and selects the best-suited model from the available options (e.g., Claude 3.7 Sonnet for coding, Perplexity/Sonar for web searches, GPT-4o Mini for reasoning).
  3. Structured Output: The agent returns a JSON response with the user’s prompt and the chosen model.
  4. Execution: The selected model processes the query and generates a response, ensuring optimal performance for the task.

Set Up Steps

  1. Configure Nodes:

    • Chat Trigger: Set up the webhook to receive user messages.
    • Routing Agent (AI Agent): Define the system message with model strengths and JSON output rules.
    • OpenRouter Chat Model: Connect to OpenRouter for model access.
    • Structured Output Parser: Ensure it validates the JSON response format (prompt + model).
    • Execution Agent (AI Agent1): Configure it to forward the prompt to the selected model.
  2. Connect Nodes:

    • Link the Chat Trigger to the Routing Agent.
    • Connect the OpenRouter Chat Model and Output Parser to the Routing Agent.
    • Route the parsed JSON to the Execution Agent, which uses the chosen model via OpenRouter Chat Model1.
  3. Credentials:

    • Ensure OpenRouter API credentials are correctly set for both chat model nodes.
  4. Test & Deploy:

    • Activate the workflow and test with sample queries to verify model selection logic.
    • Adjust the routing rules if needed for better accuracy.

Need help customizing?

Contact me for consulting and support or add me on Linkedin.

1.2 Logical Blocks

This catalog entry is organized from the workflow JSON. The node-level section below shows the executable blocks available for review before importing the template.

2. Block-by-Block Analysis

Block 1 - When chat message received

Type / Role
@n8n/n8n-nodes-langchain.chatTrigger - chatTrigger
Config choices
Version 1.1

Block 2 - OpenRouter Chat Model

Type / Role
@n8n/n8n-nodes-langchain.lmChatOpenRouter - lmChatOpenRouter
Config choices
Version 1

Block 3 - Structured Output Parser

Type / Role
@n8n/n8n-nodes-langchain.outputParserStructured - outputParserStructured
Config choices
Version 1.2

Block 4 - OpenRouter Chat Model1

Type / Role
@n8n/n8n-nodes-langchain.lmChatOpenRouter - lmChatOpenRouter
Config choices
Version 1

Block 5 - Sticky Note

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 6 - Routing Agent

Type / Role
@n8n/n8n-nodes-langchain.agent - agent
Config choices
Version 1.9

Block 7 - AI Agent

Type / Role
@n8n/n8n-nodes-langchain.agent - agent
Config choices
Version 1.9

Block 8 - Auto-fixing Output Parser

Type / Role
@n8n/n8n-nodes-langchain.outputParserAutofixing - outputParserAutofixing
Config choices
Version 1

Block 9 - OpenAI Chat Model

Type / Role
@n8n/n8n-nodes-langchain.lmChatOpenAi - lmChatOpenAi
Config choices
Version 1.2

3. Summary Table

Workflow Dynamic AI model router for query optimization with OpenRouter
Complexity intermediate
Nodes 9
Categories Engineering, AI Chatbot
Author Davide
Published 20 May 2025

4. Reproducing the Workflow from Scratch

  1. 1. Download the workflow JSON

    Use the JSON export at /data/workflows/4237/4237.json as the source template for this automation.

  2. 2. Import the template into n8n

    Open n8n, import the downloaded JSON, and review each node before activating the workflow.

  3. 3. Configure credentials and variables

    Replace placeholder credentials, API keys, webhook URLs, account IDs, and environment-specific values with your own settings.

  4. 4. Test with sample data

    Run the workflow manually or in a staging workspace, inspect node output, and confirm downstream systems receive the expected data.

  5. 5. Activate and monitor

    Enable the workflow only after testing, then monitor executions, errors, and rate limits during the first production runs.

5. General Notes & Resources

Review imported nodes carefully before activation. This catalog entry is intended to help you inspect the workflow structure, understand required services, and find related templates faster.

Node names, credentials, schedules, webhook paths, and external service limits may need adjustment for your workspace.

Frequently asked questions

What does Dynamic AI model router for query optimization with OpenRouter do?

The Agent Decisioner is a dynamic, AI powered routing system that automatically selects the most appropriate large language model (LLM) to respond to a user's query based on the query’s content and...

What do I need before importing this workflow?

Review the workflow JSON, configure any required credentials in n8n, and test the automation in a safe workspace before using it in production.

Can I customize this workflow?

Yes. Use the block-by-block analysis and the downloadable JSON to inspect each node, then adjust credentials, prompts, schedules, filters, or destinations for your Engineering, AI Chatbot use case.