Dynamic LLM Routing via OpenRouter Chat Agent - n8n Workflow

Build flexible AI agents using this dynamic automation. Leverage OpenRouter with the OpenAI node, memory, and chat capabilities in your custom n8n workflow.

Workflow Preview

Ready to automate?

Download this n8n workflow template and start using it instantly.

Who is this best for?

n8n automation developers looking for highly customizable LLM solutions.
Users who need access to multiple AI models (DeepSeek, Mistral, etc.) without changing their core n8n workflow structure.
Technical teams seeking cost-effective and flexible LLM routing through OpenRouter.
Anyone creating interactive AI agents using an n8n trigger for conversational input.

Overview

This powerful automation solves the common challenge of vendor lock-in by routing all Large Language Model (LLM) requests through OpenRouter. OpenRouter acts as a unified API layer, providing access to dozens of models while maintaining compatibility with the standard OpenAI LangChain integration within n8n. This specific n8n workflow utilizes the When chat message received n8n trigger to start a conversational flow, ideal for building dynamic chat agents. By defining the model name dynamically in the Settings n8n node, users can switch between models like DeepSeek, Gemini, or proprietary models instantly. This reusable n8n templates structure is essential for building flexible and future-proof AI applications using the n8n platform, providing unmatched flexibility within an n8n workflow.

How it Works

The n8n workflow begins with the When chat message received n8n trigger, which activates upon receiving a new chat input, supplying the user's prompt and a unique session ID.


  1. Configuration: The workflow immediately flows to the Settings n8n node (a Set node), where crucial variables are defined: the specific model to use (e.g., deepseek/deepseek-r1-distill-llama-8b), the prompt extracted from the n8n trigger, and the sessionId for context.

  2. LLM Setup: The workflow connects the core AI components: the LLM Model (configured for OpenRouter access) and the Chat Memory n8n node (configured to use the dynamic sessionId for conversation history).

  3. Agent Execution: These components feed into the AI Agent n8n node. The agent receives the input prompt defined in the Settings n8n node and uses the selected LLM via OpenRouter, maintaining conversation history through the memory buffer. This allows the AI agent n8n node to process complex, multi-turn conversations.

  4. Result: The AI Agent processes the request using the specified third-party model, generating a response that completes this robust n8n workflow, leveraging the power of OpenRouter.

Installation Guide

To use this advanced n8n workflow, follow these steps:


  1. Import the n8n template: Download the provided JSON data and import it directly into your n8n instance via the Workflows section.

  2. Set up OpenRouter Credentials:

In n8n, navigate to Credentials.
Create a new credential using the OpenAI API type.
Name the credential something descriptive, like "OpenRouter."
Crucially, set the Base URL to https://openrouter.ai/api/v1.
* Enter your OpenRouter API key in the API Key field.

  1. Configure the LLM Model n8n node: The LLM Model n8n node is pre-configured to use the "OpenRouter" credential. Ensure the connection is established correctly.

  2. Customize Settings: Open the Settings n8n node and adjust the model assignment value if you wish to use a different LLM supported by OpenRouter (see the accompanying sticky note for examples). This change only requires modifying one n8n node.

  3. Activate the n8n trigger: The When chat message received n8n trigger will require platform-specific configuration depending on where the chat input originates.

  4. Save and activate the n8n workflow.

Node Details

When chat message received (n8n trigger):
Function: Serves as the starting point (n8n trigger) for the conversational process, providing the user input (chatInput) and necessary context (sessionId). This initiates the entire n8n workflow.
Key Configuration: Essential for initiating the workflow structure, supplying data to the subsequent n8n node.
Settings (n8n node - Set):
Function: Dynamically defines the parameters for the AI run, including the specific model (e.g., DeepSeek) accessed through OpenRouter, the user prompt, and the unique sessionId needed for the memory n8n node.
Key Configuration: Assignments include model: deepseek/deepseek-r1-distill-llama-8b, prompt: {{ $json.chatInput }}, and sessionId: {{ $json.sessionId }}.
LLM Model (n8n node - LangChain Chat OpenAI):
Function: Configures the actual connection to the Large Language Model. Although labeled OpenAI, it uses OpenRouter credentials and base URL for vendor flexibility, making this n8n node crucial for integration.
Key Configuration: Uses the dynamic expression ={{ $json.model }} to select the provider model. Credentials are set to "OpenRouter."
Chat Memory (n8n node - LangChain Memory Buffer Window):
Function: Stores the history of the conversation, allowing the AI agent to reference previous messages in the current session ID, ensuring continuous context within the n8n workflow.
Key Configuration: Session Key is dynamically set to ={{ $json.sessionId }}.
AI Agent (n8n node - LangChain Agent):
Function: The central processing logic of the n8n workflow. It orchestrates the memory and the LLM model to generate intelligent responses based on the input prompt defined in the Settings n8n node.
* Key Configuration: Input text is dynamically set using ={{ $json.prompt }}.

Related n8n Workflows

Free

Nodes: 6 Nodes
Updated: December 26 2025
View all
Created by
Daniel Nolde
Daniel Nolde

Featured*