> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# LCEL Demo 5

> Demonstrates an LCEL chain-of-chains pipeline that generates a title, outline, 200-word blog post, and social-media summary using prompts, LLMs, parsers, and passthroughs.

This lesson demonstrates a practical, end-to-end LCEL content-generation pipeline that composes multiple mini-chains into a single workflow. The pipeline produces an impactful title, a detailed outline, a 200-word blog post, and a short social-media style summary by chaining prompt → LLM → parser → passthrough for each stage.

## Key idea and imports

We use:

* `ChatPromptTemplate` to define prompts with placeholders
* `ChatOpenAI` as the LLM runnable (swapable per stage)
* `StrOutputParser` to extract clean string outputs
* `RunnablePassthrough` to attach parsed strings to keys in the execution context so downstream mini-chains can reference them

<Callout icon="lightbulb" color="#1CB2FE">
  This pattern composes small, focused runnables into a chain-of-chains. Each stage returns a parsed string that is then attached to the execution context under a key like `title`, `outline`, or `blog`, allowing subsequent prompts to reference those values via placeholders (for example, `"{title}"`).
</Callout>

Example Python imports and the pipeline wiring:

```python theme={null}
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI
from langchain_core.runnables import RunnablePassthrough
```

## Example pipeline (title → outline → blog → summary)

Below is a compact, readable pipeline that demonstrates the pattern. Note how each mini-chain ends with a `StrOutputParser()` followed by attaching the parsed value to a new key with `RunnablePassthrough()`.

```python theme={null}
# Generate an impactful title for the input
title = (
    ChatPromptTemplate.from_template("Generate an impactful title for {input}")
    | ChatOpenAI()
    | StrOutputParser()
    | {"title": RunnablePassthrough()}
)

# Generate a detailed outline for the generated title
outline = (
    ChatPromptTemplate.from_template("Generate a detailed outline for {title}")
    | ChatOpenAI()
    | StrOutputParser()
    | {"outline": RunnablePassthrough()}
)

# Generate a blog post based on the outline
blog = (
    ChatPromptTemplate.from_template(
        "Generate a 200 word blog post based on the outline: {outline}"
    )
    | ChatOpenAI()
    | StrOutputParser()
    | {"blog": RunnablePassthrough()}
)

# Generate a short summary for the blog post (for social media)
summary = (
    ChatPromptTemplate.from_template("Generate a short summary for the post: {blog}")
    | ChatOpenAI()
    | StrOutputParser()
)

# Compose the full content chain (chain of chains)
content_chain = title | outline | blog | summary
```

## Pipeline overview (quick reference)

| Stage   | Prompt placeholder used | Output attached key | Purpose                                        |
| ------- | ----------------------- | ------------------- | ---------------------------------------------- |
| Title   | `"{input}"`             | `title`             | Create a short, impactful title                |
| Outline | `"{title}"`             | `outline`           | Produce a structured outline for the title     |
| Blog    | `"{outline}"`           | `blog`              | Expand the outline into \~200 words of content |
| Summary | `"{blog}"`              | (final output)      | Create a short social-media-ready summary      |

## Notes on placeholders and MDX safety

* When mentioning prompt placeholders in prose, always wrap them in backticks so MDX does not interpret curly braces as JavaScript: `"{input}"`, `"{title}"`, `"{outline}"`, `"{blog}"`.

## How RunnablePassthrough connects stages

Each mini-chain parses the LLM output into a string via `StrOutputParser()`. Using `| {"title": RunnablePassthrough()}` attaches that parsed string under the `title` key in the chain's execution context. Downstream chat prompts can then reference `"{title}"` to receive the exact string produced by the previous stage.

This produces a chain-of-chains effect: the output from one mini-chain becomes the input to the next via context keys.

## Invoking the pipeline

Call the top-level chained runnable with a single input dictionary. Intermediate values (like `title`, `outline`, `blog`) will be attached to the chain execution context while the final return value is the summary.

```python theme={null}
response = content_chain.invoke({"input": "The impact of AI on jobs"})
# `response` contains the final chain output (the summary).
# Intermediate outputs are available in the chain execution context as `title`, `outline`, and `blog`.
```

## Execution considerations

* This is a multi-stage pipeline: each mini-chain triggers a separate LLM call and parsing step. Expect longer runtimes than a single LLM request.
* For rapid iteration you can execute each mini-chain independently to validate or refine a stage without running the entire pipeline.

## Extending the pipeline: different LLMs per stage

A common pattern is to use specialized models per task:

* Use a fast, cheap model tuned for short creative outputs for the `title` stage.
* Use a model good at structured responses for the `outline` stage.
* Use a high-quality, longer-context model for the `blog` stage.

To swap models, replace `ChatOpenAI()` with another LLM runnable at the stage you want to change. The LCEL wiring (prompts → parser → passthrough) remains identical.

<Callout icon="lightbulb" color="#1CB2FE">
  Using different LLMs per mini-chain is a powerful approach: you can optimize cost and quality by selecting the best model for each subtask (e.g., catchy titles, structured outlines, long-form writing, and concise summarization).
</Callout>

## What we achieved

* Demonstrated how LCEL composes small runnable units (prompt → LLM → parser → attached key) into a larger, maintainable workflow.
* Built a reusable pattern for content generation, multi-stage processing, and orchestrating different LLMs for specialized subtasks.

## Links and references

* [LangChain Core documentation](https://python.langchain.com/)
* [OpenAI API documentation](https://platform.openai.com/docs)

In a future lesson we will explore memory, retrieval, and building more complex chains and integrations—stay tuned.

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/langchain/module/754457c5-1386-422b-98ad-3342dfc6aab3/lesson/1f3becbf-1cff-4ffc-9082-6e462c0617b6" />
</CardGroup>
