0
0
LangChainframework~10 mins

Prompt composition and chaining in LangChain - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Prompt composition and chaining
Define Base Prompts
Compose Prompts Together
Chain Prompts Sequentially
Pass Output of One as Input to Next
Get Final Combined Result
Start by creating simple prompts, then combine them into a chain where each prompt's output feeds the next, producing a final result.
Execution Sample
LangChain
from langchain import PromptTemplate, LLMChain

# Define two prompts
prompt1 = PromptTemplate(input_variables=["name"], template="Hello {name}!")
prompt2 = PromptTemplate(input_variables=["greeting"], template="{greeting} How can I help you?")
This code defines two simple prompts: one greets by name, the other adds a question using the greeting.
Execution Table
StepActionInputOutputNext Input
1Run prompt1 with name='Alice'name='Alice'Hello Alice!greeting='Hello Alice!'
2Run prompt2 with greeting='Hello Alice!'greeting='Hello Alice!'Hello Alice! How can I help you?None
3Chain completeNoneHello Alice! How can I help you?End
💡 All prompts executed in sequence; final output produced.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
nameNoneAliceAliceAlice
greetingNoneHello Alice!Hello Alice!Hello Alice!
final_outputNoneNoneHello Alice! How can I help you?Hello Alice! How can I help you?
Key Moments - 2 Insights
Why does the output of the first prompt become the input for the second?
Because in chaining, the output of one prompt is passed as input to the next, as shown in execution_table step 1 and 2.
Can prompts be chained in any order?
No, the order matters because each prompt may depend on the previous output, as seen in the flow from step 1 to step 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output after step 1?
AHello Alice!
BHello Alice! How can I help you?
CAlice
DHow can I help you?
💡 Hint
Check the 'Output' column in row for step 1.
At which step does the final combined result appear?
AStep 1
BStep 2
CStep 3
DNone
💡 Hint
Look at the 'Output' and 'Action' columns for step 3.
If the first prompt output changed, how would it affect the chain?
ANo effect on the final output
BThe second prompt input and final output change
COnly the first prompt output changes
DChain breaks and stops
💡 Hint
Refer to how output of step 1 is input to step 2 in execution_table.
Concept Snapshot
Prompt composition and chaining:
- Define simple prompts with variables
- Combine prompts sequentially
- Output of one prompt feeds next
- Final output is combined result
- Order matters for correct chaining
Full Transcript
Prompt composition and chaining in Langchain means creating small prompts and linking them so the output of one becomes the input of the next. This lets you build complex conversations or tasks step-by-step. For example, a greeting prompt outputs 'Hello Alice!' which then feeds into a second prompt that adds 'How can I help you?'. The chain runs in order, producing a final combined message. This method helps organize prompts clearly and reuse parts easily.