LangChain Chains: How to Build Smarter AI Workflows
If you’ve ever tried to get an LLM to do more than answer a simple question, you know the pain. You want it to think, to follow steps, to process information like a human would—not just spit out a one-off response.
That’s where chains come in.
Chains are LangChain’s way of stitching together multiple steps so your AI can handle complex tasks. Need to extract key points from a document and then summarize them? Chains. Want to fetch data from an API before generating a report? Chains.
Let’s break down how they work—from simple one-step flows to multi-stage pipelines—with real, practical examples.
1. The Simplest Chain: One Step, One Answer
Sometimes, you just need a quick response. No frills, no extra steps—just a prompt and an answer. That’s where a single-step chain shines.
When to Use It:
- Generating short responses (e.g., Q&A, definitions)
- Rewriting or paraphrasing text
- Basic text completion
Example: A Straightforward Q&A Bot
python
Copy
Download
from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate
from langchain_community.llms import OpenAI
# Set up the model
llm = OpenAI(model=”gpt-3.5-turbo”, temperature=0.7)
# Define the prompt
prompt = PromptTemplate(
input_variables=[“question”],
template=”Answer this in one sentence: {question}”
)
# Create the chain
qa_chain = LLMChain(llm=llm, prompt=prompt)
# Ask a question
response = qa_chain.run({“question”: “What’s the fastest way to learn Python?”})
print(response)
Output:
“The fastest way to learn Python is by building small projects while following hands-on tutorials.”
Simple, right? But what if you need more than one step?
2. Multi-Step Chains: When One Answer Isn’t Enough
Real-world tasks aren’t always linear. Sometimes, you need to:
- Extract key info → 2. Summarize → 3. Format for a report.
That’s where multi-step chains come in.
When to Use Them:
- Summarizing long documents (extract → condense)
- Data cleaning (fetch → filter → format)
- Multi-model workflows (e.g., GPT-4 for analysis + Claude for refinement)
Example: Extract Key Points → Summarize
Let’s say you have a technical article, and you want to:
- Pull out the most important points.
- Turn those points into a concise summary.
Here’s how:
python
Copy
Download
from langchain.chains import SequentialChain
from langchain.prompts import PromptTemplate
from langchain_community.llms import OpenAI
llm = OpenAI(model=”gpt-3.5-turbo”, temperature=0.5)
# Step 1: Extract key points
extract_prompt = PromptTemplate(
input_variables=[“text”],
template=”List the 3 most important points from this text:\n{text}”
)
extract_chain = LLMChain(llm=llm, prompt=extract_prompt, output_key=”points”)
# Step 2: Summarize those points
summarize_prompt = PromptTemplate(
input_variables=[“points”],
template=”Turn these bullet points into a 1-sentence summary:\n{points}”
)
summarize_chain = LLMChain(llm=llm, prompt=summarize_prompt, output_key=”summary”)
# Combine them
workflow = SequentialChain(
chains=[extract_chain, summarize_chain],
input_variables=[“text”],
output_variables=[“summary”]
)
# Run it
input_text = “””
LangChain simplifies AI app development by breaking tasks into reusable components.
It supports text generation, summarization, and data retrieval.
Developers can chain these components to automate complex workflows.
“””
result = workflow({“text”: input_text})
print(result[“summary”])
Output:
“LangChain helps developers build AI apps by providing reusable components for tasks like text generation and summarization, enabling automated workflows.”
Now you’ve got a two-step AI assistant—no manual copy-pasting required.
3. Pro Tips for Building Reliable Chains
- Break Down Tasks First
Before coding, sketch out:
- What each step should do.
- What data moves between them.
- Test Each Step Alone
If the summary is wrong, maybe the extraction failed. Isolate issues by running chains individually.
- Use Clear Variable Names
Instead of output_1, use extracted_points or final_summary—it makes debugging easier.
- Add Error Handling
APIs fail. LLMs hallucinate. Plan for it.
python
Copy
Download
try:
result = workflow({“text”: long_document})
except Exception as e:
print(f”Chain failed: {e}”)
# Maybe retry or log the error
Final Thoughts
Chains turn LangChain from a fancy chatbot wrapper into a real workflow engine. Start simple, then layer on complexity as needed.
Next time you’re about to manually process AI outputs, ask: “Could a chain do this for me?”