AI Prompt Chaining: Breaking Down Complex Tasks Step by Step

๐Ÿ“˜ Tutorials 2026-07-19 2 min read

AI Prompt Chaining: Breaking Down Complex Tasks Step by Step

💡 What You Will Learn

AI Prompt Chaining: Breaking Down Complex Tasks Step by Step

from langgraph.graph import StateGraph
from typing import TypedDict, Optional

class ProductState(TypedDict):
    raw_input: str
    extracted_info: dict
    title: str
    description: str
    seo_keywords: list
    final_output: str

# Steps1Extract key information
def extract_info(state: ProductState) -> dict:
    prompt = f"""
Product name, brand, specs, price, key features (max 3)

{state['raw_input']}

JSON"""
    result = llm.invoke(prompt)
    return {"extracted_info": json.loads(result)}

# Steps2Generate
def generate_title(state: ProductState) -> dict:
    info = state['extracted_info']
    prompt = f"""3
{info['name']}
{info['brand']}
{info['spec']}
{', '.join(info['key_points'])}
30"""
    return {"title": llm.invoke(prompt)}

# Steps3
def generate_description(state: ProductState) -> dict:
    prompt = f"""200-300
{state['title']}
{state['extracted_info']['brand']}
{state['extracted_info']['spec']}
{state['extracted_info']['price']}
"""
    return {"description": llm.invoke(prompt)}

# Steps4SEO
def extract_seo_keywords(state: ProductState) -> dict:
    prompt = f"""5SEO

{state['title']}
{state['description']}

23-521"""
    return {"seo_keywords": llm.invoke(prompt).split('\n')[:5]}

# Steps5
def assemble(state: ProductState) -> dict:
    output = f"""
{state['title']}


{state['description']}

SEO
{', '.join(state['seo_keywords'])}"""
    return {"final_output": output}

# Chain
workflow = StateGraph(ProductState)
workflow.add_node("extract", extract_info)
workflow.add_node("title", generate_title)
workflow.add_node("desc", generate_description)
workflow.add_node("seo", extract_seo_keywords)
workflow.add_node("assemble", assemble)
workflow.set_entry_point("extract")
workflow.add_edge("extract", "title")
workflow.add_edge("title", "desc")
workflow.add_edge("desc", "seo")
workflow.add_edge("seo", "assemble")
chain = workflow.compile()

|:----|:---------------|:--------|

Related Articles
2026-07-27
ComfyUI Workflow Tutorial 2026: Node-Based AI Image Generation
2026-08-01
AI Coding Assistants with Free Trials in 2026: 9 Compared
2026-07-18
Prompt Engineering for Google 2026: Writing for the AI Search Era

Written by our editorial team; tools listed here are tested or verified against public sources. Links point to official sites or GitHub repos for reference only โ€” no paid placements.

๐Ÿ’ฌ Comments (0)

No comments yet. Be the first!

Login to comment