prompt chaining

What is prompt chaining?

Prompt chaining is a technique used when working with generative AI models in which the output from one prompt is used as input for the next. This method is a form of prompt engineering, or the practice of eliciting better output from pretrained generative AI models by improving how questions are asked.

Prompt chaining is best suited for either approaching a complicated problem in a piecewise manner or refining and expanding on an initial output. It is an especially helpful strategy for users who might have a task in mind and a general idea of their desired output, but do not yet know what the exact details or structure of that output should be.

Prompt chaining is most commonly used when interacting with large language models (LLMs), as these models are currently best able to retain context and refine previously generated output without making too many changes or removing desirable features. However, developers are working on the ability to iteratively refine the output of other types of generative AI models, such as image generators. For example, OpenAI's Dall-E image generation model, accessible within ChatGPT, offers this capability, albeit to varying degrees of success.

How does prompt chaining work?

The process starts in a manner similar to any other interaction with a generative AI model: by providing the model with an initial prompt, usually a question or a statement describing the desired output. After processing this initial input, the model generates its first output.

That initial output is then evaluated, either by a human user or by an automated system that has been trained to check against criteria such as accuracy and creativity. Based on the results of that evaluation, the user or system creates another prompt that takes into account the feedback from the previous round, aiming to bring the output closer to the user's intent.

For example, the evaluation might determine that the initial output is overly broad and not sufficiently focused on the target problem. In the next prompt, the user could instruct the model to focus on a specific element. The new prompt is fed back into the model, and the process continues until a satisfactory output is achieved.

From a technical perspective, prompt chaining is effective because it takes advantage of certain aspects of LLM architecture. Structurally speaking, LLMs are neural networks that rely heavily on transformer models, which are adept at identifying patterns and relationships in long sequences of text data. Thus, LLMs are well suited for recognizing and replicating complex patterns and maintaining an awareness of context over time.

As described above, prompt chaining involves building on previous output, with each new prompt incrementally adjusting the context or focus. This method is a good fit for LLMs' ability to manage context over extended sequences and also allows for more nuanced refinement compared with giving the LLM a lengthy, detailed initial prompt.

Prompt chaining vs. chain-of-thought prompting

While prompt chaining and chain-of-thought prompting sound similar, and both techniques aim to improve LLM output through prompt engineering, they have several key differences. Prompt chaining involves using output from one model interaction as input for the next, and thus inherently involves multiple prompts. Chain-of-thought prompting, in contrast, uses a single, lengthier prompt, which asks the model to describe the step-by-step reasoning process used to arrive at its answer. In many cases, chain-of-thought prompts also include one or more examples of such step-by-step reasoning as an illustration for the model.

Prompt chaining benefits

Some of the specific benefits of prompt chaining include the following:

  • Flexibility. Prompt chaining breaks down a problem or query into multiple stages, which gives users several opportunities to provide feedback on model output or edit their querying approach. This, in turn, offers better flexibility and customization in LLM output.
  • Creativity. Prompt chaining is useful for open-ended, creative tasks such as brainstorming. With prompt chaining, users can engage in a back-and-forth dialogue that asks the model to expand on particularly promising ideas.
  • Precision. Prompt chaining enables users to gradually adjust their approach and provide feedback on the model's responses at each step. In this way, the technique can elicit more precise, higher-quality responses from LLMs.
  • Efficiency. Handling a less-than-optimal response with chained prompts is often more efficient than restating the entire problem or asking the model to regenerate its response. Rather than asking the model to reprocess the entire task, prompt chaining focuses only on making specific, targeted improvements.
  • Problem solving. Prompt chaining breaks down complicated questions and scenarios into smaller, more manageable components. This makes it a useful technique for approaching complex problems.

For machine learning engineers and data scientists, prompt chaining can also be a useful tool for model training and fine-tuning. Giving models the opportunity to learn from iterative feedback in a prompt chaining context hones their ability to generate higher-quality, more accurate outputs.

Prompt chaining examples and use cases

As noted above, prompt chaining is a technique well suited to use cases involving creativity and complex problem-solving. The following are a few examples:

  • Software development. Developers can use prompt chaining to produce higher-quality code. After generating code with an initial prompt, developers can then chain prompts to optimize it, align it with specific organizational standards or debug it.
  • Product design. Product teams can use prompt chaining to work through the initial stages of product design. For example, a product designer could use an LLM to generate initial design documents, then use chained prompts to refine those documents based on considerations such as technical feasibility and market research.
  • Content creation. Marketing and content teams can use prompt chaining to iteratively generate marketing collateral, such as blog posts, ad copy and social media posts. Chained prompts can help refine a basic draft to match requirements such as brand voice, length and tone specifications.
  • Strategic planning. Executives and business unit leaders can use chained generative AI prompts to assist them with strategic decision support. After producing a general market analysis, subsequent follow-up prompts can dig deeper into specific areas or introduce new relevant information, eventually producing a well-rounded and detailed scenario analysis or prediction.
This was last updated in May 2024

Continue Reading About prompt chaining

Dig Deeper on AI technologies

Business Analytics
Data Management