Prompt chaining is rapidly becoming a cornerstone technique for orchestrating advanced, multi-step AI workflows—especially in document automation, data extraction, and approval pipelines. In this tutorial, you’ll learn how to design, implement, and troubleshoot robust prompt chaining systems using 2026’s best practices, with hands-on code and actionable examples.
For broader context on how prompt chaining fits into the modern AI automation landscape, see our Pillar: The 2026 Guide to Automating Complex Document Workflows with AI—Best Practices, Tools & Use Cases.
Prerequisites
- Python 3.10+ (all code examples use Python)
- OpenAI Python SDK (v1.5+), or compatible LLM API client (e.g., Anthropic, Cohere, Google Gemini)
- Basic knowledge of prompt engineering (see: Prompt Engineering for Automated Document Workflows: 2026’s Most Effective Prompts)
- Familiarity with API keys and environment variables
- Unix-like terminal (macOS, Linux, or WSL on Windows)
- Optional:
langchain(v0.1.0+),dotenvfor env management
Step 1. Understand the Core Concept of Prompt Chaining
-
Prompt chaining is the process of connecting multiple LLM prompts so that the output of one prompt becomes the input for the next. This enables you to:
- Break down complex tasks (e.g., document extraction, summarization, approval) into manageable steps
- Improve accuracy by validating or refining intermediate outputs
- Handle conditional logic and dynamic workflows
-
For example, in an automated invoice workflow:
- Step 1: Extract key invoice details (date, vendor, amount)
- Step 2: Summarize findings and flag anomalies
- Step 3: Generate approval/rejection messages based on findings
- For more on multi-step document review, see How to Set Up Automated Multi-Step Document Review Workflows with AI (2026 Tutorial).
Step 2. Set Up Your Environment
-
Install required packages:
python3 -m venv venv source venv/bin/activate pip install openai python-dotenv langchainScreenshot description: A terminal window showing successful installation of
openai,python-dotenv, andlangchain. -
Add your API key to a
.envfile in your project root:OPENAI_API_KEY=sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx -
Load your environment variables in Python:
from dotenv import load_dotenv load_dotenv()
Step 3. Design a Prompt Chain for a Real-World Use Case
-
Identify your workflow. Let’s use an invoice processing pipeline as an example:
- Extract fields (vendor, date, amount) from unstructured text
- Validate extracted data (e.g., check if amount is positive)
- Generate a summary and approval recommendation
-
Draft prompts for each stage:
extract_prompt = """ Extract the following fields from the invoice text: - Vendor Name - Invoice Date - Amount Invoice Text: {invoice_text} Return as JSON. """ validate_prompt = """ Check the extracted data for anomalies: - Is the amount a positive number? - Is the date in the past? - Is the vendor name non-empty? Extracted Data: {extracted_json} Return a JSON object with 'valid': true/false and 'issues': [list of issues]. """ approve_prompt = """ Given the validation result, should this invoice be approved? Explain your reasoning. Validation Result: {validation_json} Reply in JSON: {'approve': true/false, 'reason': '...'} """ - Screenshot description: Three prompt templates displayed side-by-side in a code editor.
Step 4. Implement the Prompt Chain in Python
-
Define a helper function to call your LLM:
import os import openai def call_llm(prompt, model="gpt-4-turbo"): response = openai.ChatCompletion.create( model=model, messages=[{"role": "user", "content": prompt}], temperature=0 ) return response.choices[0].message['content'].strip() -
Build the prompt chain:
import json invoice_text = """ Invoice #1234 Vendor: Acme Corp Date: 2026-04-18 Total Due: $1,250.00 """ extract_filled = extract_prompt.format(invoice_text=invoice_text) extracted_json = call_llm(extract_filled) print("Extracted Data:", extracted_json) validate_filled = validate_prompt.format(extracted_json=extracted_json) validation_json = call_llm(validate_filled) print("Validation Result:", validation_json) approve_filled = approve_prompt.format(validation_json=validation_json) approval_json = call_llm(approve_filled) print("Approval Decision:", approval_json)Screenshot description: Terminal output showing each stage’s JSON result: extracted fields, validation, and approval decision.
-
Parse and use results programmatically:
try: extracted = json.loads(extracted_json) validation = json.loads(validation_json) approval = json.loads(approval_json) except json.JSONDecodeError: print("Error parsing JSON from LLM output.")
Step 5. Add Conditional Logic & Dynamic Branching
- Often, you’ll want to branch your workflow based on intermediate results. For example, if validation fails, skip approval and trigger a manual review.
-
Example:
if validation.get('valid'): approval_filled = approve_prompt.format(validation_json=validation_json) approval_json = call_llm(approval_filled) print("Approval:", approval_json) else: print("Manual review required. Issues:", validation.get('issues'))Screenshot description: Terminal output showing a “Manual review required” message when validation fails.
- For more advanced branching and workflow automation, see Prompt Engineering for Multi-Step Automated Data Pipelines: Strategies for Accuracy and Speed.
Step 6. Refine and Chain Prompts for Greater Accuracy
- Iteratively improve prompts based on observed outputs. Add explicit format instructions, examples, or error handling steps.
-
Example: Add a formatting checker step to ensure LLM outputs valid JSON.
format_checker_prompt = """ Does the following string contain valid JSON? If not, correct it. String: {llm_output} Reply with corrected JSON only. """ def ensure_json(llm_output): checker_filled = format_checker_prompt.format(llm_output=llm_output) return call_llm(checker_filled) -
Chain this checker after each LLM call:
extracted_json = ensure_json(extracted_json) validation_json = ensure_json(validation_json) approval_json = ensure_json(approval_json) - For prompt templates and chaining best practices, see Prompt Engineering for Document AI: Real-World Templates for Approval and Extraction.
Step 7. Integrate with Workflow Automation Tools
- For production-grade systems, integrate your prompt chains with workflow tools like Apache Airflow, Zapier, or custom orchestrators.
-
Example: Using LangChain’s SequentialChain
from langchain.chains import SequentialChain, LLMChain from langchain.prompts import PromptTemplate from langchain.llms import OpenAI llm = OpenAI(model_name="gpt-4-turbo") extract_chain = LLMChain( llm=llm, prompt=PromptTemplate(input_variables=["invoice_text"], template=extract_prompt) ) validate_chain = LLMChain( llm=llm, prompt=PromptTemplate(input_variables=["extracted_json"], template=validate_prompt) ) approve_chain = LLMChain( llm=llm, prompt=PromptTemplate(input_variables=["validation_json"], template=approve_prompt) ) overall_chain = SequentialChain( chains=[extract_chain, validate_chain, approve_chain], input_variables=["invoice_text"], output_variables=["extracted_json", "validation_json", "approval_json"] ) result = overall_chain({"invoice_text": invoice_text}) print(result) - Screenshot description: Output in the terminal showing the full dictionary result from the chained workflow.
- For more on automating document-centric pipelines, check out How to Automate Invoice Processing with Document AI Workflow Tools in 2026.
Common Issues & Troubleshooting
-
LLM outputs invalid JSON: Use a format checker as in Step 6, or add explicit instructions (e.g., “Return JSON only, no text before or after”). If using LangChain, set
output_parserto enforce JSON. - Hallucinated or missing fields: Add more examples to your prompts, or use few-shot prompting. Specify required fields explicitly.
- API rate limits or timeouts: Implement retries with exponential backoff. Check your API quota.
- Chaining logic errors: Log all intermediate outputs. Validate JSON schema before passing to next step.
- Security/privacy concerns: Mask sensitive data before sending to LLMs. For compliance, see Data Privacy by Design: Building Secure AI-Driven Document Workflows in 2026.
Next Steps
- Experiment with more complex branching, such as multi-document or multi-user approval flows.
- Integrate external data sources (e.g., databases, APIs) between prompt steps.
- Explore advanced prompt engineering—see 10 Advanced Prompts for Document AI Workflow Automation in 2026.
- For a complete playbook on automating document workflows, revisit our Pillar: The 2026 Guide to Automating Complex Document Workflows with AI.
- For related AI workflow automation in other domains, see How to Use AI Workflow Automation for Dynamic Pricing in E-commerce—2026 Guide.
By mastering prompt chaining, you unlock the full power of LLMs for orchestrating reliable, auditable, and scalable AI workflows. With these techniques, you’re ready to build the next generation of document automation, data extraction, and approval systems for 2026 and beyond.