In today’s AI-driven world, orchestrating complex tasks often requires more than a single prompt. Prompt chaining—the art of linking multiple AI prompts into a coordinated, multi-step workflow—has become a cornerstone of advanced AI automation. Whether you’re building robust data pipelines, automating business processes, or crafting intelligent agents, mastering prompt chaining unlocks new levels of capability and control.
As we covered in our complete guide to mastering AI workflow prompt engineering in 2026, this area deserves a deeper look. In this tutorial, we’ll dive into the nuts and bolts of advanced prompt chaining, showing you how to design, implement, and debug multi-step AI workflows using the latest tools and best practices.
Prerequisites
- Python 3.10+ (All code samples use Python 3.10 or newer syntax)
- LangChain 0.1.0+ (for workflow orchestration)
- OpenAI API key (or another LLM provider—OpenRouter, Anthropic, etc.)
- Basic knowledge of prompt engineering (see our parent pillar article for a primer)
- Familiarity with Python virtual environments
- Optional: Familiarity with JSON, REST APIs, and CLI tools
1. Setting Up Your Development Environment
-
Create and activate a new Python virtual environment:
python3 -m venv prompt-chaining-env source prompt-chaining-env/bin/activate
-
Install required libraries:
pip install langchain openai python-dotenv
-
Set your OpenAI API key (or your LLM provider’s key):
- Create a file named
.envin your project directory:
echo "OPENAI_API_KEY=sk-..." > .env
- Replace
sk-...with your actual API key.
- Create a file named
-
Verify installation:
python -c "import langchain; import openai; print('All good!')"
Screenshot description: Terminal window showing successful activation of virtual environment and successful output from the verification command.
2. Designing a Multi-Step Prompt Chain
The power of prompt chaining lies in breaking down a complex task into clear, modular steps. Let’s walk through a real-world scenario:
Example Use Case: Given a news article URL, extract the main points, summarize them for a 12-year-old, and generate three follow-up questions for discussion.
-
Step 1: Define the workflow steps
- Fetch article text from URL
- Extract main points
- Summarize for a young audience
- Generate discussion questions
-
Step 2: Draft prompts for each step
-
Extract Points Prompt:"You are an expert editor. Given this article, list its 5 main points as bullet points." -
Summarize Prompt:"You are a teacher. Summarize these points for a 12-year-old in simple language." -
Question Generation Prompt:"Based on this summary, write three discussion questions to encourage critical thinking."
-
-
Step 3: Map data flow between steps
- Output from each step becomes input for the next.
Screenshot description: Diagram showing arrows from "Article Text" → "Main Points" → "Summary" → "Questions".
3. Implementing Prompt Chaining in Python with LangChain
-
Load environment variables:
from dotenv import load_dotenv load_dotenv() -
Set up the LLM and prompts:
from langchain.llms import OpenAI from langchain.prompts import PromptTemplate llm = OpenAI(temperature=0.3, model_name="gpt-4") extract_points_prompt = PromptTemplate( input_variables=["article_text"], template="You are an expert editor. Given this article, list its 5 main points as bullet points:\n\n{article_text}" ) summarize_prompt = PromptTemplate( input_variables=["main_points"], template="You are a teacher. Summarize these points for a 12-year-old in simple language:\n\n{main_points}" ) questions_prompt = PromptTemplate( input_variables=["summary"], template="Based on this summary, write three discussion questions to encourage critical thinking:\n\n{summary}" ) -
Build the chained workflow:
from langchain.chains import LLMChain, SequentialChain extract_points_chain = LLMChain(llm=llm, prompt=extract_points_prompt, output_key="main_points") summarize_chain = LLMChain(llm=llm, prompt=summarize_prompt, output_key="summary") questions_chain = LLMChain(llm=llm, prompt=questions_prompt, output_key="questions") workflow = SequentialChain( chains=[extract_points_chain, summarize_chain, questions_chain], input_variables=["article_text"], output_variables=["main_points", "summary", "questions"], verbose=True, ) -
Fetch article text (simple example using
requests):import requests from bs4 import BeautifulSoup def fetch_article_text(url): resp = requests.get(url) soup = BeautifulSoup(resp.text, 'html.parser') paragraphs = soup.find_all('p') return "\n".join([p.get_text() for p in paragraphs if len(p.get_text()) > 50])[:4000] # Limit for LLM input article_url = "https://www.example.com/news/article" article_text = fetch_article_text(article_url) -
Execute the workflow:
result = workflow({"article_text": article_text}) print("Main Points:\n", result["main_points"]) print("\nSummary:\n", result["summary"]) print("\nDiscussion Questions:\n", result["questions"])
Screenshot description: Console showing the main points, summary, and discussion questions for a given article URL.
4. Advanced Techniques: Dynamic Branching & Memory
In 2026, advanced prompt chaining often involves dynamic branching (where the next step depends on prior outputs) and short-term memory (passing context between steps). Let’s enhance our workflow to branch based on article sentiment:
-
Add a sentiment analysis step:
sentiment_prompt = PromptTemplate( input_variables=["article_text"], template="What is the sentiment of this article? Respond with 'positive', 'neutral', or 'negative'.\n\n{article_text}" ) sentiment_chain = LLMChain(llm=llm, prompt=sentiment_prompt, output_key="sentiment") -
Branch workflow logic in Python:
sentiment_result = sentiment_chain({"article_text": article_text}) if "negative" in sentiment_result["sentiment"].lower(): followup_prompt = PromptTemplate( input_variables=["summary"], template="The article is negative. Suggest two ways to address the issues mentioned in this summary:\n\n{summary}" ) followup_chain = LLMChain(llm=llm, prompt=followup_prompt, output_key="followups") # Run the rest of the workflow main_results = workflow({"article_text": article_text}) followups = followup_chain({"summary": main_results["summary"]}) print("Suggested Actions:\n", followups["followups"]) else: main_results = workflow({"article_text": article_text}) print("No further action needed.")
Screenshot description: Terminal output showing suggested actions only when the article sentiment is negative.
5. Orchestrating Multi-Step AI Workflows via CLI
For production use, you’ll want to run prompt chains from the command line or a CI/CD pipeline.
-
Create a CLI entry point:
import argparse def main(): parser = argparse.ArgumentParser(description="Run prompt chaining workflow.") parser.add_argument("url", help="URL of the article") args = parser.parse_args() article_text = fetch_article_text(args.url) result = workflow({"article_text": article_text}) print(result) if __name__ == "__main__": main() -
Run your workflow from the terminal:
python cli_chain.py "https://www.example.com/news/article"
Screenshot description: CLI output showing results of the workflow for a user-supplied URL.
Common Issues & Troubleshooting
-
Issue:
ModuleNotFoundError: No module named 'langchain'
Solution: Make sure you are in your virtual environment and installed all dependencies:source prompt-chaining-env/bin/activate pip install langchain openai python-dotenv
-
Issue:
openai.error.AuthenticationError: No API key provided.
Solution: Check your.envfile and ensure it is loaded. Try printingos.getenv("OPENAI_API_KEY")in your script. -
Issue: LLM output is garbled or missing expected structure.
Solution: Refine your prompt templates to be more explicit about desired output format (e.g., “Respond with a numbered list”). -
Issue:
requests.exceptions.RequestExceptionwhen fetching articles.
Solution: Check that the URL is correct and the site allows scraping. For production, use robust APIs or scraping libraries. -
Issue: Hitting LLM token limits.
Solution: Truncate article text before passing to the LLM. Use summarization or chunking for long content.
Next Steps
You’ve now mastered the essentials and advanced techniques of prompt chaining for multi-step AI workflows in 2026. To deepen your skills, explore:
- How to Use Prompt Chaining to Automate Complex Multi-Step Workflows for automation patterns and more examples.
- How to Automate Complex Multi-Step Workflows Using LLM Plugins in 2026 for plugin-based orchestration.
- Prompt Chaining for Workflow Automation: Best Patterns and Real-World Examples (2026) for deeper architectural insights.
- Return to our parent pillar on mastering AI workflow prompt engineering for frameworks, best practices, and more.
Experiment with chaining more steps, integrating APIs, or adding user feedback loops. The future of AI workflow automation is modular, composable, and—thanks to prompt chaining—limited only by your imagination.