Agentic Design Patterns

Prompt Chaining

Prompt Chaining

The core idea is to break down the original, dainting pronlem into a sequence of smaller, more manageable sub-problems. Each sub-problem is addressed individually through a specifically designed prompt, and the output generated from one prompt is strategically fed as input into the subsequent prompt in the chain. Here are several practical applications and use cases:

  1. Information Processing Worflows: Many taks involve procesing raw information through multiple transformations. For instance, summarizing a document, extracting key entities, and then using those entities to query a database or generate a report. A prompt chain could look like:
  • Prompt 1: Extract text content from a given URL or document.
  • Prompt 2: Summarize the cleaned text.
  • Prompt 3: Extract specific entities (e.g., names, dates, locations) from the summary or original text.
  • Prompt 4: Use the entities to search an internal knowledge base.
  • Prompt 5: Generate a final report incorporating the summary, entities, and search results.
  1. Complex Query Answering: Answering complex questions that require multiple steps of reasoning or information retrieval is a prime use case. For example, "What were the main causes of the stock market crash in 1929, and how did government policy respond?"
  • Prompt 1: Identify the core sub-questions in the user's query (causes of crash, government response).
  • Prompt 2: Research or retrieve information specifically about the causes of the 1929 crash.
  • Prompt 3: Research or retrieve information specifically about the government's policy response to the 1929 stock market crash.
  • Prompt 4: Synthesize the information from steps 2 and 3 into a coherent answer to the original query.
  1. Data Extraction and Transformation: The conversion of unstructured text into a structured format is typically achieved through an iterative process, requiring sequential modifications to improve the accuracy and completeness of the output.
  • Prompt 1: Attempt to extract specific fields (e.g., name, address, amount) from an invoice document.
  • Processing: Check if all required fields were extracted and if they meet format requirements.
  • Prompt 2 (Conditional): If fields are missing or malformed, craft a new prompt asking the model to specifically find the missing/malformed information, perhaps providing context from the failed attempt.
  • Processing: Validate the results again. Repeat if necessary.
  • Output: Provide the extracted, validated structured data.
  1. Content Generation Workflows: The composition of complex content is a procedural task that is typically decomposed into distinct phases, including initial ideation, structural outlining, drafting, and subsequent revision
  • Prompt 1: Generate 5 topic ideas based on a user's general interest.
  • Processing: Allow the user to select one idea or automatically choose the best one.
  • Prompt 2: Based on the selected topic, generate a detailed outline.
  • Prompt 3: Write a draft section based on the first point in the outline.
  • Prompt 4: Write a draft section based on the second point in the outline, providing the previous section for context. Continue this for all outline points.
  • Prompt 5: Review and refine the complete draft for coherence, tone, and grammar.
  1. Conversational Agents with State: Although comprehensive state management architectures employ methods more complex than sequential linking, prompt chaining provides a foundational mechanism for preserving conversational continuity. This technique maintains context by constructing each conversational turn as a new prompt that systematically incorporates information or extracted entities from preceding interactions in the dialogue sequence.
  • Prompt 1: Process User Utterance 1, identify intent and key entities.
  • Processing: Update conversation state with intent and entities.
  • Prompt 2: Based on current state, generate a response and/or identify the next required piece of information.
  • Repeat for subsequent turns, with each new user utterance initiating a chain that leverages the accumulating conversation history (state).
  1. Code Generation and Refinement: The generation of functional code is typically a multi-stage process, requiring a problem to be decomposed into a sequence of discrete logical operations that are executed progressively
  • Prompt 1: Understand the user's request for a code function. Generate pseudocode or an outline.
  • Prompt 2: Write the initial code draft based on the outline.
  • Prompt 3: Identify potential errors or areas for improvement in the code (perhaps using a static analysis tool or another LLM call).
  • Prompt 4: Rewrite or refine the code based on the identified issues.
  • Prompt 5: Add documentation or test cases.
  1. Multimodal and multi-step reasoning: Analyzing datasets with diverse modalities necessitates breaking down the problem into smaller, prompt-based tasks. For example, interpreting an image that contains a picture with embedded text, labels highlighting specific text segments, and tabular data explaining each label, requires such an approach.
  • Prompt 1: Extract and comprehend the text from the user's image request.
  • Prompt 2: Link the extracted image text with its corresponding labels.
  • Prompt 3: Interpret the gathered information using a table to determine the required output.

Summary

What: Complex tasks often overwhelm LLMs when handled within a single prompt, leading to significant performance issues. The cognitive load on the model increases the likelihood of errors such as overlooking instructions, losing context, and generating incorrect information. A monolithic prompt struggles to manage multiple constraints and sequential reasoning steps effectively. This results in unreliable and inaccurate outputs, as the LLM fails to address all facets of the multifaceted request.

Why: Prompt chaining provides a standardized solution by breaking down a complex problem into a sequence of smaller, interconnected sub-tasks. Each step in the chain uses a focused prompt to perform a specific operation, significantly improving reliability and control. The output from one prompt is passed as the input to the next, creating a logical workflow that progressively builds towards the final solution. This modular, divide-and-conquer strategy makes the process more manageable, easier to debug, and allows for the integration of external tools or structured data formats between steps. This pattern is foundational for developing sophisticated, multi-step Agentic systems that can plan, reason, and execute complex workflows.

Rule of thumb: Use this pattern when a task is too complex for a single prompt, involves multiple distinct processing stages, requires interaction with external tools between steps, or when building Agentic systems that need to perform multi-step reasoning and maintain state.

Key Takeaways

  • Prompt Chaining breaks down complex tasks into a sequence of smaller, focused steps. This is occassionally knows as the Pipeline pattern.
  • Each step in a chain involves calling an LLM or processing logic, using the output of the previous step as input.
  • This pattern improves the reliability and manageability of complex interactions with language models.
  • Frameworks like LangChain/LangGraph, and Google ADK provide robust tools to define, manage, and execute these multi-step sequences.

Code Examples

LangChain Implementation

prompt_chaining.py

import os
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from dotenv import load_dotenv
load_dotenv() # Load environment variables from .env file
MODEL_ID = os.getenv("MODEL_ID")
llm = ChatOpenAI(temperature=0, model=MODEL_ID)
# --- Prompt 1: Extract Information ---
prompt_extract = ChatPromptTemplate.from_template(
"Extract the technical specifications from the following text:\n\n{text_input}\n\n"
)
# --- Prompt 2: Transform to JSON ---
prompt_transform = ChatPromptTemplate.from_template(
"Transform the following specifications into a JSON object with 'cpu', 'memory', and 'storage' as keys:\n\n{specifications}"
)
# -- Build the Chain using LCEL --
# The StrOutputParser() converts the LLM's message output to a simple string.
extraction_chain = prompt_extract | llm | StrOutputParser()
# The full chain passes the output of the extraction chain into the 'specifications' variable for the transformation prompt.
full_chain = ({"specifications": extraction_chain} | prompt_transform | llm | StrOutputParser())
# --- Run the Chain ---
input_text = "The new laptop model featuires a 3.5 GHz octa-core processor, 16 GB of RAM, and a 1 TB NVMe SSD."
# Execute the chain with the input text dictionary
final_result = full_chain.invoke({"text_input": input_text})
print("\n--- Final JSON Output ---")
print(final_result)