Building Your First Chain
Now it's time to put everything together and build a complete prompt chain. We'll walk through the process step by step with a practical example.
The Task: Product Review Analyzer
We'll build a chain that:
- Extracts key information from a product review
- Analyzes the sentiment and identifies specific feedback
- Generates an appropriate response
This is a common real-world use case for customer service automation.
Step 1: Define the Chain Architecture
Before writing any prompts, map out the flow:
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ STEP 1 │ │ STEP 2 │ │ STEP 3 │
│ Extract │ ───► │ Analyze │ ───► │ Generate │
│ Information │ │ Sentiment │ │ Response │
└─────────────────┘ └─────────────────┘ └─────────────────┘
│ │ │
▼ ▼ ▼
Structured Sentiment + Customer
Review Data Themes Response
Step 2: Design Each Step
Step 1: Extract Information
Purpose: Pull structured data from unstructured review text
Step 2: Analyze Sentiment and Themes
Purpose: Determine overall sentiment and categorize the feedback
Step 3: Generate Response
Purpose: Create an appropriate customer response based on the analysis
Step 3: Connect the Steps
Now let's see how the outputs flow between steps:
// Pseudo-code for the chain
async function processReview(reviewText) {
// Step 1: Extract
const extractPrompt = `Extract information from: ${reviewText}`;
const extracted = await runPrompt(extractPrompt);
// Validate Step 1 output
if (!extracted.product_type || !extracted.positives || !extracted.negatives) {
throw new Error("Extraction incomplete");
}
// Step 2: Analyze
const analyzePrompt = `Analyze this data: ${JSON.stringify(extracted)}`;
const analysis = await runPrompt(analyzePrompt);
// Validate Step 2 output
if (!analysis.overall_sentiment || !analysis.recommended_action) {
throw new Error("Analysis incomplete");
}
// Step 3: Generate
const generatePrompt = `
Generate response based on:
Analysis: ${JSON.stringify(analysis)}
Positives: ${extracted.positives.join(", ")}
Negatives: ${extracted.negatives.join(", ")}
`;
const response = await runPrompt(generatePrompt);
return {
extracted,
analysis,
response
};
}
Complete Chain Exercise
Now try running the complete chain yourself. Start with a new review:
Take your Step 1 output and use it for Step 2:
Finally, generate a response:
What You Just Built
Congratulations! You've just run a complete 3-step prompt chain:
- Extract: Unstructured text → Structured data
- Analyze: Structured data → Insights and recommendations
- Generate: Insights → Human-ready output
Each step:
- Had a clear single purpose
- Produced structured, verifiable output
- Passed exactly what the next step needed
Common Issues and Fixes
Issue: JSON Parsing Fails
Symptom: Step output includes extra text around the JSON
Fix: Add explicit instructions:
Return ONLY valid JSON. No explanations, no markdown formatting.
Start your response with { and end with }
Issue: Missing Fields
Symptom: Some expected fields are null or missing
Fix: Make fields explicit in the prompt:
You MUST include all of these fields, even if the value is null or empty:
- field1 (required)
- field2 (required)
- field3 (use [] if none found)
Issue: Inconsistent Output Format
Symptom: Sometimes JSON, sometimes prose
Fix: Add format enforcement:
OUTPUT FORMAT: JSON only
VALIDATION: Your response must be parseable by JSON.parse()
Key Takeaways
- Start by mapping your chain architecture before writing prompts
- Each step should have a single, clear purpose
- Design outputs to be easily consumable by the next step
- Validate outputs between steps to catch errors early
- Test each step independently before connecting them
- Use explicit format instructions to ensure consistent outputs
You now have the foundational skills to build prompt chains. In the next module, we'll explore design principles for creating robust, maintainable chains.

