Prompt Chaining: Building Step-by-Step AI Reasoning

Introduction

In the evolving landscape of AI, achieving accurate and coherent outputs often requires guiding models through structured reasoning processes. Prompt chaining emerges as a pivotal technique in this context, enabling complex tasks to be broken down into manageable steps, each handled by the AI in sequence.

What is Prompt Chaining?

Prompt chaining involves decomposing a complex task into a series of interconnected prompts. Each prompt addresses a specific subtask, and the output from one serves as the input for the next. Out of all the existing Agentic AI workflows (which we'll take a look later on), this one is the simplest one.

Why Use Prompt Chaining?

  • Enhanced Accuracy: By focusing on one subtask at a time, the AI can provide more precise outputs.
  • Improved Interpretability: Each step's output can be examined, making it easier to understand the AI's reasoning.
  • Modularity: Subtasks can be modified or replaced independently, offering flexibility in workflow design.

Implementing Prompt Chaining

Let's take a look at an example implementation using Node.js and Gemini.

  • Task Decomposition: Identify the main task and divide it into smaller, logical subtasks.
  • Prompt Design: Craft prompts tailored to each subtask, ensuring clarity and specificity.
  • Sequential Execution: Run each prompt in order, feeding the output of one as the input to the next.
  • Validation: At each stage, assess the output for correctness before proceeding. (Optional)

Practical Example: Generating a social media post based on a press release

Let's create a new project and install some dependencies:

npm init -y && npm pkg set type="module"
npm i @google/genai cheerio

For the purposes of this example you need to have access to a Large Language Model. In my case I opted in to use Google's Gemini. You need to make sure that you have an API key for your model, and that you have some credit uploaded to the model's provider for token usage.

Ideally you should create a .env file and add your API key there.

Add the following to package.json:

"start": "node --env-file=.env --experimental-strip-types --watch --no-warnings=ExperimentalWarning app.ts"

And also please create app.ts.

The first thing we'll do is to add a function to extract information from the press release. This is the sample press release that we'll be using.

async function fetchAndExtractTextFromUrl(url: string): Promise<string> {
const res = await fetch(url);
const html = await res.text();
const $ = cheerio.load(html);

const title = $('h1.entry-title').text().trim();
const subtitle = $('h2.subtitle, .entry-subtitle').first().text().trim();
const body = $('section.entry-content[itemprop="articleBody"]').text().trim();

return `${title}\n\n${subtitle}\n\n${body}`
.replace(/\s+/g, ' ')
.slice(0, 4000);
}

Next we can look at the main run function:

async function run() {
if (!process.env.GEMINI_API_KEY) {
console.error(
'GEMINI_API_KEY environment variable not set. Please set it before running the script.'
);
return;
}

const url = '...';

const originalText = await fetchAndExtractTextFromUrl(url);

const prompt1 = `Summarise the following press release in two sentences: ${originalText}`;
try {
console.log('Sending prompt 1 (Summarise):', prompt1, '\n\n');
const result1 = await ai.models.generateContent({
model: 'gemini-2.0-flash',
contents: prompt1,
});

const summary = result1.text;
console.log('Summary:', summary, '\n\n');

const prompt2 = `Translate the following summary into Spanish, only return the translation, no other text: ${summary}`;

console.log('Sending prompt 2 (Translate):', prompt2, '\n\n');
const result2 = await ai.models.generateContent({
model: 'gemini-2.0-flash',
contents: prompt2,
});
const translation = result2.text;
console.log('Translation:', translation);
} catch (error) {
console.error('An error occurred:', error);
}
}

As you can see we ask the LLM to do the first task, and feed the result of that to the second prompt hence achieving the desired outcome of prompt chaining.

Check out the sample output below.

Summary: ZENITH has released a new version of its CHRONOMASTER Original Triple Calendar chronograph, now featuring an 18-carat rose gold case with a striking black dial. This edition blends the elegance of precious metal with the functionality of a triple calendar and moon phase display, powered by the El Primero 3610 calibre with 1/10th of a second accuracy.
ZENITH ha lanzado una nueva versión de su cronógrafo CHRONOMASTER Original Triple Calendar, ahora con una caja de oro rosa de 18 quilates y una llamativa esfera negra. Esta edición combina la elegancia del metal precioso con la funcionalidad de un calendario triple y una indicación de las fases lunares, impulsado por el calibre El Primero 3610 con una precisión de 1/10 de segundo.

We could now further enhance this by adding another step in the chain and ask the LLM to generate 2-3 relevant hashtags.

Challenges and Considerations

  • Error Propagation: Mistakes in early prompts can affect subsequent outputs. Implement validation steps to mitigate this.
  • Complexity Management: As chains grow longer, maintaining clarity becomes crucial. Document each step meticulously.
  • Performance: Long chains may increase processing time. Optimize prompts for efficiency.

Conclusion

Prompt chaining stands as a foundational technique in constructing agentic AI workflows. By guiding models through structured, step-by-step processes, we can harness their capabilities more effectively, leading to outputs that are not only accurate but also interpretable and adaptable.