Prompt chaining

Prompt chaining is the simplest and most fundamental agentic pattern. Instead of asking an LLM to do everything in one call, you break the task into steps. Each step gets its own prompt, and the output of one step feeds into the next.

A single prompt works for simple tasks, but it struggles with complex multi-step processes. Chaining breaks the problem into focused steps where each prompt does one thing well. It is easier to debug, test, and improve individual steps. Plus, you can use different models or temperatures for different steps.

Prompt chaining flow

Each step produces output that becomes input for the next step.

patterns/01_prompt_chaining.py
python
def research_topic(topic, llm):
    """Step 1: Research the topic."""
    prompt = f"""
    Research and provide key facts about: {topic}
    Include important dates, people, and concepts.
    """
    return llm.generate(prompt).content

def analyze_research(research, llm):
    """Step 2: Analyze the research."""
    prompt = f"""
    Analyze this research and identify key themes:
    {research}
    Highlight the most important points.
    """
    return llm.generate(prompt).content

def create_summary(analysis, llm):
    """Step 3: Create a summary."""
    prompt = f"""
    Create a concise summary from this analysis:
    {analysis}
    Make it clear and actionable.
    """
    return llm.generate(prompt).content

# Chain execution
research = research_topic("AI agents", llm)
analysis = analyze_research(research, llm)
summary = create_summary(analysis, llm)

The core chaining functions: each step calls the LLM and passes its result forward.

AI prompt: Try it with AI

Loading practice…

Matching exercise: Match the chaining concepts

Loading practice…

Quiz: Quiz

Loading practice…

Ordering exercise: Order the prompt chaining steps

Loading practice…

Flashcards: Flashcards

Loading practice…

Prompt chaining is the workhorse pattern you will reach for most often. Any time a task has clear sequential steps, chaining gives you better results and easier debugging. Next, we will learn routing, where the AI decides which specialist should handle each request.