Prompt Chaining
Instead of asking a single LLM to perform 5 complex tasks in one massive prompt, you break the task into 5 separate, focused prompts. The output of Prompt 1 becomes the input for Prompt 2.
Why Does This Exist?
When developers first start building with GenAI, they suffer from the "Mega-Prompt" anti-pattern. They write a 2,000-word system prompt asking the LLM to:
- Extract named entities from a document.
- Cross-reference those entities against a list.
- Translate the results into Spanish.
- Format the final output as a specific JSON schema.
- Ensure the tone is strictly professional.
When you ask an LLM to juggle 5 complex constraints simultaneously, it will almost always drop one. It will format the JSON perfectly, but forget the translation. Or it will translate it perfectly, but hallucinate an entity.
Prompt Chaining solves this by decomposing the Mega-Prompt into a series of highly specialized, isolated LLM calls (nodes). The output of one node is fed into the next node as input.
Think of It Like This
The Assembly Line
Mega-Prompt: You hire a single brilliant artisan to build a car from scratch. They have to weld the frame, install the engine, wire the electronics, and paint the exterior simultaneously. They get overwhelmed, forget the turn signals, and the paint is smudged.
Prompt Chaining: You build a factory assembly line.
- Worker 1 (LLM 1) only welds the frame.
- Worker 2 (LLM 2) takes the frame and only installs the engine.
- Worker 3 (LLM 3) takes the motorized frame and only paints it.
Because each worker is entirely focused on a single, isolated task, the quality and reliability of the final product skyrockets.
How It Actually Works
Prompt chaining is orchestrated in application code (Python, Node.js) or visually in frameworks like Langflow or Flowise.
The Pipeline Architecture
A typical chain might look like this:
- The Classifier: The user inputs text. The first LLM call categorizes it (e.g., "Support Ticket" vs "Sales Inquiry").
- The Router (Code): A Python
ifstatement reads the classification. If "Support", it routes to the Support Chain. - The Extractor: The second LLM extracts the specific error code and user email from the text.
- The Responder: The third LLM takes the extracted error code and drafts an apology email.
Why is this better?
- Specialization: You can use a massive, expensive model (GPT-4) for the difficult logical extraction step, but use a fast, cheap model (GPT-4o-mini) for the simple formatting and translation steps, saving massive amounts of money.
- Debugging: If the Mega-Prompt fails, you don't know why. If a Chain fails, you can look at the intermediate outputs. You know exactly which node failed and can adjust the prompt for that specific node without breaking the rest of the system.
- Reliability: By removing competing constraints from the prompt, hallucination rates drop dramatically.
Show Me the Code
This code demonstrates a 3-step chain to process a raw customer review into a formatted database entry.
import openaiimport json
def call_llm(prompt, model="gpt-4o-mini"): response = openai.chat.completions.create( model=model, messages=[{"role": "user", "content": prompt}], temperature=0.0 # Keep chains deterministic ) return response.choices[0].message.content.strip()
def process_review_chain(raw_review): print("--- Starting Chain ---") # Node 1: Translation (if needed) and Cleanup print("Node 1: Translating and cleaning...") clean_text = call_llm(f"Translate this review to English and fix typos. Only output the clean text.\nReview: {raw_review}") print(f" Result: {clean_text}") # Node 2: Sentiment and Extraction print("\nNode 2: Extracting sentiment and product name...") extraction_prompt = f""" Analyze this review: "{clean_text}" Output exactly two lines: Sentiment: (Positive/Negative/Neutral) Product: (Name of the product) """ extraction_result = call_llm(extraction_prompt) print(f" Result:\n {extraction_result.replace('\n', '\n ')}") # Node 3: Formatting (We can use a cheaper model here if we want) print("\nNode 3: Formatting as JSON...") format_prompt = f""" Convert this extracted data into valid JSON with keys 'sentiment' and 'product'. Data: {extraction_result} """ json_output = call_llm(format_prompt) print("\n--- Final Pipeline Output ---") print(json_output) return json_output
# --- Execution ---raw_input = "La battería de mi nuevo iPhone 15 Pro es terrible, se muere en dos horas."process_review_chain(raw_input)
# -> --- Starting Chain ---# -> Node 1: Translating and cleaning...# -> Result: The battery of my new iPhone 15 Pro is terrible, it dies in two hours.# -> # -> Node 2: Extracting sentiment and product name...# -> Result:# -> Sentiment: Negative# -> Product: iPhone 15 Pro# -> # -> Node 3: Formatting as JSON...# -> # -> --- Final Pipeline Output ---# -> {# -> "sentiment": "Negative",# -> "product": "iPhone 15 Pro"# -> }Watch Out For
Latency Accumulation
Prompt chaining trades speed for reliability. If you break a task into 4 sequential LLM calls, and each call takes 1.5 seconds, your user is waiting 6 seconds for a response. For real-time chat applications, deep chains are often unacceptably slow. You must aggressively parallelize independent nodes (e.g., extracting the sentiment and translating the text at the same time on two different threads) to reduce latency.
The Quick Version
- The "Mega-Prompt" (asking one LLM to do 5 complex things at once) is unreliable and difficult to debug.
- Prompt Chaining breaks the complex task into a sequence of small, highly focused prompts.
- The output of the first prompt is injected via code as the input to the second prompt.
- This creates an "Assembly Line" that is highly reliable, easy to debug, and allows you to mix and match cheap and expensive models for different steps.
- The major drawback is increased latency due to multiple sequential API calls.
What to Read Next
- Read ReAct Pattern to see what happens when the LLM gets to decide how to chain its own prompts together dynamically (Agentic behavior).
- Read Prompt Anatomy to learn how to write the specific instructions for each node in your chain.
- Read Query Decomposition for an example of how chaining is used to break down complex search queries.