Mod 2.4Building Simple Multi-Step LLM Workflows
Level 2›Module 2.4
Level 2: Core Implementation & WorkflowsModule 2.4

Building Simple Multi-Step LLM Workflows

Multi-Step LLM

Level 2 • Core Implementation & Workflows
Est. ~15 mins
5 Key Topics
🎁 Free Learner Perk

Unlock Verified Certificate & Daily Streak Tracker

Ready to master Building Simple Multi-Step LLM Workflows? Enable cloud sync to record your daily streak 🔥 and earn your Informational Completion Badge for your study milestones.

Day 1 Streak ActiveFree Completion BadgeSync Laptop & Phone

🎯 By the end of this module, you will:

Build multi-step LLM workflows using LCEL's pipe operator (|)
Compose PromptTemplate → LLM → OutputParser chains without boilerplate
Pass state between sequential chain steps as structured typed objects
Choose correctly between deterministic chains vs autonomous agent loops
LCEL Chains • Sequential Workflows

Building Multi-Step LLM Workflows

Sequential chains give you 100% predictable, deterministic pipelines — when you know every step in advance, chains beat autonomous loops every time for speed, cost, and reliability.

1. Prompt Template: Safe Variable InjectionBlock 1
# Block 1: Prompt Template with dynamic variables
from langchain_core.prompts import ChatPromptTemplate

draft_prompt = ChatPromptTemplate.from_messages([
    ("system", "You are an expert technical writer."),
    ("human", "Write a detailed but concise blog post about: {topic}")
])

# Variables are safely injected at runtime, not as f-strings
# This prevents prompt injection attacks from user input

⚖️ When to Use Chains vs. Agents

✅ Use a Chain When:
  • • Steps are known in advance (deterministic)
  • • You need strict cost control per request
  • • Predictable latency is required (<2s SLA)
  • • Output quality is consistent across runs
✅ Use an Agent When:
  • • Steps cannot be predicted in advance
  • • Task requires adaptive tool selection
  • • Multiple attempts/retries may be needed
  • • Task complexity varies widely per query

🔬 Multi-Step Chain Builder

Multi-Step LCEL Chain SimulatorPrompt | Model | Parser

Visualize how deterministic multi-step chains pipe state cleanly from one node to the next

PromptTemplate
Step 1
LLM
Step 2
PromptTemplate
Step 3
OutputParser
Step 4
Step Execution Details
Step 1: Prompt Template (Draft)PromptTemplate
Input: Topic: 'Why SQLite is great for local agents'
# Output Payload:Prompt formatted: "Write a 2-sentence draft highlighting speed and zero-configuration for SQLite in local AI agents."
Python LCEL Pipe Architecture
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_openai import ChatOpenAI

# 1. Compose chain with pipe operator (|)
prompt = ChatPromptTemplate.from_template("Improve this text: {input}")
model = ChatOpenAI(model="gpt-4o-mini")
parser = StrOutputParser()

# Pure, clean, deterministic chain
chain = prompt | model | parser

# 2. Invoke chain
result = chain.invoke({"input": "Draft text..."})
✍️ Instructor Note: "LCEL chains are synchronous by default. If you're building a web server, use chain.ainvoke() for async execution — synchronous chain.invoke() in a FastAPI handler will block your entire event loop!"
💡 Mental Model: LCEL chains are like an assembly line in a factory — Part A goes to Station 1 (template), gets processed, goes to Station 2 (LLM), gets processed, then to Station 3 (parser). Each station does exactly one job.
📌 Core Rule: Don't chain more than 3-4 LLM calls in a single user request. Each call adds latency, cost, and a chance of error. If your workflow has 8+ LLM steps, redesign it as an agent loop with early exit conditions.

Multi-Step Chain Traps

TRAP #1: Context Loss Between Steps

By default, each chain step only sees what you explicitly pass to it. If Step 2 needs context from Step 1 AND the original input, you must manually pass both. Always design state passing explicitly — nothing flows automatically.

TRAP #2: Using Chains for Dynamic Tasks

Don't force a sequential chain onto tasks that require conditional branching (e.g., "if the search fails, try a different query"). Conditional logic belongs in a LangGraph agent, not a linear LCEL chain.

Key Takeaways

  • 1.Chains for Determinism: Use LCEL chains when every step is known, latency is critical, and cost must be predictable. Chains give you assembly-line reliability.
  • 2.Pipe Operator (|) is Declarative: prompt | model | parser reads exactly like the data flow. LCEL chains are self-documenting by design.
  • 3.Explicit State Passing: Nothing flows between chain steps unless you pass it. Design your state contracts (what each step receives) before writing any code.
Up Next • Module 2.5

Create an Agent Class from Scratch in Python

Strip away all frameworks. Build a raw Python Agent class with an LLM caller, a tool executor, and a manual ReAct while loop — so you understand every abstraction from the inside out.

Continue to Module 2.5