LangChain for GenAI and AI Agents Handbook · PRACTICAL GUIDE

Build Reusable Prompts and Chains with LangChain

Turn one model call into a reusable LangChain prompt-to-model-to-output pipeline, then validate structured results before Python uses them.

HANDBOOK JOURNEYByte 2 of 5View all Bytes
HANDBOOK JOURNEYByte 2 of 5

LangChain for GenAI and AI Agents Handbook

32 min focused reading
  1. BYTE 01What Is LangChain?
  2. 03BYTE 03Build RAG Applications with Your Own Documents
  3. 04BYTE 04Add Conversation Memory to LangChain Applications
  4. 05BYTE 05Build LangChain Agents with Tools and Safety Controls
FAMILIAR SCENARIO

A translator needs a clear brief and output form

A translator receives the audience, source text and required format—not only “translate this”—then returns a result the next team can use.

01Configure
02Message
03Model
04Validate

Connect the idea: Good model integration includes context, roles and a predictable output contract.

Quick Start

LANGCHAIN ENGLISH 02

Turn separate AI steps into one reusable pipeline

Build a prompt template, connect it to a model and parse the response through a clear LCEL chain.

In Byte 1, we learned how to call a model. But real apps rarely stop at one question and one answer — you usually need multiple steps: clean the user's input, build a prompt, send it to the model, parse the output. In this byte we'll look at how to design Prompts, and how Chains (using LCEL — LangChain Expression Language) let you join these steps into a single pipeline.

Time investment: 20-25 mins.


Turning an Expense into a Reliable Category

Karthik's next task at PaisaWise: detect the expense category from a customer's message. When a user types "Spent 450 rupees on Swiggy," the system should automatically tag it as "Food & Dining." Karthik's first attempt used a manual f-string to build the prompt:

PYTHON
01prompt = f"Categorize this expense: {user_input}. Reply with only the category name."

Rahul asks: "That works, Karthik, but every time you want to reuse or edit this prompt, isn't it a pain?" Karthik admits: "Yeah, every edit means searching through the whole string." Divya chimes in: "That's exactly what PromptTemplate is for — reusable, variable-based prompts."


SCENARIO MAPSee why this concept is neededChoose a stage
CURRENT UNDERSTANDINGClassify every expense

Core Explanation

Think of a PromptTemplate as a fill-in-the-blanks letter — like a wedding invitation template: "Dear , please join us on ." The structure stays fixed; only the variables change for each guest.

Think of Chains as an assembly line. In a factory, raw material moves from one station to the next — cutting → stitching → packing. In a LangChain chain, it's: Prompt → Model → Output Parser — the output of one step becomes the input of the next. The LCEL pipe operator (|) lets you express this beautifully.

Meena asks: "How many steps can a chain have?" Divya: "As many as you need — 2 steps, 10 steps, even chains nested inside chains (sequential chains)."


Architecture / Flow Diagram

In LCEL, every component is a "Runnable" — it has common methods like .invoke(), .stream(), and .batch(). When you connect components with |, the output type of one has to match the expected input type of the next — that's what makes a chain "work."

Sequential chains — the output of step 1 becomes the input of step 2. Example: raw text → summarize it → translate it → categorize it.

Output Parsers — convert the model's raw text response into structured data (JSON, a list, or a specific format). This matters a lot, because downstream code (saving to a database, displaying in a UI) needs structured data, not free text.


INTERACTIVE WORKFLOWFollow the data one step at a timeStep 1 of 3

Prompt: Insert the expense text

LANGCHAIN CONCEPT LAB 02

Build one predictable prompt pipeline

Run the value through one stage at a time. A chain is useful when the order is fixed and every output has a known next destination.

LIVE SIMULATOR
PIPELINE STEP 1 OF 4User value"Swiggy ₹450"
Order
fixed
Next step
Prompt template
Agent choice
not needed
Safe practice environment — no provider request or external action is executed.

Code Walkthrough

