HOW-TO · DEV
How to chain prompts together to break complex tasks into sequential AI reasoning steps
Target environment
Ubuntu 24.04 · Python 3.12Ubuntu 24.04 · Python 3.12
PREREQUISITES
AI API with chat/continuation support (OpenAI, Anthropic, or compatible), Python 3.10+, basic API interaction library
What this does
Prompt chaining decomposes a complex, multi-step task into a series of smaller, focused prompts where each step consumes output from the previous step. This technique improves reliability, reduces hallucination, and makes reasoning traceable by keeping each prompt narrow and verifiable. Chaining is particularly effective for tasks such as research synthesis, multi-document analysis, and procedural code generation.
Steps
- Define the overall task goal and identify logical phase boundaries. Example phases for a research summary:
extract,analyze,synthesize. - Create a prompt template for phase 1 that takes a raw input and produces structured output matching the next phase's input schema.
- Execute phase 1 prompt against the API, capturing the response in a variable or file.
- Validate phase 1 output before proceeding — check that required fields are present and non-empty.
- Construct the phase 2 prompt by inserting phase 1 output as context. Keep instructions focused on phase 2's objective only.
- Execute phase 2 prompt and capture output.
- Repeat validation and chaining for each subsequent phase.
- Implement a loop or pipeline function that reads prior output, injects into next prompt, and writes result to a shared context object.
- Add error handling: if any phase returns a malformed response, retry the same phase up to a configurable maximum.
- Serialize the final output for downstream use.
Verification
python3 -c "
import json, subprocess, sys
result = subprocess.run([sys.executable, '-c', '''
from openai import OpenAI
client = OpenAI()
msgs = [
{'role': 'user', 'content': 'List the primary colors. Respond with exactly three words.'},
{'role': 'assistant', 'content': 'red green blue'},
{'role': 'user', 'content': 'Capitalize each word and return as JSON list.'}
]
resp = client.chat.completions.create(model='gpt-4o-mini', messages=msgs)
print(resp.choices[0].message.content)
'''], capture_output=True, text=True)
print(result.stdout.strip())
"
Expected output: a JSON-formatted list such as ["RED", "GREEN", "BLUE"].
Common failures
- Phase output drifts from schema: the next prompt receives malformed data. Solution: add a validation function after each phase and re-prompt with schema correction instructions on failure.
- Context window overflow: long chained outputs accumulate and exceed model limits. Solution: implement truncation or summarization of prior phases before injection.
- Inconsistent separator formatting: multi-phase output uses inconsistent delimiters. Solution: define strict output grammar rules in each phase prompt template.
- Silent hallucination in intermediate steps: downstream phases propagate incorrect facts. Solution: add a verification sub-step within each phase that asks the model to self-certify output accuracy.