Prompt chaining is a powerful technique for orchestrating complex, multi-step AI workflows using large language models (LLMs). By connecting outputs from one prompt as inputs to the next, you can automate decision-making, data transformation, and end-to-end business processes. As we covered in our complete guide to AI workflow prompt engineering, prompt chaining is a foundational skill for anyone building robust, scalable AI automations in 2026 and beyond. This hands-on tutorial will take you from zero to a working, testable prompt chaining workflow—complete with code, CLI commands, and troubleshooting tips.
Prerequisites
- Python 3.9+ (tested with Python 3.11)
- OpenAI API key (or compatible LLM provider with OpenAI-compatible API)
- openai Python package (version 1.0.0+)
- Basic familiarity with Python scripting and JSON
- Optional:
dotenvpackage for environment variable management - Optional: Familiarity with workflow orchestration concepts
If you want to explore more advanced prompt chaining strategies, see our in-depth article on advanced multi-step AI workflow techniques.
1. Install Required Tools
-
Set up a Python virtual environment (recommended):
python3 -m venv venv source venv/bin/activate
-
Install the required packages:
pip install openai python-dotenv
-
Get your OpenAI API key from the OpenAI dashboard and export it as an environment variable:
export OPENAI_API_KEY="sk-..."
Or, create a.envfile:OPENAI_API_KEY=sk-...
2. Define Your Workflow & Prompts
Prompt chaining starts with mapping out your workflow. For this tutorial, we'll automate a simple content workflow:
- Summarize a research article.
- Generate three catchy blog post titles based on the summary.
- Draft an intro paragraph for the best title.
Here's the high-level prompt chain:
[Article Text] → [Summarization Prompt] → [Summary] [Summary] → [Title Generation Prompt] → [Titles] [Best Title, Summary] → [Intro Generation Prompt] → [Intro Paragraph]
3. Implement Prompt Chaining in Python
Let's build the workflow step by step. We'll use the OpenAI gpt-3.5-turbo model, but you can substitute any compatible LLM.
-
Set up your Python script:
import os from openai import OpenAI from dotenv import load_dotenv load_dotenv() client = OpenAI(api_key=os.getenv("OPENAI_API_KEY")) -
Define your prompts as functions:
def summarize_article(article_text): prompt = f"Summarize the following research article in 3 sentences:\n\n{article_text}" response = client.chat.completions.create( model="gpt-3.5-turbo", messages=[{"role": "user", "content": prompt}] ) return response.choices[0].message.content.strip() def generate_titles(summary): prompt = f"Based on this summary, suggest 3 catchy blog post titles:\n\n{summary}" response = client.chat.completions.create( model="gpt-3.5-turbo", messages=[{"role": "user", "content": prompt}] ) return response.choices[0].message.content.strip().split('\n') def generate_intro(title, summary): prompt = f"Write an engaging introduction paragraph for a blog post titled '{title}', using this summary:\n\n{summary}" response = client.chat.completions.create( model="gpt-3.5-turbo", messages=[{"role": "user", "content": prompt}] ) return response.choices[0].message.content.strip() -
Chain the prompts together:
def prompt_chain_workflow(article_text): # Step 1: Summarize summary = summarize_article(article_text) print("Summary:", summary) # Step 2: Generate Titles titles = generate_titles(summary) print("Titles:", titles) # Pick the first title (could add user selection or ranking logic) best_title = titles[0].replace("1. ", "").strip() print("Best Title:", best_title) # Step 3: Generate Intro intro = generate_intro(best_title, summary) print("Intro Paragraph:", intro) return { "summary": summary, "titles": titles, "best_title": best_title, "intro": intro } -
Test with sample input:
if __name__ == "__main__": article_text = """ Artificial Intelligence (AI) is transforming industries by automating tasks, improving decision-making, and enabling new products. Recent advances in large language models have accelerated adoption in content creation, customer service, and scientific research. However, challenges remain in ensuring reliability, fairness, and transparency in AI-driven workflows. """ result = prompt_chain_workflow(article_text) print(result)
Screenshot description: Terminal output should display the summary, generated titles, the selected best title, and the AI-generated introduction paragraph.
4. Make Your Workflow Robust
- Validate outputs at each step: Check for empty responses or unexpected formats.
-
Add error handling:
try: summary = summarize_article(article_text) if not summary: raise ValueError("Summary is empty") except Exception as e: print("Error in summarization:", e) - Log intermediate results for debugging and reproducibility.
5. Orchestrate Multi-Step Chains (Optional: Using JSON)
For more advanced workflows, pass structured data (e.g., JSON) between steps. This is especially useful if you plan to integrate with other tools or APIs.
import json
def generate_titles_json(summary):
prompt = (
f"Based on this summary, return a JSON array of 3 catchy blog post titles. "
"Example: [\"Title 1\", \"Title 2\", \"Title 3\"]\n\n"
f"Summary: {summary}"
)
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": prompt}]
)
try:
titles = json.loads(response.choices[0].message.content.strip())
except json.JSONDecodeError:
print("Could not parse JSON, falling back to line split.")
titles = response.choices[0].message.content.strip().split('\n')
return titles
Common Issues & Troubleshooting
- API key errors: Ensure your
OPENAI_API_KEYis set correctly and has sufficient quota. - Empty or malformed outputs: Add checks after each prompt. If the model returns unexpected results, try rephrasing your prompt or adding explicit output instructions.
- Rate limits: The OpenAI API may throttle requests. Add
time.sleep()between requests or implement retry logic. - Prompt injection or hallucination: If the model outputs irrelevant or unsafe content, use more specific instructions and consider using
systemmessages for guardrails. - JSON parsing errors: Always validate and sanitize AI outputs before using them in downstream steps.
For more on avoiding common pitfalls, check out Prompt Engineering Mistakes That Are Killing Your AI Workflow Performance in 2026.
Next Steps
- Expand your workflow: Add more steps, use external APIs, or integrate with workflow tools like Zapier or Airflow.
- Explore advanced chaining: See Prompt Chaining Tactics: Building Reliable Multi-Stage AI Workflows for best practices.
- Test and optimize: Learn how to systematically test your prompt chains with AI prompt testing frameworks.
- Build your own prompt library: Browse the Ultimate Prompt Library for AI Workflow Automation for inspiration.
Prompt chaining unlocks the full power of LLMs for workflow automation. By chaining prompts thoughtfully, validating outputs, and handling errors, you can build reliable, production-ready AI automations. For a broader view of this field and more frameworks, revisit our Mastering AI Workflow Prompt Engineering in 2026 pillar guide.