Welcome Back!
In Part 1, we learned:
- What a Runnable is
- Why LangChain introduced the Runnable Interface
- The Runnable contract
- Runnable architecture
- Why PromptTemplate is a Runnable
- Why ChatPromptTemplate is a Runnable
- Why Chat Models are Runnables
- Why Output Parsers are Runnables
- Why
pipe()works
Now it's time to understand how Runnables execute.
Every Runnable supports a common set of execution methods.
This consistency is what makes LangChain easy to learn and extend.
Let's continue.
Runnable Execution Lifecycle
Every Runnable follows the same lifecycle.
Input
↓
Receive Input
↓
Process
↓
Generate Output
↓
Return Output
Whether it's a prompt, a model, or a parser, the execution flow remains the same.
The invoke() Method
The most common execution method is invoke().
Conceptually:
Input
↓
Runnable
↓
Output
You've already used it with:
- Chat Models
- PromptTemplate chains
- Output Parsers
The important takeaway isn't the syntax—it's that every Runnable exposes the same execution method.
Example:
const chain = prompt
.pipe(openAIModel)
.pipe(parser);
const result = await chain.invoke({
topic: "Node.js",
});
console.log(result);
Notice that you're invoking the entire chain, not each component individually.
Why Doesn't Each Component Need Its Own Method?
Imagine if every component had a different API.
PromptTemplate
↓
createPrompt()
↓
Model
↓
generate()
↓
Parser
↓
parse()
Every time you added a new component, you would need to learn another API.
Instead, LangChain standardizes execution.
Prompt
↓
invoke()
↓
Model
↓
invoke()
↓
Parser
↓
invoke()
This is the power of the Runnable Interface.
The batch() Method
Sometimes your application needs to process multiple independent inputs.
Instead of calling invoke() repeatedly:
await chain.invoke({
topic: "React",
});
await chain.invoke({
topic: "Node.js",
});
await chain.invoke({
topic: "Express",
});
You can process them together.
const results = await chain.batch([
{ topic: "React" },
{ topic: "Node.js" },
{ topic: "Express" },
]);
console.log(results);
Conceptually:
Input 1
Input 2
Input 3
↓
Runnable
↓
Output 1
Output 2
Output 3
Every Runnable supports batch execution.
When Should You Use batch()?
Use it when the requests are independent.
Examples:
- Generate blog titles for multiple topics.
- Summarize several articles.
- Translate multiple sentences.
- Generate interview questions for different technologies.
Avoid using batch() when one request depends on the result of another.
The stream() Method
Normally, invoke() waits until the model finishes generating the complete response.
With stream(), the response is delivered gradually.
Thinking...
↓
N
↓
No
↓
Node
↓
Node.js
↓
Complete Response
Example:
const stream = await openAIModel.stream(
"Explain Node.js."
);
for await (const chunk of stream) {
process.stdout.write(chunk.content);
}
The same approach works with Gemini.
Streaming improves the user experience in chat applications because users don't have to wait for the entire response before seeing output.
Runnable Composition
One of the biggest strengths of LangChain is composition.
Instead of creating one large function that does everything, you connect smaller Runnables.
Example:
Prompt
↓
Model
↓
Parser
Each component has one responsibility.
- Prompt creates instructions.
- Model generates content.
- Parser transforms the response.
This separation makes the workflow easier to understand and maintain.
Building a Complete AI Chain
Let's build a simple chain.
import "dotenv/config";
import { PromptTemplate } from "@langchain/core/prompts";
import { StringOutputParser } from "@langchain/core/output_parsers";
import { ChatOpenAI } from "@langchain/openai";
const prompt = PromptTemplate.fromTemplate(
"Explain {topic} in simple words."
);
const model = new ChatOpenAI({
model: "gpt-4.1-mini",
});
const parser = new StringOutputParser();
const chain = prompt
.pipe(model)
.pipe(parser);
const result = await chain.invoke({
topic: "Node.js",
});
console.log(result);
Execution flow:
Input
↓
PromptTemplate
↓
Formatted Prompt
↓
Chat Model
↓
AI Message
↓
StringOutputParser
↓
Plain Text
Each component processes the output of the previous one.
Gemini Example
Replacing OpenAI with Gemini is straightforward.
import { ChatGoogleGenerativeAI } from "@langchain/google-genai";
const model = new ChatGoogleGenerativeAI({
model: "gemini-2.5-flash",
});
const chain = prompt
.pipe(model)
.pipe(parser);
const result = await chain.invoke({
topic: "React",
});
console.log(result);
The chain structure remains unchanged.
Real-World Workflow
Imagine you're building an AI blog generator.
Blog Topic
↓
PromptTemplate
↓
Chat Model
↓
StringOutputParser
↓
Markdown Blog
Now imagine a resume analyzer.
Resume
↓
PromptTemplate
↓
Chat Model
↓
Structured Output
↓
JavaScript Object
The same Runnable principles apply in both cases.
Why This Architecture Scales
Suppose you later decide to:
- Change OpenAI to Gemini.
- Replace plain text with structured output.
- Add another processing step.
You don't need to rewrite the entire workflow.
You simply replace or add a Runnable.
For example:
Prompt
↓
Model
↓
Parser
↓
Formatter
↓
Application
Each component remains independent.
This modularity is one of LangChain's biggest strengths.
Best Practices
- Keep each Runnable focused on one responsibility.
- Reuse prompts, models, and parsers across multiple chains.
- Use
batch()for independent tasks. - Use
stream()for interactive user interfaces. - Compose workflows using
pipe()instead of manual orchestration.
Common Beginner Mistakes
- Calling individual components instead of invoking the composed chain.
- Using
batch()for dependent operations. - Ignoring streaming in chat-based applications.
- Combining multiple responsibilities into a single Runnable.
- Assuming only models are Runnables.
What's Next?
Now that you understand how Runnables execute and compose together, it's time to build more advanced workflows.
In the next article, we'll learn RunnableSequence.
You'll discover how to create multi-step AI pipelines where the output of one step automatically becomes the input of the next, enabling more sophisticated applications with minimal code.
Final Thoughts
The Runnable Interface is more than just a common API—it's the design philosophy behind LangChain. By giving every major component the same execution model, LangChain makes it easy to build modular, reusable AI workflows.
Whether you're invoking a single prompt, processing multiple requests in parallel, streaming responses, or composing complex chains, the Runnable Interface ensures a consistent developer experience.
With this foundation in place, you're ready to move beyond simple chains and start building more advanced AI pipelines using RunnableSequence and the rest of the Runnable ecosystem.
Happy Coding! 🚀