Introduction
In the previous article, we learned about:
RunnableWithMessageHistory
We learned how to build stateful AI applications that can remember previous messages.
For example:
User
↓
Conversation History
↓
Prompt
↓
OpenAI
↓
AI Response
↓
Save History
Now imagine we want to make that application more flexible.
For example, instead of hardcoding:
const model = new ChatOpenAI({
model: "gpt-4.1-mini",
});
we might want the user or application to choose:
OpenAI
or
Gemini
at runtime.
Or we might want to change:
temperature
max tokens
model
provider
without rebuilding the Runnable.
Instead of creating separate workflows:
OpenAI Workflow
Gemini Workflow
we can create one configurable workflow:
┌──→ OpenAI
│
AI Workflow ────────┤
│
└──→ Gemini
This is where configurable Runnables become useful.
In this article, we'll learn:
-
What configurable Runnables are
-
Why runtime configuration is useful
-
RunnableConfig -
The
configurableobject -
configurableFields() -
configurableAlternatives() -
Switching between OpenAI and Gemini
-
Configuring model parameters
-
Runtime configuration
-
Combining configurable models with chains
-
Configurable alternatives
-
Configurable fields
-
Combining configuration with message history
-
Practical use cases
-
Common mistakes
-
Best practices
Let's get started.
What Are Configurable Runnables?
A configurable Runnable is a Runnable whose behavior can be changed at runtime using configuration values.
Without configuration:
Code
↓
Fixed Runnable
↓
Fixed Model
With configuration:
Code
↓
Runnable
↓
Runtime Configuration
↓
Selected Behavior
For example:
const chain = ...;
Instead of creating:
const openAIChain = ...;
const geminiChain = ...;
we can create one configurable chain:
chain
↓
provider = "openai"
↓
OpenAI
or:
chain
↓
provider = "gemini"
↓
Gemini
Why Do We Need Configurable Runnables?
Suppose we build an AI chatbot.
A simple implementation might look like:
const model =
new ChatOpenAI({
model: "gpt-4.1-mini",
});
This works.
But what if we want to support:
OpenAI
Gemini
We could manually write:
if (provider === "openai") {
// OpenAI
}
if (provider === "gemini") {
// Gemini
}
But this creates provider-specific application logic.
Configurable Runnables allow us to move this configuration into the Runnable architecture.
The Main Idea
Think about a configurable Runnable like this:
Runtime Configuration
↓
┌─────────────┐
│ Runnable │
└──────┬──────┘
↓
Selected Behavior
For example:
provider = "openai"
↓
OpenAI Model
or:
provider = "gemini"
↓
Gemini Model
The workflow itself doesn't need to change.
Only the configuration changes.
RunnableConfig
LangChain Runnables can receive a configuration object when they are invoked.
For example:
const result =
await chain.invoke(
input,
{
configurable: {
provider: "openai",
},
}
);
The important part is:
{
configurable: {
provider: "openai"
}
}
This tells the Runnable:
Use the runtime configuration named
providerwith the valueopenai.
Basic Runnable Configuration
Let's start with a simple example.
Import:
import {
RunnableLambda,
} from "@langchain/core/runnables";
Create a Runnable:
const runnable =
RunnableLambda.from(
(input) => {
return `Hello ${input}`;
}
);
Invoke:
const result =
await runnable.invoke("Sundar");
console.log(result);
Output:
Hello Sundar
This is a normal Runnable.
There is no configuration yet.
Understanding Configurable Values
A configurable Runnable allows us to define values that can be supplied at runtime.
Conceptually:
Application
↓
Runnable
↓
configurable:
{
key: value
}
For example:
{
configurable: {
provider: "gemini"
}
}
Configurable Fields
One important feature is:
configurableFields()
It allows certain Runnable parameters to be configured at runtime.
For example, we might have:
temperature
as a configurable field.
Instead of:
temperature: 0.2
being permanently fixed, we can make it configurable.
Configurable Alternatives
Another important feature is:
configurableAlternatives()
This is particularly useful when we want to choose between alternative Runnables.
For example:
┌──→ OpenAI
AI Workflow ─┤
└──→ Gemini
We can define the alternatives once and choose one at runtime.
Project Setup
Let's create a project.
mkdir configurable-runnables
cd configurable-runnables
npm init -y
Install the packages:
npm install @langchain/core @langchain/openai @langchain/google-genai dotenv
Project Structure
configurable-runnables/
│
├── index.js
├── .env
├── .gitignore
└── package.json
Environment Variables
Create .env:
OPENAI_API_KEY=your_openai_api_key
GOOGLE_API_KEY=your_google_api_key
Create .gitignore:
node_modules/
.env
Step 1 — Initialize OpenAI
import "dotenv/config";
import {
ChatOpenAI,
} from "@langchain/openai";
const openAIModel =
new ChatOpenAI({
model: "gpt-4.1-mini",
});
Step 2 — Initialize Gemini
import {
ChatGoogleGenerativeAI,
} from "@langchain/google-genai";
const geminiModel =
new ChatGoogleGenerativeAI({
model: "gemini-2.5-flash",
});
Now we have:
OpenAI Model
+
Gemini Model
Step 3 — Create Configurable Alternatives
Import:
import {
ConfigurableAlternatives,
} from "@langchain/core/runnables";
In current LangChain.js APIs, configurable alternatives are commonly created from a Runnable using configurableAlternatives(), allowing runtime selection among alternative Runnables.
The exact API surface can vary across LangChain.js releases, so use the version-compatible method exposed by your installed @langchain/core package.
A typical pattern is:
const model =
openAIModel.configurableAlternatives(
...
);
The important concept is:
default model
+
alternative models
+
runtime selector
A Practical Provider Router
The goal is to create:
provider = "openai"
↓
OpenAI
provider = "gemini"
↓
Gemini
A typical configurable-alternative setup looks conceptually like:
const model =
openAIModel.configurableAlternatives(
{
which: ...,
defaultKey: "openai",
alternatives: {
gemini: geminiModel,
},
}
);
The exact configuration object depends on the installed LangChain.js version.
The important pieces are:
defaultKey
alternatives
runtime configurable key
Understanding the Configuration
Suppose the selector is called:
provider
and the alternatives are:
openai
gemini
Then:
provider = "openai"
↓
OpenAI
provider = "gemini"
↓
Gemini
The application can switch providers without rebuilding the chain.
Runtime Provider Selection
Once the Runnable has been configured, the invocation can look like:
const result =
await model.invoke(
"Explain Node.js streams.",
{
configurable: {
provider: "gemini",
},
}
);
Now Gemini is selected.
Change:
provider: "openai"
and OpenAI is selected.
Why Is This Useful?
Imagine a frontend application with a dropdown:
AI Provider:
[ OpenAI ▼ ]
The user changes it to:
Gemini
The backend can simply pass:
{
configurable: {
provider: "gemini"
}
}
The workflow remains the same.
Configurable Model Architecture
The architecture becomes:
┌──→ OpenAI
│
User Request
↓ │
Configurable Runnable ───┤
↑ │
│ └──→ Gemini
│
provider
This is much cleaner than creating separate application workflows.
Configurable Fields
Now let's look at another concept.
Suppose we want to control the model's:
temperature
Instead of hardcoding:
temperature: 0.7
we can expose it as a configurable field.
Conceptually:
Application
↓
temperature = 0.2
↓
Model
or:
temperature = 0.8
↓
Model
Why Configure Temperature?
Different applications may require different behavior.
For example:
Technical explanation
→ lower temperature
Creative writing
→ higher temperature
We could allow:
temperature = 0.2
for technical answers.
And:
temperature = 0.8
for creative tasks.
Creating a Configurable Temperature
A typical pattern is:
const model =
new ChatOpenAI({
model: "gpt-4.1-mini",
temperature: 0.7,
}).configurableFields({
temperature: {
id: "temperature",
name: "Temperature",
description:
"Controls response creativity",
},
});
Now the temperature can be provided through runtime configuration.
For example:
const result =
await model.invoke(
"Write a short story.",
{
configurable: {
temperature: 0.9,
},
}
);
Understanding configurableFields()
The purpose is:
Fixed parameter
↓
Expose as configurable field
↓
Runtime value
For example:
.configurableFields({
temperature: {
id: "temperature",
},
});
Then:
{
configurable: {
temperature: 0.8
}
}
controls the value.
Configurable Field vs Normal Field
Without configuration:
const model =
new ChatOpenAI({
temperature: 0.5,
});
The value is fixed.
With configuration:
const model =
new ChatOpenAI({
temperature: 0.5,
}).configurableFields(...);
the value can be changed at invocation time.
Example: Technical vs Creative
Suppose we have:
Technical Mode
temperature = 0.2
Creative Mode
temperature = 0.9
The application can choose:
{
configurable: {
temperature: 0.2
}
}
or:
{
configurable: {
temperature: 0.9
}
}
without creating a completely new chain.
Configurable Alternatives vs Configurable Fields
This distinction is very important.
Configurable Alternatives
Used to choose between different Runnables.
Example:
OpenAI
Gemini
Think:
Which model?
Configurable Fields
Used to change a parameter of a Runnable.
Example:
temperature
max tokens
Think:
How should this model behave?
Simple Mental Model
Remember:
Configurable Alternatives
↓
Choose WHAT
Configurable Fields
↓
Configure HOW
For example:
Provider
↓
OpenAI / Gemini
is an alternative.
While:
Temperature
↓
0.2 / 0.7 / 0.9
is a field.
Combining Both
This is where configurable Runnables become powerful.
We can configure:
Provider
Temperature
at the same time.
For example:
{
configurable: {
provider: "gemini",
temperature: 0.7
}
}
The workflow becomes:
Provider
↓
Gemini
Temperature
↓
0.7
Configurable Runnables with PromptTemplate
Let's create a normal prompt.
import {
ChatPromptTemplate,
} from "@langchain/core/prompts";
const prompt =
ChatPromptTemplate.fromMessages([
[
"system",
"You are a helpful AI assistant.",
],
[
"human",
"{question}",
],
]);
Now:
Prompt
↓
Configurable Model
↓
Output
Create:
const chain =
prompt.pipe(model);
The chain inherits the model's configuration.
Runtime Model Selection Through a Chain
Now we can invoke:
const response =
await chain.invoke(
{
question:
"What is Node.js?",
},
{
configurable: {
provider: "gemini",
},
}
);
The workflow is:
Question
↓
Prompt
↓
Provider = Gemini
↓
Gemini
↓
Response
Change:
provider: "openai"
and the same chain uses OpenAI.
Configurable Runnables + Output Parser
Let's add:
import {
StringOutputParser,
} from "@langchain/core/output_parsers";
const parser =
new StringOutputParser();
Then:
const chain =
prompt
.pipe(model)
.pipe(parser);
Now:
const result =
await chain.invoke(
{
question:
"Explain JavaScript closures.",
},
{
configurable: {
provider: "openai",
},
}
);
console.log(result);
Configurable Runnables + Message History
We learned about:
RunnableWithMessageHistory
in the previous article.
We can combine it with configurable models.
The architecture becomes:
Session ID
↓
Message History
↓
Chat Prompt
↓
Configurable Model
↓
OpenAI / Gemini
↓
Response
↓
Save History
This allows a user to have a conversation while selecting the AI provider dynamically.
Example
Suppose the user chooses:
Provider:
Gemini
The first message:
"What is Node.js?"
uses Gemini.
Then the user switches:
Provider:
OpenAI
and asks:
"Where is it used?"
The same conversation history can be passed to the new provider.
Conceptually:
Conversation
↓
History
↓
OpenAI
or:
Conversation
↓
History
↓
Gemini
Configurable Runnables + RunnableWithFallbacks
We can also combine:
Configurable Model
+
Fallback
For example:
Provider = OpenAI
↓
OpenAI
↓
Failure
↓
Gemini
This is useful when the application allows the user to choose a preferred provider but still wants resilience.
Configurable Runnables + RunnableBranch
We can also combine runtime configuration with conditional routing.
For example:
mode = "technical"
↓
Technical Chain
or:
mode = "creative"
↓
Creative Chain
The configuration determines which workflow should be used.
A Real-World AI Assistant
Imagine a dashboard with these settings:
AI Provider:
[ OpenAI ▼ ]
Temperature:
[ 0.7 ]
Mode:
[ Technical ▼ ]
The frontend sends:
{
"provider": "openai",
"temperature": 0.3,
"mode": "technical"
}
The backend can pass:
{
configurable: {
provider: "openai",
temperature: 0.3
}
}
The same chain can then be reused for many users and configurations.
Why This Is Better Than Hardcoding
Without configurable Runnables, we might create:
const openAIChain = ...;
const geminiChain = ...;
if (provider === "openai") {
...
} else {
...
}
As the application grows, this becomes harder to maintain.
With configurable Runnables:
One Workflow
↓
Runtime Configuration
↓
Different Behavior
This improves:
-
Reusability
-
Maintainability
-
Flexibility
-
Separation of concerns
Runtime Configuration
One of the most important concepts is:
Configuration is supplied when the Runnable executes.
For example:
await chain.invoke(
input,
{
configurable: {
provider: "gemini",
},
}
);
The same chain can later be invoked with:
await chain.invoke(
input,
{
configurable: {
provider: "openai",
},
}
);
The chain itself doesn't need to be recreated.
Configuration Is Separate From Input
This is another important concept.
Suppose we have:
{
question:
"What is Node.js?"
}
This is the actual input.
While:
{
configurable: {
provider: "gemini"
}
}
is runtime configuration.
Think:
Input
↓
What should the AI process?
Configuration
↓
How should the Runnable process it?
Example
await chain.invoke(
{
question:
"Explain closures.",
},
{
configurable: {
provider: "gemini",
temperature: 0.4,
},
}
);
Here:
question
↓
Input
provider
↓
Configuration
temperature
↓
Configuration
Configurable Runnables in REST APIs
This becomes particularly useful in backend APIs.
Suppose the frontend sends:
{
"question": "Explain Node.js streams",
"provider": "gemini",
"temperature": 0.3
}
The backend can separate:
const {
question,
provider,
temperature,
} = req.body;
Then:
const result =
await chain.invoke(
{
question,
},
{
configurable: {
provider,
temperature,
},
}
);
This creates a clean separation between:
Request Data
and:
Runnable Configuration
Configurable Runnables in a React Application
Imagine a React UI:
AI Provider
○ OpenAI
○ Gemini
Temperature
0.2 ─────────●──── 1.0
The selected values can be sent to the backend:
const response =
await fetch("/api/chat", {
method: "POST",
body: JSON.stringify({
question,
provider,
temperature,
}),
});
The backend maps those values into Runnable configuration.
This is a practical application of configurable Runnables.
Configuration and Environment Variables
There is an important difference between:
Environment Configuration
and:
Runnable Configuration
Environment variables are usually used for:
API Keys
Database URLs
Application Secrets
For example:
OPENAI_API_KEY=...
Runtime Runnable configuration is used for:
Provider
Temperature
Model behavior
Runtime options
Don't confuse the two.
Configurable Runnables and Security
Be careful when exposing configuration to clients.
For example, don't blindly allow users to send arbitrary model parameters:
{
configurable: {
temperature: 999999
}
}
Validate configuration before passing it to the model.
For example:
const temperature =
Math.min(
Math.max(
requestedTemperature,
0
),
1
);
Or use a validation library such as Zod.
Configurable Runnables + Zod
Since we've already learned Zod in our earlier LangChain articles, we can validate incoming configuration.
For example:
import { z } from "zod";
const configSchema =
z.object({
provider:
z.enum([
"openai",
"gemini",
]),
temperature:
z.number()
.min(0)
.max(1),
});
Validate:
const config =
configSchema.parse({
provider,
temperature,
});
Then:
await chain.invoke(
{
question,
},
{
configurable: config,
}
);
This is a much safer architecture.
Configurable Alternatives with Multiple Models
You aren't limited to two alternatives.
You could have:
OpenAI
Gemini
Anthropic
Local Model
Conceptually:
┌──→ OpenAI
│
├──→ Gemini
│
Request → Model ├──→ Anthropic
│
└──→ Local
Then:
{
configurable: {
provider: "gemini"
}
}
selects Gemini.
Model Abstraction
This gives us a useful abstraction:
Application
↓
AI Chain
↓
Configurable Model
↓
Provider
The application doesn't necessarily need to know the implementation details of every provider.
It simply provides:
provider = "openai"
or:
provider = "gemini"
Configurable Model Selection vs RunnableBranch
You might be wondering:
Why not just use
RunnableBranch?
That's a good question.
Both can route behavior, but they solve slightly different problems.
RunnableBranch
Used for conditional workflow logic:
If condition A
↓
Chain A
Otherwise
↓
Chain B
Configurable Alternatives
Used when the Runnable itself exposes alternative implementations:
provider = openai
↓
OpenAI
provider = gemini
↓
Gemini
A configurable alternative is more like:
"This Runnable supports multiple interchangeable implementations."
While RunnableBranch is more like:
"Choose a workflow based on a condition."
When Should You Use Configurable Alternatives?
Good use cases:
-
Model provider selection
-
Model version selection
-
Embedding model selection
-
Vector store selection
-
Different LLM implementations
-
Different processing strategies
For example:
Embedding Provider
↓
OpenAI / Gemini / Local
When Should You Use Configurable Fields?
Good use cases:
-
Temperature
-
Model parameters
-
Runtime options
-
Search limits
-
Retrieval settings
-
Application-level tuning parameters
Common Beginner Mistakes
Mistake 1 — Confusing Input and Configuration
Don't mix:
{
question: "What is React?"
}
with:
{
configurable: {
provider: "gemini"
}
}
They serve different purposes.
Mistake 2 — Hardcoding Configuration
If a value needs to change per request, don't unnecessarily hardcode it into the Runnable.
Instead, consider exposing it as configuration.
Mistake 3 — Exposing Too Many Settings
Just because something can be configurable doesn't mean it should be.
Avoid exposing every model parameter to the frontend.
Only expose settings that actually make sense for your application.
Mistake 4 — Not Validating User Configuration
Never blindly trust:
req.body.temperature
or:
req.body.provider
Validate them.
For example:
z.enum([
"openai",
"gemini",
]);
Mistake 5 — Using Configurable Alternatives for Complex Business Logic
If the decision is:
If customer is premium
and order > $100
and region is US
then use chain A
that's probably better represented by business logic or RunnableBranch.
Configurable alternatives are better suited for interchangeable Runnable implementations.
Mistake 6 — Confusing Configurable Fields with Environment Variables
This:
OPENAI_API_KEY=...
is application configuration.
This:
{
configurable: {
temperature: 0.7
}
}
is Runnable runtime configuration.
Best Practices
1. Define clear configuration names
Prefer:
provider
temperature
over unclear names such as:
option1
value2
2. Validate configuration
Use Zod or another validation mechanism.
3. Keep configuration limited
Expose only what the application actually needs.
4. Keep provider abstraction clean
Your application code should not become tightly coupled to every model provider.
5. Use sensible defaults
For example:
Default provider → OpenAI
Default temperature → 0.3
If the caller doesn't provide configuration, the application still works.
A Production-Style Architecture
Let's combine what we've learned so far.
Imagine an AI customer support application.
The architecture could be:
HTTP Request
↓
Validate Input
↓
RunnableWithMessageHistory
↓
ChatPromptTemplate
↓
Configurable Model
↓
OpenAI / Gemini
↓
Fallback
↓
Output Parser
↓
Save Conversation
↓
HTTP Response
Configuration might look like:
{
configurable: {
provider: "openai",
temperature: 0.3,
sessionId: "chat-123"
}
}
Now we have:
-
Conversation memory
-
Provider selection
-
Runtime model configuration
-
Fallback support
-
Structured Runnable composition
This is much closer to a production architecture.
Complete Conceptual Example
Let's put everything together.
import "dotenv/config";
import {
ChatOpenAI,
} from "@langchain/openai";
import {
ChatGoogleGenerativeAI,
} from "@langchain/google-genai";
import {
ChatPromptTemplate,
} from "@langchain/core/prompts";
import {
StringOutputParser,
} from "@langchain/core/output_parsers";
const openAI =
new ChatOpenAI({
model: "gpt-4.1-mini",
});
const gemini =
new ChatGoogleGenerativeAI({
model: "gemini-2.5-flash",
});
const prompt =
ChatPromptTemplate.fromMessages([
[
"system",
"You are a helpful AI assistant.",
],
[
"human",
"{question}",
],
]);
const parser =
new StringOutputParser();
/*
Configure the model so that
OpenAI is the default and
Gemini can be selected
at runtime.
*/
const model =
openAI.configurableAlternatives(
// Configuration
);
const chain =
prompt
.pipe(model)
.pipe(parser);
const result =
await chain.invoke(
{
question:
"Explain Node.js streams.",
},
{
configurable: {
provider: "gemini",
},
}
);
console.log(result);
The exact configurableAlternatives() configuration depends on the installed LangChain.js version, but the architecture is the important part:
Prompt
↓
Configurable Model
↓
provider = gemini
↓
Gemini
↓
Parser
↓
Response
The Bigger Picture
We have now learned another important Runnable concept.
Our series has progressed from simple composition to runtime-configurable workflows.
The architecture now includes:
RunnableSequence
↓
RunnableParallel
↓
RunnableLambda
↓
RunnablePassthrough
↓
RunnableAssign
↓
RunnableBranch
↓
RunnableWithFallbacks
↓
RunnableWithMessageHistory
↓
Configurable Runnables
Each solves a different problem.
Final Thoughts
Configurable Runnables allow us to build flexible AI workflows without hardcoding every possible configuration.
The core idea is:
One Runnable
↓
Runtime Configuration
↓
Different Behavior
For example:
provider = "openai"
↓
OpenAI
or:
provider = "gemini"
↓
Gemini
Similarly:
temperature = 0.2
can produce more deterministic responses, while:
temperature = 0.8
can be used for more creative tasks.
The important distinction is:
Configurable Alternatives
↓
Choose between Runnables
while:
Configurable Fields
↓
Change Runnable parameters
And runtime configuration is passed separately from the normal input:
await chain.invoke(
input,
{
configurable: {
provider: "gemini",
temperature: 0.5,
},
}
);
When combined with the Runnable concepts we've already learned, this gives us a powerful architecture:
┌──→ OpenAI
│
Input
↓ │
Prompt → Configurable Model
↓ │
History └──→ Gemini
↓
Fallback
↓
Parser
↓
Response
This is a major step toward building flexible, reusable, and production-oriented LangChain.js applications.
Happy Coding! 🚀