PYTHON
01from langchain_core.prompts import ChatPromptTemplate02from langchain_openai import ChatOpenAI03from langchain_core.output_parsers import StrOutputParser0405# Reusable template - variables go in curly braces06prompt = ChatPromptTemplate.from_template(07    "Categorize this expense into ONE word (Food, Travel, Shopping, Bills, Other): {expense_text}"08)0910model = ChatOpenAI(model="gpt-4o-mini", temperature=0)11parser = StrOutputParser()1213# LCEL - use the pipe operator to build the chain14chain = prompt | model | parser1516result = chain.invoke({"expense_text": "Spent 450 rupees on Swiggy"})17print(result)  # Output: Food1819# Batch - process multiple expenses at once20expenses = [21    {"expense_text": "Uber ride to airport, 800 rs"},22    {"expense_text": "Electricity bill 1200 rs"},23]24results = chain.batch(expenses)25print(results)  # ['Travel', 'Bills']
CODE RESULTEXPENSE CATEGORY RETURNED
CLICK EACH EXECUTION STEP
EXPECTED OUTPUT
Single input: Food
Batch output: ["Travel", "Bills"]
VISUAL EXECUTIONChoose a stage to inspect it

Karthik was genuinely surprised by the pipe operator — "3 lines and I have a whole working pipeline!"


Practical Cost Scenario

Imagine a TCS-style internal project (as a case study) that used a chain like this to auto-categorize employee expense reports. Manually, the HR team categorizing 8,000 expense entries a month needed roughly a full-time person at ₹35,000/month. After automating it with a chain, that same task cost about ₹2,500/month in API costs (at GPT-4o-mini rates) — a 99% cost reduction, and the processing time dropped from 3 days to about 10 minutes.


Common Mistakes

  1. Vague instructions in the prompt — just saying "categorize this" can get you a free-text paragraph back. Be explicit: "Reply with ONE word only."
  2. Skipping the output parser — trying to use the raw string directly leads to parsing errors downstream.
  3. Over-engineering the chain — if 2 steps get the job done, don't add 5. Start simple, add complexity only when you actually need it.
  4. Type mismatches — if one component's output (say, a dict) doesn't match what the next component expects (a string), the chain will fail.

Scenario Result

After the feature shipped, Meena is happy to see 95%+ auto-categorization accuracy on the expense dashboard. Rahul feels a bit more confident too: "I might try chains for my resume screening tool." Divya nods: "Exactly right, Rahul — that's what we'll look at next, adding retrieval to make your chain even smarter."


Tool / Technology Comparison

ApproachWhen to Use
Single prompt + model callSimple one-shot tasks, prototyping
Chain (prompt | model | parser)Multi-step processing, structured output needed
Sequential chain (chain of chains)Complex workflows — summarize → translate → categorize

Practical Task

Build a chain that takes a customer review as input and returns two things: (1) sentiment (Positive/Negative/Neutral), and (2) a 5-word summary. Hint: structure the output parser as JSON so you can extract both fields separately.


Key Takeaways

  • PromptTemplate — reusable, variable-based prompts that replace hardcoded strings.
  • LCEL pipe operator (|) — joins components into readable, composable pipelines.
  • Chains — enable multi-step workflows: prompt → model → parser, and beyond.
  • Output parsers — convert free text into structured, usable data for downstream systems.
  • Start simple, and only add complexity when you actually need it.

Interactive Knowledge Check

LESSON CHECKPOINTConfirm the concept before moving forward

Choose an answer, inspect the explanation and explain the idea in your own words.

RETENTION
Learning rule: explain the answer in your own words before checking the next Byte.

FAQ + Knowledge Check

Q1: What's the difference between PromptTemplate and an f-string? PromptTemplate is reusable, validatable, and integrates seamlessly with LangChain's other components. f-strings are manual and error-prone at scale.

Q2: What happens if a step in the chain fails? By default, an exception is raised and the chain stops. If you need error handling, use .with_retry() or wrap it in try/except.

Q3: Is LCEL mandatory? No, but it's recommended — it gives you readable code and automatic support for streaming, async, and batch operations.

Knowledge Check:

  1. How many steps are in the chain prompt | model | parser?
  2. Why do we use an output parser?
  3. True/False: You can nest a chain inside another chain.

(Answers: 1. Three steps; 2. To get structured, usable output; 3. True)


Next byte: Document Loading, Embeddings & Retrieval — connecting external data to your chain (the foundation of RAG).

OPTIONAL LEARNING CONNECTIONS

Continue by concept

Choose only what supports your next goal. This Byte does not require either link.