What Is Prompt Chaining?
The concept of sequential prompt execution and information passing.
What Is Prompt Chaining? is a free AI Prompt Engineering lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the AI Prompt Engineering learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
The Single-Prompt Ceiling
A single prompt can do a lot, but it has limits. Complex tasks that require multiple specialized sub-tasks, very long context, or iterative refinement often exceed what one prompt can do reliably.
Signs you have hit the single-prompt ceiling:
- The model skips steps in a multi-step task
- Quality drops for the later parts of a long task
- The output is too long to fit in one context window
- Different parts of the task require different expertise or tones
What Is Prompt Chaining?
Prompt chaining is sequential execution where each prompt's output becomes the input for the next prompt. Instead of one large prompt trying to do everything, you break the task into specialized steps.
Step 1 → output → Step 2 → output → Step 3 → final result
Each step can be optimized independently, use different models, or apply different constraints.
A Simple Chaining Example
Consider writing a blog post. Instead of one prompt, you chain three:
import anthropic
client = anthropic.Anthropic(api_key='YOUR_API_KEY')
def call(prompt):
r = client.messages.create(
model='claude-opus-4-5',
max_tokens=1000,
messages=[{'role': 'user', 'content': prompt}]
)
return r.content[0].text
# Step 1: Generate outline
outline = call('Create a 5-point outline for a blog post about prompt engineering for beginners.')
# Step 2: Expand outline into draft
draft = call(f'Write a full blog post based on this outline:\n\n{outline}')
# Step 3: Edit for clarity
final = call(f'Edit this blog post for clarity and conciseness. Remove jargon:\n\n{draft}')
print(final[:500])When Chaining Helps: Task Complexity
Chaining excels when a task is too complex for one prompt to handle with high quality. Breaking complexity into specialized steps allows each step to excel at its narrow job.
Example: Analyzing customer feedback at scale
- Step 1: Extract all issues mentioned (extraction)
- Step 2: Categorize each issue (classification)
- Step 3: Prioritize by frequency and severity (ranking)
- Step 4: Write an executive summary (synthesis)
Each step uses a focused prompt optimized for its sub-task.
When Chaining Helps: Quality Through Specialization
Specialized prompts outperform generalist prompts for their specific sub-task. A chain allows you to use the right prompt style for each stage:
# Each step uses a prompt optimized for its role
step1_prompt_template = '''
<task>Extract all named entities from the text below. Return JSON:
{"people": [], "companies": [], "locations": []}</task>
<text>{text}</text>
'''
step2_prompt_template = '''
<task>For each company in the list below, classify it as:
startup, enterprise, government, or nonprofit.
Return JSON: [{"company": str, "type": str}]</task>
<companies>{companies}</companies>
'''
# Each prompt is simpler, more focused, and easier to debug
# than a single prompt trying to do both tasks at once.
print('Specialized prompts per step.')When Chaining Helps: Context Window Limits
Even with large context windows (100K+ tokens), processing very long documents in one shot degrades quality due to the lost-in-the-middle effect. Chaining provides a solution:
- Step 1: Process each chunk independently → produce a summary or extraction per chunk
- Step 2: Combine the chunk outputs → synthesize a final answer
This map-reduce pattern is a fundamental chaining strategy for long documents.
def map_reduce_summarize(document, chunk_size=3000):
chunks = [document[i:i+chunk_size] for i in range(0, len(document), chunk_size)]
# Map: summarize each chunk
summaries = []
for i, chunk in enumerate(chunks):
summary = call(f'Summarize this section of a document in 3 bullet points:\n\n{chunk}')
summaries.append(summary)
# Reduce: synthesize all summaries
combined = '\n\n'.join(f'Section {i+1}:\n{s}' for i, s in enumerate(summaries))
final = call(f'Combine these section summaries into a single executive summary:\n\n{combined}')
return finalSynchronous vs Parallel Chains
Chains are not always linear. Some steps can run in parallel when they are independent:
import concurrent.futures
def parallel_step(topics):
# Each topic can be researched in parallel — they do not depend on each other
def research_topic(topic):
return call(f'List 5 key facts about: {topic}')
with concurrent.futures.ThreadPoolExecutor() as executor:
results = list(executor.map(research_topic, topics))
# Then synthesize in a sequential step
combined = '\n\n'.join(f'{t}:\n{r}' for t, r in zip(topics, results))
synthesis = call(f'Write a comparative analysis of these topics:\n\n{combined}')
return synthesis
print('Parallel steps can reduce total latency.')Chains vs Agents
Prompt chains and AI agents are related but different:
- Chains: Pre-defined, deterministic flow. Step sequence is fixed. Easier to test, debug, and predict.
- Agents: Dynamic flow. The model decides the next step based on output. More flexible but harder to control and debug.
For most production use cases, start with chains. Use agents only when the task is genuinely open-ended and the chain structure cannot be determined in advance.
Cost Considerations in Chains
Each step in a chain is a separate API call with its own cost. Design chains with cost awareness:
- Use cheaper/smaller models for simple steps (extraction, classification)
- Reserve expensive models (GPT-4o, Claude Opus) for steps requiring high reasoning
- Cache intermediate results to avoid re-running expensive steps on identical inputs
- Fail fast — validate each step's output before passing to the next
import anthropic
import functools
client = anthropic.Anthropic(api_key='YOUR_API_KEY')
@functools.lru_cache(maxsize=128)
def cached_call(prompt, model='claude-haiku-4-5'):
r = client.messages.create(
model=model,
max_tokens=500,
messages=[{'role': 'user', 'content': prompt}]
)
return r.content[0].text
# Cheap model for extraction, expensive for synthesis
def smart_chain(text):
entities = cached_call(f'Extract entities from: {text}', model='claude-haiku-4-5')
synthesis = cached_call(f'Analyze these entities: {entities}', model='claude-opus-4-5')
return synthesisDocumenting Your Chain
A well-documented chain is maintainable. Use a simple structure to describe each step:
chain_spec = {
'name': 'Blog Post Generator',
'steps': [
{
'id': 'step_1',
'name': 'Outline Generation',
'model': 'claude-haiku-4-5',
'input': 'topic (string)',
'output': 'outline (string, 5 bullet points)',
'prompt_template': 'outline_prompt.txt'
},
{
'id': 'step_2',
'name': 'Draft Writing',
'model': 'claude-opus-4-5',
'input': 'outline from step_1',
'output': 'draft (string, ~800 words)',
'prompt_template': 'draft_prompt.txt'
},
{
'id': 'step_3',
'name': 'Editorial Polish',
'model': 'claude-haiku-4-5',
'input': 'draft from step_2',
'output': 'final post (string)',
'prompt_template': 'polish_prompt.txt'
}
]
}
print('Chain documented with step specs.')Real World Chain: Competitive Analysis
A practical three-step chain for competitive analysis:
def competitive_analysis_chain(competitor_list, product_description):
# Step 1: Research each competitor (parallel)
def research(competitor):
return call(f'List key features, pricing model, and target market for: {competitor}')
with concurrent.futures.ThreadPoolExecutor() as ex:
research_results = dict(zip(competitor_list, ex.map(research, competitor_list)))
# Step 2: Compare against our product
comparison_input = '\n\n'.join(f'{k}:\n{v}' for k, v in research_results.items())
comparison = call(f'Compare these competitors against our product:\nOur product: {product_description}\n\nCompetitors:\n{comparison_input}')
# Step 3: Strategic recommendations
recommendations = call(f'Based on this competitive analysis, provide 3 strategic recommendations:\n\n{comparison}')
return recommendationsQuick Check
Which scenario is best suited for prompt chaining rather than a single prompt?
Prompt Chaining — Key Takeaways
Prompt chaining is the foundation of production AI systems:
- Sequential execution where each output feeds the next prompt as input
- Enables specialization — each step uses a prompt optimized for its narrow sub-task
- Solves context window limits via map-reduce patterns
- Independent steps can run in parallel to reduce latency
- More predictable and debuggable than fully agentic systems
- Optimize costs by using cheaper models for simple steps and expensive models for complex reasoning
- Always document your chain with step IDs, input/output specs, and model assignments
Frequently asked questions
Is the “What Is Prompt Chaining?” lesson free?
Yes — the full text of “What Is Prompt Chaining?” is free to read here on the web, and the AI Prompt Engineering course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the AI Prompt Engineering course, upgrade to CoddyKit PRO.
What will I learn in “What Is Prompt Chaining?”?
The concept of sequential prompt execution and information passing. You practise AI Prompt Engineering with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start AI Prompt Engineering?
No prior experience is required. AI Prompt Engineering on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “What Is Prompt Chaining?” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this AI Prompt Engineering lesson?
Yes. Every AI Prompt Engineering lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- What Is Prompt Chaining?
- Output-to-Input Patterns
- Sequential Transformation Chains
- Error Handling in Prompt Chains