Skip to content
FlowHubFluxonLab
B
AI Automationfree

Dynamically switch between LLMs for AI Agents using LangChain Code

by Marioadapted from n8n official workflow galleryUpdated Aug 2026
RequiresBBasic LLM ChainLangChain CodeLangChain CodeOpenAI Chat ModelOpenAI Chat ModelSentiment AnalysisSentiment Analysis
Share Post Share
ChWhen chat message receivedWhen chat messa…CoSwitch ModelSeSet LLM indexSeIncrease LLM indexIncrease LLM in…NONo Operation, do nothingNo Operation, d…IfCheck for expected errorCheck for expec…SeLoop finished without resultsLoop finished w…SeUnexpected errorSeReturn resultOpenAI 4o-miniOpenAI 4oOpenAI o1OpenAI Chat ModelOpenAI Chat Mod…SAValidate responseValidate respon…CLGenerate responseGenerate respon…123456789101112131415
1/5
STEPS · 15
Starts from a chat message

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.

Tags

n8nreference-onlychain-llmlm-chat-open-aisentiment-analysis
Connects
BBasic LLM ChainlangchaincodeLangChain CodeopenaichatmodelOpenAI Chat ModelsentimentanalysisSentiment Analysis
CategoryAI Automation
Triggermanual
Complexitycomplex
Nodes15
AddedMay 1, 2025

Related workflows

See all AI Automation
autofixingoutputparserBopenaichatmodelstructuredoutputparser
free

Force AI to use a specific output format

This workflow is for anyone looking to automatically fetch, validate, and parse complex language-based queries into a structured format. Its unique capability lies in not only processing language but also fixing invalid outputs before structuring them. Note that to use this template, you need to be on n8n version 1.19.4 or later.

by n8n Team
Acodetoollangchaincodeopenaichatmodel
free

Custom LangChain agent written in JavaScript

This workflow has multiple functionalities. It starts with a manual trigger, "When clicking 'Execute Workflow'", that activates two separate paths. The first path takes a preset string "Tell me a joke" and processes it through a custom Language Learning Model (LLM) chain node. This node interacts with an OpenAI node for query processing. The second path takes another preset string "What year was Einstein born?" and passes it to an "Agent" node. This agent further interacts with a Chat OpenAI node and a custom Wikipedia node to produce the required information. The workflow uses both built-in and custom nodes, and integrates with OpenAI for both paths. It's built for experimenting with language models, specifically in the context of conversational agents and information retrieval. Note that to use this template, you need to be on n8n version 1.19.4 or later.

by n8n Team
DhtmlWopenaichatmodel
free

Scrape and summarize webpages with AI

This workflow integrates both web scraping and NLP functionalities. It uses HTML parsing to extract links, HTTP requests to fetch essay content, and AI-based summarization using GPT-4o. It's an excellent example of an end-to-end automated task that is not only efficient but also provides real value by summarizing valuable content. Note that to use this template, you need to be on n8n version 1.50.0 or later.

by n8n Team