Prompt chaining lets you break down complex tasks into a series of smaller, manageable LLM calls, each building on the output of the previous one.
Let’s see it in action. Imagine we want to extract key information from a product review and then use that information to generate a concise summary for a marketing email.
Here’s a Python example using a hypothetical LLM API:
import requests
import json
LLM_API_URL = "https://api.example-llm.com/v1/completions"
API_KEY = "YOUR_API_KEY" # Replace with your actual API key
def call_llm(prompt, max_tokens=150):
headers = {
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json"
}
data = {
"prompt": prompt,
"max_tokens": max_tokens,
"temperature": 0.7,
"stop": None
}
response = requests.post(LLM_API_URL, headers=headers, data=json.dumps(data))
response.raise_for_status() # Raise an exception for bad status codes
return response.json()['choices'][0]['text'].strip()
# --- Step 1: Extract key information ---
review_text = """
I recently purchased the new XYZ Smartwatch and I'm mostly impressed. The battery life is phenomenal, lasting easily two full days with heavy use. The display is crisp and vibrant, making it easy to read even in direct sunlight. However, I found the companion app to be quite clunky and difficult to navigate. Setting up custom watch faces was a frustrating experience, and the sync process occasionally dropped connections. The fitness tracking features, though, are spot on, accurately measuring my steps and heart rate. For the price, it's a solid device, but the software definitely needs some refinement.
"""
extraction_prompt = f"""
Extract the following information from the product review below:
- Product Name
- Positive Features (list them)
- Negative Features (list them)
- Overall Sentiment (e.g., Positive, Negative, Mixed)
Product Review:
"{review_text}"
Extracted Information:
"""
extracted_info = call_llm(extraction_prompt)
print("--- Extracted Information ---")
print(extracted_info)
# --- Step 2: Generate a marketing summary ---
summary_prompt = f"""
Using the extracted information below, write a concise and compelling marketing summary for an email. Highlight the key benefits and briefly acknowledge areas for improvement without dwelling on them.
Extracted Information:
{extracted_info}
Marketing Summary:
"""
marketing_summary = call_llm(summary_prompt, max_tokens=100)
print("\n--- Marketing Summary ---")
print(marketing_summary)
This process breaks down the task. The first LLM call acts as an information extractor, identifying the core components of the review. The second LLM call then takes this structured, extracted data and transforms it into a marketing-friendly summary.
The real power of prompt chaining comes from how you structure the intermediate steps. Each step is essentially a mini-LLM task, and the output of one becomes the input for the next. This allows you to build sophisticated workflows that can handle tasks like:
- Sentiment Analysis + Topic Modeling: First, determine the overall sentiment of a batch of customer feedback. Then, for negative feedback, identify the main topics being discussed.
- Data Extraction + Table Generation: Pull specific entities from unstructured text and organize them into a structured table.
- Question Answering + Content Generation: Answer a user’s question based on a document, then use that answer to draft an email response.
The key levers you control are:
- Prompt Design: The clarity and specificity of each prompt are paramount. You need to guide the LLM precisely on what to do at each stage.
- Output Formatting: Standardizing the output of intermediate steps (e.g., JSON, bullet points) makes it easier for subsequent prompts to parse and use the information reliably.
- Parameter Tuning: Adjusting
temperature,max_tokens, andstopsequences for each call can optimize the output for its specific task.
When you define an intermediate prompt, you’re not just asking the LLM to "summarize this." You’re creating a specific, bounded problem for it to solve within the larger workflow. This bounded problem-solving is what makes chaining effective; it prevents the LLM from getting lost in the complexity of the entire task at once, instead focusing its attention and computational resources on a single, well-defined sub-problem. The output of each step acts as a highly curated context for the next, effectively funneling the LLM’s "attention" through a series of logical transformations.
The next challenge in prompt chaining is handling errors and ensuring robustness across multiple LLM calls.