Prompt chaining is transforming how developers automate complex, multi-step workflows using AI. By linking the outputs of one prompt directly into the inputs of the next, you can orchestrate sophisticated processes—like document processing, decision support, or data extraction—across multiple stages with minimal manual intervention. This tutorial walks you through a practical, code-centric approach to building robust prompt chains, using Python and OpenAI's GPT models as a concrete example.
For a broader look at the methodology, see Prompt Chaining in Automated Workflows: Best Practices for 2026.
Prerequisites
- Python (3.8+) installed on your system
- OpenAI Python SDK (v1.0+)
- Basic knowledge of Python scripting
- OpenAI API key (sign up at OpenAI Platform)
- Familiarity with JSON and REST APIs (helpful but not required)
If you’re new to AI workflow automation, you may also find value in our related guides such as How to Automate Healthcare Claims Adjudication with AI Workflows and How to Build an Automated AI Workflow for Invoice Matching and Payment in 2026.
-
Install Required Tools and Set Up Your Environment
Start by setting up your Python environment and installing the OpenAI SDK.
python3 -m venv prompt-chaining-env source prompt-chaining-env/bin/activate pip install openai==1.3.7
Create a file named
.envin your project directory and add your OpenAI API key:OPENAI_API_KEY=sk-xxxxxx
Load this key in your scripts using Python’s
osmodule or a package likepython-dotenv.pip install python-dotenv
-
Design Your Multi-Step Workflow
Clearly define each step in your workflow. For this tutorial, we’ll automate a three-step process:
- Extract key points from a long email
- Summarize those key points into an action plan
- Generate a follow-up email draft based on the action plan
This modular approach is at the heart of prompt chaining. Each step’s output feeds directly into the next.
Tip: For more complex pipelines, diagram your steps and data flow before coding. For advanced strategies, see Prompt Engineering for Multi-Step Automated Data Pipelines: Strategies for Accuracy and Speed.
-
Write Modular Prompt Functions in Python
Each workflow stage is encapsulated as a Python function. Here’s how you can structure them:
import os from openai import OpenAI from dotenv import load_dotenv load_dotenv() client = OpenAI(api_key=os.getenv("OPENAI_API_KEY")) def extract_key_points(email_text): prompt = f"Extract the 5 most important key points from the following email:\n\n{email_text}\n\nKey Points:" response = client.chat.completions.create( model="gpt-3.5-turbo", messages=[{"role": "user", "content": prompt}], max_tokens=300 ) return response.choices[0].message.content.strip() def summarize_action_plan(key_points): prompt = f"Given these key points, create a concise action plan:\n\n{key_points}\n\nAction Plan:" response = client.chat.completions.create( model="gpt-3.5-turbo", messages=[{"role": "user", "content": prompt}], max_tokens=200 ) return response.choices[0].message.content.strip() def generate_followup_email(action_plan): prompt = f"Draft a professional follow-up email based on this action plan:\n\n{action_plan}\n\nEmail:" response = client.chat.completions.create( model="gpt-3.5-turbo", messages=[{"role": "user", "content": prompt}], max_tokens=300 ) return response.choices[0].message.content.strip()Screenshot description: Screenshot shows a VSCode window with the above three Python functions, each clearly separated, and the
openaiimport at the top. -
Chain the Prompts Together
Now, orchestrate these functions so each step’s output becomes the next step’s input.
def process_email_workflow(email_text): key_points = extract_key_points(email_text) print("Key Points Extracted:\n", key_points) action_plan = summarize_action_plan(key_points) print("\nAction Plan:\n", action_plan) followup_email = generate_followup_email(action_plan) print("\nGenerated Follow-Up Email:\n", followup_email) return followup_email if __name__ == "__main__": long_email = """ Hi team, We need to finalize the Q3 marketing strategy by next Friday. Please review the attached draft and provide feedback. Also, coordinate with the design team for new assets. Budget approvals are pending; finance will update us soon. Let's schedule a call to align all departments. Thanks! """ process_email_workflow(long_email)Screenshot description: Terminal output shows "Key Points Extracted", "Action Plan", and "Generated Follow-Up Email" sections, each with relevant text.
-
Test and Refine Each Step
Test each function individually with sample inputs before chaining. This helps isolate issues and ensures each prompt is well-tuned.
python your_script.py
Best Practices:
- Use
print()statements to inspect each intermediate output. - Tweak your prompts for clarity and specificity; ambiguous prompts yield inconsistent results.
- Limit
max_tokensto avoid excessive responses and reduce costs.
- Use
-
Handle Errors and Add Logging
Add error handling to make your workflow robust. For example:
import logging logging.basicConfig(level=logging.INFO) def safe_extract_key_points(email_text): try: return extract_key_points(email_text) except Exception as e: logging.error(f"Error extracting key points: {e}") return "Error: Could not extract key points."For production, consider retry logic and more granular logging.
-
Automate Workflow Execution (Optional: CLI or Scheduler)
To automate your workflow, turn your script into a CLI tool or schedule it with
cron:0 9 * * 1-5 /path/to/prompt-chaining-env/bin/python /path/to/your_script.py
Or, accept input files via CLI arguments:
import sys if __name__ == "__main__": if len(sys.argv) > 1: with open(sys.argv[1], 'r') as f: email_text = f.read() process_email_workflow(email_text) else: print("Usage: python your_script.py")
Common Issues & Troubleshooting
-
OpenAI API errors: Ensure your API key is loaded and valid. Check for typos in your
.envfile. - Prompt outputs are inconsistent or incomplete: Tweak your prompts for specificity. Add example outputs in your prompt to guide the model.
-
Rate limiting or quota exceeded: Review your OpenAI account usage and adjust
max_tokensas needed. - Chained outputs are not formatted as expected: Use explicit formatting instructions in your prompts (e.g., "Respond in bullet points").
- Script crashes on empty or malformed input: Add input validation and error handling before processing.
Next Steps
You’ve now built and tested a practical prompt chaining workflow! To expand your automation:
- Integrate with APIs (e.g., email, CRM, or ticketing systems) for end-to-end automation.
- Explore advanced prompt engineering to minimize hallucinations and boost accuracy.
- Scale up by parallelizing chains or using orchestration tools like Airflow or Prefect.
- Read our parent pillar on prompt chaining best practices for deeper strategies and real-world case studies.
For more domain-specific AI workflow automation, see our guides on healthcare claims adjudication and invoice matching and payment.
Prompt chaining empowers you to automate and orchestrate complex tasks with AI—unlocking new efficiencies and possibilities in your workflows.