Understanding chaining in AI workflows and how LangChain Expression Language (LCEL) simplifies it.
When people first start using language models, they usually write code like this:
send a prompt → get a response → done
That works for demos. It does not work for real applications.
Real AI systems are workflows, not single calls.
They often need to:
The idea that connects all of this is called chaining.
Chaining simply means:
taking multiple steps and connecting them so the output of one step becomes the input of the next.
With language models, this idea shows up in a few common ways.
Language models are powerful, but they are not deterministic. They can make mistakes, hallucinate, or miss details.
Because of this, many applications use more than one model call.
Common patterns include:
For example:
Chaining makes these multi-step processes clear and manageable.
Very often, the model cannot work with raw user input alone.
Before calling the LLM, applications may need to:
This means the model call is only one step in a larger chain.
The data must be transformed and enriched before the LLM ever sees it.
Chaining does not stop when the model responds.
In many systems, the LLM's output is used to:
In these cases, the LLM is part of a pipeline, not the end of it.
All of this could be done with traditional code. So why do people keep reaching for chaining abstractions?
Because text has become a universal interface.
Language models take text as input and produce text as output. Prompts, responses, SQL, code, JSON—everything is text.
When everything is text flowing between steps, chaining becomes the most natural way to think.
This is why visual tools like Flowwise and LangFlow became popular, built on top of LangChain.
LangChain started with pre-built chains like LLMChain and ConversationalRetrievalChain.
They helped get started but were hard to combine, customize, and lacked consistent async/batching/streaming support.
Developers wanted composable, consistent chains with built-in features. The old approach couldn't deliver cleanly.
So LangChain introduced LCEL.
LangChain Expression Language (LCEL) is a declarative way to build chains using the pipe (|) operator.
Instead of large classes, compose small building blocks. Describe data flow, not execution control.
LCEL uses the pipe to connect components:
prompt | model
This passes the prompt's output directly to the model.
Example:
from langchain.chat_models import ChatOpenAI
from langchain.prompts import ChatPromptTemplate
model = ChatOpenAI()
prompt = ChatPromptTemplate.from_template("tell me a joke about {foo}")
chain = prompt | model
# Run it
result = chain.invoke({"foo": "bears"})
print(result.content)
Install LangChain and try this simple chain:
pip install langchain langchain-openai
Then run:
from langchain.chat_models import ChatOpenAI
from langchain.prompts import ChatPromptTemplate
# Set your OpenAI API key
import os
os.environ["OPENAI_API_KEY"] = "your-api-key-here"
model = ChatOpenAI()
prompt = ChatPromptTemplate.from_template("Summarize this text: {text}")
chain = prompt | model
# Test it
result = chain.invoke({"text": "LangChain makes building AI apps easy."})
print(result.content)
This creates a summarizer chain. Change the prompt or add more steps!
Every LCEL chain supports:
result = chain.invoke({"text": "Hello world"})
results = chain.batch([{"text": "First"}, {"text": "Second"}])
for chunk in chain.stream({"text": "Long text"}):
print(chunk.content, end="")
result = await chain.ainvoke({"text": "Async call"})
All built-in—no extra code needed.
LCEL chains are composable. Add steps easily:
from langchain.output_parsers import StrOutputParser
# Add output parsing
chain = prompt | model | StrOutputParser()
Swap models, add retrieval, or combine chains.
Here is a real chain that explains a concept, then roasts its own explanation, and finally compliments it.
First, choose your LLM provider:
Now build the pipeline:
This pipeline demonstrates LCEL's power:
topic inputexplain prompt to generate an explanationroast and compliment prompt in parallelThe pipeline runs automatically in parallel where possible, streaming results as they arrive.
Think of LCEL as an expression language for AI workflows.
You write:
input | transform | model | transform | output
The system handles execution, async, batching, streaming, tracing.
Once you think in flows, chaining becomes simple.
LCEL makes chaining clear, composable, and scalable.