Route Between LLMs
Multi-LLM Workflows called mini, then gpt-4o on every invoke. This lesson calls one or the other. Tags in COMPLEX_TAGS (form, table, …) go to detail_chain with gpt-4o. Everything else goes to simple_chain with mini. RunnableBranch picks the path.
Compared to Multi-LLM Workflows
Last lesson ran pick and explain back to back. This lesson checks tag first and runs one chain.
pick_chain → mini explain → gpt-4o Both steps run every time.
Route Between LLMs
if tag in COMPLEX_TAGS:
detail_chain → gpt-4o
else:
simple_chain → mini
One branch per invoke.RunnableBranch
List (condition, chain) tuples, then pass the default chain last. First True condition wins. LangChain routing docs.
Input with tag=form:
input
{ "tag": "form" }
condition
tag in COMPLEX_TAGS?
yes
detail_chain
gpt-4o
no
simple_chain
gpt-4o-mini
form is in COMPLEX_TAGS, so detail_chain runs. simple_chain does not.simple_chain and detail_chain
Two separate pipes: short prompt + mini, longer prompt + gpt-4o. The router passes {"tag": "..."} to whichever chain matches.
| Tag | Branch | Model |
|---|---|---|
| a, p, img | simple_chain | gpt-4o-mini |
| form, table, select | detail_chain | gpt-4o |
COMPLEX_TAGS in the script to change which tags use gpt-4o.Run the script
venv active, from the project folder:
python route_between_llms_demo.py
route_between_llms_demo.py
RunnableBranch — mini or gpt-4o
What's Next
Next lesson: Output Formatting (StrOutputParser and PydanticOutputParser).