LCEL & ChainsLesson 4

Multi-LLM Workflows

Simple Sequential Chains used one ChatOpenAI for both steps. Here you use two — gpt-4o-mini to pick a tag, gpt-4o to write the explanation. Same pipe layout; you change which model sits on each step.

Two model variables

Create fast_model and smart_model, then plug each into the step that needs it. One chain.invoke — step 1 calls mini, step 2 calls gpt-4o.

Each step can call a different model:

input

{ feature: "links" }

step 1

gpt-4o-mini

string

"a"

step 2

gpt-4o

output

explanation

pick_chainfast_model. Explain step → smart_model.
Same chain shape — different ChatOpenAI on each step.

Compared to Simple Sequential

The chain line is the same. Instead of reusing one model, you attach fast_model to pick and smart_model to explain.

Simple Sequential

model = ChatOpenAI(
    model="gpt-4o-mini"
)

chain = pick_chain
    | explain_prompt
    | model
    | parser

Same model for every step

Multi-LLM Workflow

fast_model = ChatOpenAI(
    model="gpt-4o-mini"
)
smart_model = ChatOpenAI(
    model="gpt-4o"
)

chain = pick_chain
    | explain_prompt
    | smart_model
    | parser

mini on pick, gpt-4o on explain

The pipe layout stays the same — only which ChatOpenAI instance you attach changes per step.

Model split in this demo

Pick returns a single tag name. Explain asks for two sentences and an example — worth the larger model. Temperature and token limits per client are covered in Model Parameters.

StepModelTask
Pick taggpt-4o-miniReturn one tag name
Explain taggpt-4oTwo sentences + HTML example
Pick is one word — mini is enough. Explain asks for two sentences plus an example — use gpt-4o.

What each step does

  • pick_chain pick_prompt | fast_model | parser. Input {"feature": "links"}, output tag string.
  • explain_prompt | smart_model | parser — input that string as {tag}, output the explanation.

The demo script

multi_llm_workflows_demo.py runs the chain for links and images.

multi_llm_workflows_demo.py
"""multi_llm_workflows_demo.py"""
fast_model = ChatOpenAI(model="gpt-4o-mini")
smart_model = ChatOpenAI(model="gpt-4o")
pick_chain = pick_prompt | fast_model | parser
chain = pick_chain | explain_prompt | smart_model | parser
explanation = chain.invoke({"feature": feature})

Download the code

multi_llm_workflows_demo.py

gpt-4o-mini → gpt-4o

Download .py
Save into your langchain-course folder. Needs venv, .env, and packages from Project Setup.

Run it

Activate the venv from Project Setup, then:

python multi_llm_workflows_demo.py
PowerShell — (.venv) active
(.venv) PS C:\projects\langchain-course> python multi_llm_workflows_demo.py
Feature: links
Explanation: The <a> tag creates a clickable link to another page. For example, <a href="/about">About us</a> sends visitors to your about page.
Feature: images
Explanation: The <img> tag displays an image on the page. For example, <img src="logo.png" alt="Site logo"> shows your logo with a text fallback.
Step 1: gpt-4o-mini. Step 2: gpt-4o.

When to split models

  • The first step is short or mechanical (pick a tag, classify, extract a value) — mini keeps the bill down.
  • The second step needs longer or clearer writing — put gpt-4o (or similar) only on that step.
  • OpenAI client options: ChatOpenAI integration.

What's Next

Next: Output Formatting — parse model output into structured data.