LangChain Series #4: Chains Explained — Building AI Workflows with LCEL LangChain released the fourth installment of its learning series, explaining how to build AI workflows using Chains and the LangChain Expression Language (LCEL). The article demonstrates Simple, Sequential, Parallel, and Conditional Chains with Python examples, showing how LCEL's pipe operator simplifies connecting prompts, models, and parsers into scalable pipelines. This is the fourth article in my LangChain learning series. In the previous article, we explored prompts and learned how to communicate effectively with language models. Now it’s time to connect those components into complete AI workflows. In this article, we’ll explore Chains in LangChain and learn how to build Simple, Sequential, Parallel, and Conditional Chains using the LangChain Expression Language LCEL . Through practical Python examples, you’ll understand how these chain patterns simplify the development of scalable, maintainable AI applications. In the previous article, we learned how to create prompts using PromptTemplate, ChatPromptTemplate, and Output Parsers. But a real AI application doesn’t stop after sending a single prompt to an LLM. Most applications involve multiple steps: This is exactly what Chains helps us accomplish. A Chain connects multiple LangChain components into a single executable workflow. A Chain is a sequence of connected components where the output of one component becomes the input of the next. Instead of manually calling every component, LangChain lets us connect them using the LangChain Expression Language LCEL . Without chains: prompt.invoke model.invoke parser.invoke We manually call every component. With chains: chain = prompt | model | parser One line executes the entire workflow. Benefits: LCEL introduces the | operator. Instead of writing nested function calls, we compose pipelines. chain = prompt | model | parser Each component automatically passes its output to the next component. This makes AI workflows easier to build and maintain. A Simple Chain is the most basic LangChain workflow. It connects a prompt, an LLM, and an output parser into a single pipeline. The output of one component automatically becomes the input of the next. python from langchain huggingface import ChatHuggingFace, HuggingFaceEndpointfrom dotenv import load dotenvfrom langchain core.prompts import PromptTemplatefrom langchain core.output parsers import StrOutputParser Load environment variablesload dotenv Prompt templateprompt = PromptTemplate template="Generate 5 interesting facts about {topic}", input variables= "topic" Load Hugging Face modelllm = HuggingFaceEndpoint repo id="Qwen/Qwen2.5-7B-Instruct", task="text-generation" model = ChatHuggingFace llm=llm Convert AIMessage - Stringparser = StrOutputParser Build the chainchain = prompt | model | parser Executeresult = chain.invoke { "topic": "cricket"} print result Visualize chainchain.get graph .print ascii A Sequential Chain is a workflow where the output of one chain becomes the input of the next chain . Unlike a Simple Chain, which performs only one task, a Sequential Chain breaks a complex problem into multiple steps, with each step depending on the previous one. For example, instead of asking an LLM to generate a report and summarize it in a single prompt, we can split the task into two stages: This approach makes the workflow more modular, reusable, and easier to maintain. python from dotenv import load dotenvfrom langchain huggingface import ChatHuggingFace, HuggingFaceEndpointfrom langchain core.prompts import PromptTemplatefrom langchain core.output parsers import StrOutputParserload dotenv --------------------------------------- Load Hugging Face Model ---------------------------------------llm = HuggingFaceEndpoint repo id="meta-llama/Llama-3.1-8B-Instruct", task="text-generation" model = ChatHuggingFace llm=llm --------------------------------------- Output Parser ---------------------------------------parser = StrOutputParser --------------------------------------- Prompt 1 Generate a Detailed Report ---------------------------------------report prompt = PromptTemplate template="""Write a detailed report about {topic}.Include:- Introduction- Working Principle- Advantages- Applications- Conclusion""", input variables= "topic" --------------------------------------- Prompt 2 Summarize the Report ---------------------------------------summary prompt = PromptTemplate template="""Summarize the following report in 5 concise bullet points.Report:{report}""", input variables= "report" --------------------------------------- Sequential Chain ---------------------------------------chain = report prompt | model | parser | {"report": lambda x: x} | summary prompt | model | parser --------------------------------------- Execute Chain ---------------------------------------result = chain.invoke { "topic": "Retrieval-Augmented Generation RAG "} print result A Parallel Chain is a workflow where multiple chains execute simultaneously on the same input , instead of running one after another. Unlike a Sequential Chain, where each step waits for the previous one to finish, a Parallel Chain performs independent tasks at the same time. This reduces execution time and allows you to generate multiple outputs from the same input. Use a Parallel Chain when multiple tasks: python from dotenv import load dotenvfrom langchain huggingface import ChatHuggingFace, HuggingFaceEndpointfrom langchain core.prompts import PromptTemplatefrom langchain core.output parsers import StrOutputParserfrom langchain core.runnables import RunnableParallelload dotenv ------------------------------- Load Hugging Face Model -------------------------------llm = HuggingFaceEndpoint repo id="Qwen/Qwen2.5-7B-Instruct", task="text-generation" model = ChatHuggingFace llm=llm parser = StrOutputParser ------------------------------- Prompt 1: Generate Notes -------------------------------notes prompt = PromptTemplate template="""Generate concise study notes about the following topic.Topic:{topic}""", input variables= "topic" ------------------------------- Prompt 2: Generate Quiz -------------------------------quiz prompt = PromptTemplate template="""Generate five multiple-choice questions about the following topic.Topic:{topic}""", input variables= "topic" ------------------------------- Create Parallel Chain -------------------------------parallel chain = RunnableParallel { "Notes": notes prompt | model | parser, "Quiz": quiz prompt | model | parser} ------------------------------- Execute -------------------------------result = parallel chain.invoke { "topic": "LangChain"} print "========== NOTES ==========\n" print result "Notes" print "\n========== QUIZ ==========\n" print result "Quiz" A Conditional Chain is a workflow where the next chain is chosen based on a condition or decision . Unlike a Sequential Chain, which always follows the same execution path, a Conditional Chain dynamically decides which branch to execute depending on the output of a previous step. For example, imagine you’re building a customer support system. When a customer submits feedback, the application first analyzes the sentiment. If the sentiment is positive , it sends a thank-you response. If it’s negative , it forwards the complaint to customer support. Instead of executing every chain, LangChain runs only the branch whose condition is satisfied . Use a Conditional Chain whenever your application needs to make decisions before choosing the next action. Examples include: python from dotenv import load dotenvfrom langchain huggingface import ChatHuggingFace, HuggingFaceEndpointfrom langchain core.prompts import PromptTemplatefrom langchain core.output parsers import StrOutputParserfrom langchain core.runnables import RunnableBranchload dotenv ------------------------------------ Load Model ------------------------------------llm = HuggingFaceEndpoint repo id="Qwen/Qwen2.5-7B-Instruct", task="text-generation" model = ChatHuggingFace llm=llm parser = StrOutputParser ------------------------------------ Prompt for Positive Feedback ------------------------------------positive prompt = PromptTemplate template="""Write a polite thank-you email for the following positive customer feedback.Feedback:{feedback}""", input variables= "feedback" ------------------------------------ Prompt for Negative Feedback ------------------------------------negative prompt = PromptTemplate template="""Write an apology email and inform the customer that the issue has been forwarded to customer support.Feedback:{feedback}""", input variables= "feedback" ------------------------------------ Create Conditional Chain ------------------------------------chain = RunnableBranch lambda x: "good" in x "feedback" .lower or "excellent" in x "feedback" .lower or "amazing" in x "feedback" .lower , positive prompt | model | parser , negative prompt | model | parser ------------------------------------ Execute ------------------------------------result = chain.invoke { "feedback": "The service was excellent and your support team was amazing."} print result Chains are the backbone of LangChain applications. Instead of manually managing prompts, models, and parsers, LCEL allows us to connect components into reusable AI workflows using a simple pipeline syntax. By understanding Simple , Sequential , Parallel , and Conditional Chains, you can build applications ranging from basic chatbots to sophisticated AI systems with multiple decision paths. In the next article, we’ll explore Runnables in LangChain and learn how the Runnable interface powers LCEL through composition, parallel execution, branching, and reusable AI workflows. Code Repository: https://github.com/Atul245/LangChain-for-developers https://github.com/Atul245/LangChain-for-developers If you found this article helpful, consider: ⭐ Starring the repository 🍴 Forking the repository 👤 Following me for more content on LangChain, Generative AI, and AI Engineering Connect with me on LinkedIn: https://www.linkedin.com/in/atulkumar8/ https://www.linkedin.com/in/atulkumar8/ 🚀 LangChain Series 4: Chains Explained — Building AI Workflows with LCEL https://pub.towardsai.net/langchain-series-4-chains-explained-building-ai-workflows-with-lcel-1d4f125ea740 was originally published in Towards AI https://pub.towardsai.net on Medium, where people are continuing the conversation by highlighting and responding to this story.