Home Blog Reviews Best Picks Guides Tools Glossary Advertise Subscribe Free
Tech Frontline Jun 12, 2026 5 min read

How to Use Prompt Chaining to Automate Complex Multi-Step Workflows

Master prompt chaining: The secret to automating sophisticated, multi-step business workflows with AI.

T
Tech Daily Shot Team
Published Jun 12, 2026
How to Use Prompt Chaining to Automate Complex Multi-Step Workflows

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

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.


  1. 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 .env in your project directory and add your OpenAI API key:

    OPENAI_API_KEY=sk-xxxxxx
      

    Load this key in your scripts using Python’s os module or a package like python-dotenv.

    pip install python-dotenv
      
  2. Design Your Multi-Step Workflow

    Clearly define each step in your workflow. For this tutorial, we’ll automate a three-step process:

    1. Extract key points from a long email
    2. Summarize those key points into an action plan
    3. 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.

  3. 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 openai import at the top.

  4. 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.

  5. 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_tokens to avoid excessive responses and reduce costs.
  6. 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.

  7. 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


Next Steps

You’ve now built and tested a practical prompt chaining workflow! To expand your automation:

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.

prompt chaining AI workflow automation tutorial

Related Articles

Tech Frontline
Prompt Engineering for Approval Workflows: Templates & Real-World Examples
Jun 13, 2026
Tech Frontline
Automating Employee Expense Approvals with AI: Workflow Best Practices
Jun 13, 2026
Tech Frontline
Playbook: Building Automated Compliance Workflows for Financial Services
Jun 13, 2026
Tech Frontline
AI Workflow Automation for Legal Case Management: Implementation Guide 2026
Jun 12, 2026
Free & Interactive

Tools & Software

100+ hand-picked tools personally tested by our team — for developers, designers, and power users.

🛠 Dev Tools 🎨 Design 🔒 Security ☁️ Cloud
Explore Tools →
Step by Step

Guides & Playbooks

Complete, actionable guides for every stage — from setup to mastery. No fluff, just results.

📚 Homelab 🔒 Privacy 🐧 Linux ⚙️ DevOps
Browse Guides →
Advertise with Us

Put your brand in front of 10,000+ tech professionals

Native placements that feel like recommendations. Newsletter, articles, banners, and directory features.

✉️
Newsletter
10K+ reach
📰
Articles
SEO evergreen
🖼️
Banners
Site-wide
🎯
Directory
Priority

Stay ahead of the tech curve

Join 10,000+ professionals who start their morning smarter. No spam, no fluff — just the most important tech developments, explained.