Prompt chaining has rapidly evolved from simple, linear automation to sophisticated, context-aware workflows capable of dynamic reasoning, decision-making, and multi-modal processing. In this tutorial, we’ll walk you through building an advanced prompt chaining AI workflow that maintains context across steps, adapts to user input, and integrates with external APIs.
If you’re new to prompt chaining or want a no-code approach, check out our parent pillar article on building prompt chaining workflows with no-code AI platforms. This guide, however, is for developers ready to tackle code-driven, context-rich automation.
Prerequisites
- Python 3.10+ (tested with 3.11)
- OpenAI API access (or Azure OpenAI, or compatible LLM endpoint)
- LangChain (v0.1.14 or later)
- Basic Python scripting skills
- Familiarity with REST APIs
- Optional: VS Code or similar IDE for editing and running scripts
You should also have an OPENAI_API_KEY (or equivalent credentials) ready in your environment.
1. Set Up Your Development Environment
-
Create and activate a virtual environment:
python3 -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install required packages:
pip install langchain openai python-dotenv requests
-
Set your API key in a
.envfile:
OPENAI_API_KEY=sk-...(Replace
sk-...with your actual key. Never commit your API key to version control.) -
Verify installation:
python -c "import langchain; import openai; print('All good!')"
Screenshot description: VS Code terminal showing successful installation and All good! message.
2. Understand Advanced Prompt Chaining Concepts
Before diving in, let’s clarify what sets advanced prompt chaining apart:
- Context propagation: Carrying state, variables, or user data across multiple prompt steps.
- Branching logic: Conditional flows based on LLM output or external data.
- API integration: Merging LLM reasoning with live data from external services.
- Multi-modal support: (Optional) Handling text, images, or structured data in a single workflow.
For a broader look at the evolution of prompt chaining, see The Future of Prompt Chaining: Will Multi-Step Prompts Rule 2027 Workflows?
3. Design the Workflow: Context-Aware Research Assistant
We’ll build a workflow that:
- Accepts a user query (e.g., “Summarize the latest AI trends in healthcare”).
- Has the LLM generate a list of relevant subtopics.
- For each subtopic, fetches up-to-date article headlines from an external API (e.g., NewsAPI.org).
- Chains the context (original query + subtopics + articles) for a final summary prompt.
This pattern is common in advanced automation, such as automating complex multi-step workflows with prompt chaining.
4. Implement Step 1: User Query and Subtopic Extraction
-
Create
main.pyand load environment variables:import os from dotenv import load_dotenv load_dotenv() OPENAI_API_KEY = os.getenv("OPENAI_API_KEY") -
Set up LangChain and OpenAI LLM:
from langchain.chat_models import ChatOpenAI llm = ChatOpenAI( openai_api_key=OPENAI_API_KEY, model_name="gpt-3.5-turbo", # Or "gpt-4" if available temperature=0.2 ) -
Prompt the user for a query:
user_query = input("Enter your research topic: ") -
Construct and run the subtopic extraction prompt:
from langchain.prompts import ChatPromptTemplate subtopic_prompt = ChatPromptTemplate.from_messages([ ("system", "You are an expert research assistant."), ("human", "Given the query: '{query}', list 3-5 relevant subtopics to explore.") ]) subtopic_chain = subtopic_prompt | llm subtopics = subtopic_chain.invoke({"query": user_query}).content print("Subtopics:\n", subtopics)Screenshot description: Terminal input for research topic, then printed subtopics list.
5. Implement Step 2: Fetch Live Data for Subtopics
We’ll use the free NewsAPI.org for demonstration. Sign up and get an API key.
-
Add your NewsAPI key to
.env:NEWSAPI_KEY=your_newsapi_key_here -
Write a helper to fetch headlines for each subtopic:
import requests NEWSAPI_KEY = os.getenv("NEWSAPI_KEY") def fetch_headlines(topic, max_results=3): url = "https://newsapi.org/v2/everything" params = { "q": topic, "apiKey": NEWSAPI_KEY, "pageSize": max_results, "sortBy": "relevancy", "language": "en" } resp = requests.get(url, params=params) if resp.status_code != 200: print(f"Error fetching news for {topic}: {resp.text}") return [] articles = resp.json().get("articles", []) return [a["title"] for a in articles] -
Parse the subtopics into a list:
import re def extract_subtopics(text): # Expecting numbered or bulleted list from LLM output return re.findall(r"(?:\d+\.|\-)\s*(.+)", text) subtopic_list = extract_subtopics(subtopics) print("Parsed subtopics:", subtopic_list) -
Fetch headlines for each subtopic:
headlines_by_subtopic = {} for topic in subtopic_list: headlines = fetch_headlines(topic) headlines_by_subtopic[topic] = headlines print(f"Headlines for {topic}:", headlines)Screenshot description: Terminal output showing subtopic names and a few news headlines for each.
6. Implement Step 3: Context-Aware Summary Generation
-
Prepare the full context for the LLM:
context_str = f"User query: {user_query}\n\n" for topic, headlines in headlines_by_subtopic.items(): context_str += f"Subtopic: {topic}\n" context_str += "Recent headlines:\n" for h in headlines: context_str += f"- {h}\n" context_str += "\n" -
Build the summary prompt chain:
summary_prompt = ChatPromptTemplate.from_messages([ ("system", "You are an expert research assistant. Use the provided subtopics and headlines to write a concise, up-to-date summary for the user's query."), ("human", "{context}") ]) summary_chain = summary_prompt | llm summary = summary_chain.invoke({"context": context_str}).content print("Final Summary:\n", summary)Screenshot description: Terminal output with a multi-paragraph summary referencing the fetched headlines.
7. Add Branching Logic & Error Handling
Advanced prompt chaining often requires conditional logic. For example, if no headlines are found for a subtopic, you might:
- Ask the LLM to infer information based on general knowledge
- Skip the subtopic or flag it for review
for topic in subtopic_list:
headlines = fetch_headlines(topic)
if not headlines:
# Fallback: Ask LLM to provide a general summary for this subtopic
fallback_prompt = ChatPromptTemplate.from_messages([
("system", "You are an expert in the topic."),
("human", f"Provide a brief summary of the subtopic: {topic}")
])
fallback_chain = fallback_prompt | llm
summary = fallback_chain.invoke({}).content
headlines = [f"[No news found] {summary}"]
headlines_by_subtopic[topic] = headlines
This pattern is crucial for robust workflows, as covered in automating video post-production workflows with AI and other advanced automation scenarios.
8. Full Example: Putting It All Together
Here’s a condensed version of the full workflow script:
import os
import re
import requests
from dotenv import load_dotenv
from langchain.chat_models import ChatOpenAI
from langchain.prompts import ChatPromptTemplate
load_dotenv()
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
NEWSAPI_KEY = os.getenv("NEWSAPI_KEY")
llm = ChatOpenAI(openai_api_key=OPENAI_API_KEY, model_name="gpt-3.5-turbo", temperature=0.2)
def fetch_headlines(topic, max_results=3):
url = "https://newsapi.org/v2/everything"
params = {"q": topic, "apiKey": NEWSAPI_KEY, "pageSize": max_results, "sortBy": "relevancy", "language": "en"}
resp = requests.get(url, params=params)
if resp.status_code != 200:
return []
articles = resp.json().get("articles", [])
return [a["title"] for a in articles]
def extract_subtopics(text):
return re.findall(r"(?:\d+\.|\-)\s*(.+)", text)
user_query = input("Enter your research topic: ")
subtopic_prompt = ChatPromptTemplate.from_messages([
("system", "You are an expert research assistant."),
("human", "Given the query: '{query}', list 3-5 relevant subtopics to explore.")
])
subtopic_chain = subtopic_prompt | llm
subtopics = subtopic_chain.invoke({"query": user_query}).content
subtopic_list = extract_subtopics(subtopics)
headlines_by_subtopic = {}
for topic in subtopic_list:
headlines = fetch_headlines(topic)
if not headlines:
fallback_prompt = ChatPromptTemplate.from_messages([
("system", "You are an expert in the topic."),
("human", f"Provide a brief summary of the subtopic: {topic}")
])
fallback_chain = fallback_prompt | llm
summary = fallback_chain.invoke({}).content
headlines = [f"[No news found] {summary}"]
headlines_by_subtopic[topic] = headlines
context_str = f"User query: {user_query}\n\n"
for topic, headlines in headlines_by_subtopic.items():
context_str += f"Subtopic: {topic}\nRecent headlines:\n"
for h in headlines:
context_str += f"- {h}\n"
context_str += "\n"
summary_prompt = ChatPromptTemplate.from_messages([
("system", "You are an expert research assistant. Use the provided subtopics and headlines to write a concise, up-to-date summary for the user's query."),
("human", "{context}")
])
summary_chain = summary_prompt | llm
summary = summary_chain.invoke({"context": context_str}).content
print("Final Summary:\n", summary)
Common Issues & Troubleshooting
-
API Key Errors: If you see
Invalid API keyor authentication errors, double-check your.envfile and ensure the environment is loaded. -
Rate Limits: NewsAPI.org and OpenAI both have rate limits. If you hit them, add
time.sleep()between requests or upgrade your plan. -
LLM Output Parsing: If
extract_subtopicsreturns an empty list, print thesubtopicsvariable to see the actual LLM output. Adjust the regex if needed. -
Network Issues: For
requestserrors, check your internet connection and the API status pages. - Prompt Quality: If summaries are too vague, tweak the system/human messages for more specificity.
Next Steps
You’ve now built an advanced, context-aware prompt chaining workflow that integrates live data and adapts to real-world conditions. Here’s how to deepen your skills:
- Explore automating financial statement generation with AI for more domain-specific chaining examples.
- Add user feedback loops, multi-turn conversations, or database storage for persistent context.
- Experiment with multi-modal chaining (e.g., image+text) or plug in other APIs (like Wikipedia, Arxiv, or custom business data).
- For a no-code/low-code approach, revisit our comprehensive guide to prompt chaining workflows with no-code AI platforms.
Prompt chaining is a cornerstone of intelligent automation. As you master these advanced patterns, you’ll unlock new possibilities for building dynamic, context-aware AI agents. For further reading, check out how to use prompt chaining to automate complex multi-step workflows.