How does Prompt Chaining work?
- Authors
- Name
- Amit Shekhar
- Published on
In this blog, we will learn about how Prompt Chaining works. We will also see why we need it, how it works step by step by passing the output of one prompt into the next, and where it is used in the real world to solve bigger tasks reliably.
We will cover the following:
- What is a prompt?
- What is Prompt Chaining?
- Why do we need Prompt Chaining?
- How does Prompt Chaining work step by step?
- A real example of Prompt Chaining
- Code example of Prompt Chaining
- Common patterns in Prompt Chaining
- Advantages of Prompt Chaining
- Things to take care of while using Prompt Chaining
- When to use Prompt Chaining
I am Amit Shekhar, Founder @ Outcome School, I have taught and mentored many developers, and their efforts landed them high-paying tech jobs, helped many tech companies in solving their unique problems, and created many open-source libraries being used by top companies. I am passionate about sharing knowledge through open-source, blogs, and videos.
I teach AI and Machine Learning at Outcome School.
Let's get started.
What is a prompt?
Before jumping into Prompt Chaining, we must know what a prompt is.
A prompt is the instruction or question that we give to an AI model.
In simple words, a prompt is what we type to tell the AI what we want.
Let's say we open an AI chat tool and type "Write a short poem about the rain." That sentence is the prompt. The AI reads our prompt and gives us an answer back. That answer is called the response.
So, a prompt goes in, and a response comes out. This is the simplest way to talk to an AI model.
Now, we have understood what a prompt is. Now, it's time to learn about Prompt Chaining.
What is Prompt Chaining?
Prompt Chaining is a way of breaking one big task into smaller prompts, where the output of one prompt becomes the input of the next prompt.
Let's decompose the term to understand it better.
Prompt Chaining = Prompt + Chaining
Here, "Prompt" is the instruction we give to the AI. And "Chaining" means joining things one after another, like the links of a chain.
In simple words, instead of asking the AI to do everything in one single big prompt, we ask it in small steps. We take the answer from the first step and feed it into the second step. Then we take the answer from the second step and feed it into the third step, and so on.
So, each step depends on the answer of the step before it. The steps are connected like a chain. That is why we call it Prompt Chaining.
Why do we need Prompt Chaining?
The best way to learn this is by taking an example.
Suppose we want the AI to do this big task for us:
"Read this long customer review, find out if the customer is happy or angry, and then write a polite reply to the customer."
We can try to do this in one single prompt. Let's call this Approach 1.
Approach 1: One big prompt
We write everything in one prompt as below:
Here is a customer review: "..."
Find out if the customer is happy or angry,
and then write a polite reply to the customer.
Here, we are asking the AI to do many things at once. It must read the review, decide the mood, and write a reply, all in one go.
The issue with this approach is that the AI tries to do too many things in one step. The mood detection can be wrong, and the reply can become messy. When one big prompt does many jobs, mistakes become hard to find and hard to fix.
Let's see how the next approach solves this issue.
Approach 2: Prompt Chaining
Now, we break the big task into small steps. Each step has its own small prompt.
So, here comes Prompt Chaining to the rescue.
We split the work like below:
- Step 1: Ask the AI to read the review and tell us only the mood (happy or angry).
- Step 2: Take that mood, and ask the AI to write a reply that matches the mood.
Here, each prompt does only one small job. The first prompt finds the mood. The second prompt uses that mood to write the reply.
This way the work becomes clean and clear. If something goes wrong, we know exactly which step to fix. The problem is solved.
We can picture the difference between the two approaches as below:
Approach 1: One big prompt
+-------------------------------+ +---------------+
| Read review + find mood + | | |
| write reply (all in one go) | ---> | Reply (messy)|
+-------------------------------+ +---------------+
Approach 2: Prompt Chaining
+----------------+ mood +----------------+
| Find the mood | --------> | Write reply | ---> Reply
| (happy/angry) | | for that mood | (clean)
+----------------+ +----------------+
Here, we can see that in Approach 1 a single prompt is forced to do every job at once, so the reply can come out messy. In Approach 2, the first prompt only finds the mood, and that mood is passed into the second prompt, which writes the reply. Each box does one small job.
This is why we need Prompt Chaining. It makes a hard task easy by splitting it into small, simple steps.
To learn Prompt Chaining, Prompt Engineering, and how AI Agents break a big task into steps, we have our AI and Machine Learning Program at Outcome School.
How does Prompt Chaining work step by step?
Now, let's understand how Prompt Chaining actually works, one step at a time.
The idea is simple. We pass the output of one prompt as the input to the next prompt.
Let's say we have three steps. Here is how the flow works:
- Step 1: We give the first prompt to the AI. The AI gives us an output. Let's call it Output 1.
- Step 2: We take Output 1 and put it inside the second prompt. The AI gives us Output 2.
- Step 3: We take Output 2 and put it inside the third prompt. The AI gives us the final answer.
Let's visualize this flow as below:
+---------+ Output 1 +---------+ Output 2 +---------+
| Prompt | -----------> | Prompt | -----------> | Prompt |
| Step 1 | | Step 2 | | Step 3 |
+---------+ +---------+ +---------+
^ |
| v
first input Final answer
Here, we can see that the output of each step becomes the input of the next step. Output 1 from Step 1 flows into Step 2, and Output 2 from Step 2 flows into Step 3, which gives us the final answer.
So, Output 1 feeds Step 2, and Output 2 feeds Step 3. Each output becomes the input of the next step. This is the heart of Prompt Chaining.
For better understanding, let's map this to a real-world team.
| Prompt Chaining role | Real-world comparison |
|---|---|
| The full task | A project given to a team |
| Each small prompt | One team member doing one job |
| Output passed forward | One member handing their work to the next member |
| Final answer | The finished project |
Here, we can see that Prompt Chaining works just like a team in a factory line. Each worker does one small job and passes the result to the next worker. The last worker hands over the finished product.
This is how Prompt Chaining works at a high level.
A quick note for you
No matter which tech domain you work in, get familiar with these topics:
- LLM
- RAG
- MCP
- Agent
- Fine-tuning
- Quantization
We put it all together in one video:
AI Engineering Explained: LLM, RAG, MCP, Agent, Fine-Tuning, and Quantization
No need to stop reading - bookmark it and watch later when you get time. Future you will thank you.
Now, let's get back to the topic.
A real example of Prompt Chaining
Let's take a real example so that everything becomes clear.
Suppose we run a blog website. We want to take a long article and turn it into a short social media post.
This is a big task. So, we break it into a chain of small prompts.
Step 1: Summarize the article.
We give the AI this prompt:
Summarize the following article in 3 short sentences:
"<the full long article goes here>"
Here, the AI reads the long article and gives us a short summary of three sentences. This summary is our Output 1.
Step 2: Turn the summary into a social media post.
Now, we take Output 1 (the summary) and put it into the next prompt as below:
Turn the following summary into a short, catchy social media post:
"<the summary from Step 1 goes here>"
Here, the AI does not see the long article again. It only sees the short summary from Step 1. The AI now writes a catchy post based on that summary. This is our Output 2.
Step 3: Add hashtags.
Finally, we take Output 2 (the social media post) and put it into the last prompt as below:
Add 3 relevant hashtags to the end of this social media post:
"<the social media post from Step 2 goes here>"
Here, the AI adds three hashtags to the post. This is our final answer, ready to be published.
We have followed the following steps:
- We summarized the long article.
- We turned the summary into a post.
- We added hashtags to the post.
Each step used the output of the step before it. This way we used Prompt Chaining to solve the task in a very simple way.
Code example of Prompt Chaining
Now, let's see the code for a simple Prompt Chaining. We will use Python for the sake of understanding.
We can write the code as below:
# A made-up helper that sends a prompt to the AI and returns the answer.
def ask_ai(prompt):
# send the prompt to the AI model and get the response back
return call_the_ai_model(prompt)
# The long article that we want to process.
article = "<the full long article goes here>"
# Step 1: Get a short summary.
summary = ask_ai("Summarize this article in 3 short sentences: " + article)
# Step 2: Turn the summary into a social media post.
post = ask_ai("Turn this summary into a catchy social media post: " + summary)
# Step 3: Add hashtags to the post.
final_post = ask_ai("Add 3 relevant hashtags to this post: " + post)
print(final_post)
Here, we can see the whole chain in action.
- The
ask_aifunction takes a prompt, sends it to the AI model, and returns the answer. - In Step 1, we pass the
articleand get back asummary. - In Step 2, we pass the
summary(not the full article) and get back apost. - In Step 3, we pass the
postand get back thefinal_post.
Here, we can notice that the output of one step is passed into the next step as input. The summary flows into Step 2, and the post flows into Step 3. This is exactly what Prompt Chaining means in code.
It works perfectly.
In real projects, frameworks like LangChain let us build these chains without wiring each step by hand. We have a detailed blog on How does LangChain work that covers this end to end.
Common patterns in Prompt Chaining
Now, let's understand a few common patterns that we use in Prompt Chaining.
Pattern 1: The straight chain.
This is the simplest pattern. Step 1 feeds Step 2, Step 2 feeds Step 3, and so on, in a straight line. Our blog example above used this pattern.
Pattern 2: Extract, then act.
First, we ask the AI to pull out some important information from a piece of text. Then, in the next prompt, we ask the AI to do something with that information.
For example, first extract the name and email from a message. Then write a personal reply using that name.
Pattern 3: Generate, then check.
First, we ask the AI to write something. Then, in the next prompt, we ask the AI to check its own work and fix any mistakes.
For example, first write a piece of code. Then ask the AI to review the code and correct any bug.
This generate-then-check idea is exactly how a Reflection Agent works, where the model reviews and improves its own output.
So, now we know the common patterns where we can use Prompt Chaining. We can pick the pattern based on our use case.
Advantages of Prompt Chaining
Now, let's understand why Prompt Chaining is so useful.
Advantages:
- Each step is simple. Every prompt does only one small job, so the AI gives better answers.
- Easy to fix mistakes. If the final answer is wrong, we can check each step and find exactly where the problem started.
- Easy to reuse. We can use the same small prompt in many different chains.
- Better control. We can look at the output of each step and even change it before passing it to the next step.
- Clear thinking. Breaking a big task into small steps helps the AI think in a clear order, just like we do.
This is how Prompt Chaining makes our life easier when we work on complex tasks.
Things to take care of while using Prompt Chaining
Now, it's time to learn about a few things we must take care of.
But, here is the catch. Prompt Chaining is powerful, but we must use it carefully.
Note: Each step makes one more call to the AI model. So, a chain with five steps makes five calls. More steps mean more time and more cost. We must keep our chain only as long as needed.
One more thing to notice is the error spreading. If Step 1 gives a wrong output, then Step 2 works on that wrong output, and the mistake travels down the whole chain. So, we must make sure each early step is correct before moving forward.
Another thing to take care of is to pass only the needed information to the next step. In our blog example, Step 2 received only the short summary, not the whole long article. This keeps each prompt small, fast, and focused.
So, we must keep our chain short, check the early steps, and pass only what is needed.
To go deeper into Context Engineering, Orchestration, and Prompt Chaining, we teach all of it in our AI and Machine Learning Program at Outcome School.
When to use Prompt Chaining
Now, the question is, when must we use Prompt Chaining?
We must use Prompt Chaining when a task is too big or has many clear steps inside it.
Here are a few example use cases:
- Reading a document, then answering questions about it.
- Translating a text, then making the translation more polite.
- Pulling data out of a form, then saving it in a clean format.
- Writing a draft, then improving the draft, then checking it for mistakes.
If a task feels like one giant instruction with many parts, that is a strong sign to break it into a chain.
But, if a task is simple and small, then we do not need Prompt Chaining. A single prompt is enough. We must choose based on our use case.
Now, we have understood how Prompt Chaining works. We learned that it means breaking a big task into small prompts, where the output of one prompt becomes the input of the next prompt. We saw a real example, a code example, the common patterns, the advantages, and the things to take care of.
This way we can use Prompt Chaining to solve any complex AI task in a very simple way.
Prepare yourself for AI Engineering Interview: AI Engineering Interview Questions
That's it for now.
Thanks
Amit Shekhar
Founder @ Outcome School
You can connect with me on:
Follow Outcome School on:
Read all of our high-quality blogs here.
Subscribe to our newsletter to get our latest AI and Machine Learning blogs straight to your inbox.
