Below is a practical “recipe” for chaining prompts—turning the output of one call into the input of the next—so you can build multi-step reasoning, summarization, or generation pipelines with any LLM (OpenAI, Anthropic, open-source, etc.).
────────────────────────────────────────
- Decide the pipeline structure
────────────────────────────────────────
A. Linear chain
Prompt-1 → Prompt-2 → Prompt-3 → … → Final answer
(Each step consumes the previous step’s text.)
B. Branch / merge
Split the output of Prompt-1 into two parallel prompts, then merge their results.
C. Loop (self-refine)
Prompt-1 → Prompt-2 (critique) → Prompt-1 again (revise) … until a stop condition.
────────────────────────────────────────
2. Design each prompt as a “function”
────────────────────────────────────────
Treat every prompt like a pure function with:
• Input variables (the previous step’s text)
• Clear instructions (what to do with that text)
• Output format (JSON, bullet list, “Answer: …”, etc.) so the next step can parse it.
Example skeleton for step k:
You are Step k in a pipeline.
Input: {previous_output}
Task: <exact task>
Constraints: <length, style, format>
Output format: <JSON schema or plain text marker>
────────────────────────────────────────
3. Pass context explicitly
────────────────────────────────────────
• Always include the previous step’s output inside the next prompt.
• If the context grows too long, summarize or extract key fields at each step.
• Use delimiters (###, XML tags, JSON) so the model knows where the new instructions start.
────────────────────────────────────────
4. Orchestrate with code
────────────────────────────────────────
Pseudo-Python (works the same in JS, Go, etc.):
import openai
def call_llm(prompt: str) -> str:
resp = openai.ChatCompletion.create(
model="gpt-4",
messages=[{"role": "user", "content": prompt}]
)
return resp.choices[0].message.content
# Step 1: Extract facts
facts = call_llm(
f"Extract all factual claims from the following article:\n\n{article}"
)
# Step 2: Fact-check
checked = call_llm(
f"For each claim below, say whether it is TRUE, FALSE, or UNCLEAR.\n\n{facts}"
)
# Step 3: Summarize results
summary = call_llm(
f"Summarize the fact-check results in 3 bullet points:\n\n{checked}"
)
────────────────────────────────────────
5. Use built-in chaining tools (optional)
────────────────────────────────────────
• LangChain (Python/JS) – SequentialChain, LLMChain, memory, etc.
• LlamaIndex – “query pipeline” nodes.
• OpenAI “Assistants” – threads keep context automatically.
These libraries just wrap the same pattern above with retries, caching, and token counting.
────────────────────────────────────────
6. Guardrails & tips
────────────────────────────────────────
- Token budget: Estimate total tokens across all steps; truncate or summarize early.
- Idempotency: Make prompts deterministic (temperature=0) for reproducible pipelines.
- Validation: After each step, parse the output; if malformed, retry or fall back.
- Observability: Log every prompt/response pair for debugging.
- Parallelization: Independent branches can run concurrently to save latency.
────────────────────────────────────────
Mini-template library
────────────────────────────────────────
Step-1 Extract
“List every mentioned in the text below. Return as JSON array.”
Step-2 Transform
“Rewrite each as a one-sentence Wikipedia-style definition.”
Step-3 Synthesize
“Combine the definitions into a coherent 150-word paragraph.”
────────────────────────────────────────
End-to-end example (blog → tweet thread)
────────────────────────────────────────
- Summarize blog post → 5 bullets
- Convert bullets → 280-character tweets
- Add hashtags & emojis
- Return JSON array of tweets
Code:
bullets = call_llm(f"Summarize this blog post in 5 bullets:\n{blog}")
tweets = call_llm(f"Turn each bullet into a 280-char tweet:\n{bullets}")
hashed = call_llm(f"Add 2 relevant hashtags and 1 emoji to each tweet:\n{tweets}")
────────────────────────────────────────
Key takeaway
────────────────────────────────────────
Chaining is just “prompt-as-function”:
outputₙ = LLM(promptₙ(outputₙ₋₁))
Wrap that in code or a framework, enforce strict formats, and you can build arbitrarily complex reasoning flows while keeping each individual prompt small and reliable.