AI Prompt Chaining: Breaking Down Complex Tasks Step by Step

🤖 Agents 2026-07-19 2 min read

AI Prompt Chaining: Breaking Down Complex Tasks Step by Step

📖 Keep reading

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-16
AI Agent Readiness Probe 2026
2026-07-17
AI Agent Feature Flags 2026
2026-08-21
Hermes Agent major version update cuts first response time from 4.3 seconds to 0.9 seconds, and now supports password managers
2026-08-10
AI Agent to Apply to Jobs on LinkedIn: How It Works and the Terms You Should Know
2026-07-16
Multi-Agent System Architecture: Design Guide for Devs 2026
2026-07-16
AI Agent Rate Limiting Strategy 2026

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