Skip to main content

Dynamically switch between LLMs for AI agents using LangChain code

Workflow preview

Workflow preview
100%
Dynamically switch between LLMs for AI agents using LangChain code preview
Open on n8n.io

Important notice

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

1. Workflow Overview

Dynamically switch between LLMs for AI Agents using LangChain Code Purpose This example workflow demonstrates a way to connect multiple LLMs to a single AI Agent/LangChain Node and programmatically...

Best for

  • Support Chatbot automation workflows
  • AI Chatbot automation workflows
  • advanced n8n builders looking for reusable templates

Tools used

@n8n/n8n-nodes-langchain.chattrigger, @n8n/n8n-nodes-langchain.code, n8n-nodes-base.set, n8n-nodes-base.noop, n8n-nodes-base.if, @n8n/n8n-nodes-langchain.lmchatopenai, @n8n/n8n-nodes-langchain.sentimentanalysis, @n8n/n8n-nodes-langchain.chainllm

Source and attribution

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

Original n8n.io source

1.1 Workflow description

Title
Dynamically switch between LLMs for AI agents using LangChain code
Workflow name
Dynamically switch between LLMs for AI agents using LangChain code

Dynamically switch between LLMs for AI Agents using LangChain Code

Purpose

This example workflow demonstrates a way to connect multiple LLMs to a single AI Agent/LangChain Node and programmatically use one – or in this case loop through them.

What it does

This AI workflow takes in customer complaints and generates a response that is being validated before returned. If the answer was not satisfactory, the response will be generated again with a more capable model.

How it works

  • A LangChain Code Node allows multiple LLMs to be connected to a single Basic LLM Chain. On every call only one LLM is actually being connected to the Basic LLM Chain, which is determined by the index defined in a previous Node.
  • The AI output is later validated by a Sentiment Analysis Node
  • If the result was not satisfactory, it loops back to the beginning and executes the same query with the next available LLM
  • The loop ends either when the result passed the requirements or when all LLMs have been used before.

Setup

  • Clone the workflow and select the belonging credentials. You'll need an OpenAI Account, alternatively you can swap the LLM nodes with ones from a different provider like Anthropic after the import.

How to use

Beware that the order of the used LLMs is determined by the order they have been added to the workflow, not by the position on the canvas.

After cloning this workflow into your environment, open the chat and send this example message:

> I really love waiting two weeks just to get a keyboard that doesn’t even work. Great job. Any chance I could actually use the thing I paid for sometime this month?

Most likely you will see that the first validation fails, causing it to loop back to the generation node and try again with the next available LLM.

Since AI responses are unpredictable, the results and number of tries will differ for each run.

Disclaimer

Please note, that this workflow can only run on self-hosted n8n instances, since it requires the LangChain Code Node.

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 - Switch Model

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

Block 3 - Set LLM index

Type / Role
n8n-nodes-base.set - set
Config choices
Version 3.4

Block 4 - Increase LLM index

Type / Role
n8n-nodes-base.set - set
Config choices
Version 3.4

Block 5 - No Operation, do nothing

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

Block 6 - Check for expected error

Type / Role
n8n-nodes-base.if - if
Config choices
Version 2.2

Block 7 - Loop finished without results

Type / Role
n8n-nodes-base.set - set
Config choices
Version 3.4

Block 8 - Unexpected error

Type / Role
n8n-nodes-base.set - set
Config choices
Version 3.4

Block 9 - Return result

Type / Role
n8n-nodes-base.set - set
Config choices
Version 3.4

Block 10 - OpenAI 4o-mini

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

Block 11 - OpenAI 4o

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

Block 12 - OpenAI o1

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

Block 13 - OpenAI Chat Model

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

Block 14 - Validate response

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

Block 15 - Generate response

Type / Role
@n8n/n8n-nodes-langchain.chainLlm - chainLlm
Config choices
Version 1.6

Block 16 - Sticky Note

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

Block 17 - Sticky Note1

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

Block 18 - Sticky Note2

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

Block 19 - Sticky Note3

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

Block 20 - Sticky Note4

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

Block 21 - Sticky Note5

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

Block 22 - Sticky Note6

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

3. Summary Table

Workflow Dynamically switch between LLMs for AI agents using LangChain code
Complexity advanced
Nodes 22
Categories Support Chatbot, AI Chatbot
Author Mario
Published 01 May 2025

4. Reproducing the Workflow from Scratch

  1. 1. Download the workflow JSON

    Use the JSON export at /data/workflows/3820/3820.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 Dynamically switch between LLMs for AI agents using LangChain code do?

Dynamically switch between LLMs for AI Agents using LangChain Code Purpose This example workflow demonstrates a way to connect multiple LLMs to a single AI Agent/LangChain Node and programmatically...

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 Support Chatbot, AI Chatbot use case.