0
0
LangChainframework~5 mins

Prompt composition and chaining in LangChain

Choose your learning style9 modes available
Introduction

Prompt composition and chaining help you build complex tasks by connecting simple prompts step-by-step. This makes your AI work smarter and more organized.

You want to break a big question into smaller, easier questions.
You need to use the answer from one prompt as input for the next.
You want to combine different AI tasks like summarizing and translating.
You want to create a workflow where each step depends on the previous one.
Syntax
LangChain
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain, SimpleSequentialChain

# Define individual prompt templates
prompt1 = PromptTemplate(input_variables=["name"], template="Hello {name}, how can I help you today?")
prompt2 = PromptTemplate(input_variables=["response"], template="You said: {response}. Can you tell me more?")

# Create chains for each prompt
chain1 = LLMChain(llm=llm, prompt=prompt1)
chain2 = LLMChain(llm=llm, prompt=prompt2)

# Chain them together
overall_chain = SimpleSequentialChain(chains=[chain1, chain2])

# Run the chain
result = overall_chain.run("Alice")

Each prompt template defines how to format the input for the AI.

Chains connect prompts so outputs flow from one to the next automatically.

Examples
This shows what happens if you give an empty input to a prompt chain.
LangChain
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain, SimpleSequentialChain

# Empty input example
prompt1 = PromptTemplate(input_variables=["text"], template="Summarize: {text}")
chain1 = LLMChain(llm=llm, prompt=prompt1)

# Running with empty string
result = chain1.run("")
Using just one prompt in the chain to keep it simple.
LangChain
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain, SimpleSequentialChain

# Single prompt chain
prompt1 = PromptTemplate(input_variables=["topic"], template="Explain {topic} simply.")
chain1 = LLMChain(llm=llm, prompt=prompt1)

result = chain1.run("photosynthesis")
This example chains two prompts so the second explains the first answer.
LangChain
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain, SimpleSequentialChain

# Chain with two steps
prompt1 = PromptTemplate(input_variables=["question"], template="Answer this: {question}")
prompt2 = PromptTemplate(input_variables=["answer"], template="Explain why: {answer}")

chain1 = LLMChain(llm=llm, prompt=prompt1)
chain2 = LLMChain(llm=llm, prompt=prompt2)

overall_chain = SimpleSequentialChain(chains=[chain1, chain2])

result = overall_chain.run("What is AI?")
Sample Program

This program first asks the AI to define a topic, then asks why that definition matters. It shows how chaining passes the first answer to the next prompt.

LangChain
from langchain.llms import OpenAI
from langchain.prompts import PromptTemplate
from langchain.chains import SimpleSequentialChain

# Initialize the language model
llm = OpenAI(temperature=0)

# Define the first prompt template
prompt1 = PromptTemplate(
    input_variables=["topic"],
    template="Give a short definition of {topic}."
)

# Define the second prompt template
prompt2 = PromptTemplate(
    input_variables=["definition"],
    template="Explain why this definition is important: {definition}"
)

# Create chains for each prompt
from langchain.chains import LLMChain
chain1 = LLMChain(llm=llm, prompt=prompt1, output_key="definition")
chain2 = LLMChain(llm=llm, prompt=prompt2, input_key="definition")

# Chain them together
overall_chain = SimpleSequentialChain(chains=[chain1, chain2])

# Run the chain with a topic
result = overall_chain.run("machine learning")

print("Final output:", result)
OutputSuccess
Important Notes

Prompt chaining helps organize complex tasks into simple steps.

Time complexity depends on the number of chained prompts and AI response time.

Common mistake: forgetting to pass output keys correctly between chains.

Summary

Prompt composition breaks big tasks into smaller prompts.

Chaining connects prompts so outputs flow smoothly.

This makes AI workflows easier to build and understand.